I am working on a menu program and I want to be able to enter as many selections as I want without the menu looping as well after the input, currently it either infinite-loops, or the program ends after one input, being unable to perform a different selection after the first. Also I want to have a case where I exit the menu if I press 0.
public void showMenu() {
System.out.println("Welcome!");
System.out.println("Select an option:\n" +
"1. Adunare\n" +
"2. Scadere\n" +
"3. Inmultire\n" +
"4. Impartire\n" +
"5. Comparare numere\n" +
"6. List To Hundred\n" +
"7. Nr to list\n" +
"8. Contains\n" +
"9. Even numbers\n" +
"10. List of Strings\n" +
"11. Second largest number\n" +
"12. Second lowest number\n" +
"13. Number \n" +
"14. Number 2 \n" +
"15. Number 3\n" +
"16. String\n" +
"17. String 2\n" +
"18. Amount of snow\n" +
"19. Eligible to vote test\n" +
"20. Odd or even\n" +
"21. Dog\n" +
"22. Cat\n" +
"23. Elev");
}
public void runProgram() {
showMenu();
int numberFromUser = citire.readNumbers();
do {
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Add
numberFromUser = citire.readNumbers();
into the loop to take numbers in the loop.
public void runProgram() {
showMenu();
int numberFromUser;
do {
numberFromUser = citire.readNumbers();
switch (numberFromUser) {
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiply();
break;
case 4:
divide();
break;
case 5:
comparareNumere();
break;
case 6:
listhundred();
break;
case 7:
setnumbertolist();
break;
case 8:
contain();
break;
case 9:
limit();
break;
case 10:
list();
break;
case 11:
secmax();
break;
case 12:
secmin();
break;
case 13:
nr();
break;
case 14:
nr2();
break;
case 15:
nr3();
break;
case 16:
string();
break;
case 17:
string2();
break;
case 18:
weather();
break;
case 19:
eligible();
break;
case 20:
oddoreven();
break;
case 21:
dog();
break;
case 22:
cat();
break;
case 23:
elev();
break;
default:
break;
}
} while (numberFromUser != 0);
}
Try changing runProgram to following :
public void runProgram()
{
int numberFromUser;
showMenu();
while(true)
{
System.out.print("Enter choice: ");
numberFromUser = citire.readNumbers();
switch(NumberFromUser)
{
// All your case statements
case 0: System.exit(0);
}
}
}
Related
I programing for my Java class and I am doing a imperial to metric conversor, everything is going good but I am having trouble to display the results from the array it is storing, I want it to display everything in the same dialog box, I am trying to use JOptionPane but it is not working and I really do not know what to try or how to do it.
package imperialconvertor;
import javax.swing.JOptionPane;
/**
* #author Mateus Holovaty
*/
public class ImperialConvertor {
public static void main(String[] args) {
//List of variables
MetricToImperial[] met = new MetricToImperial[10];
ImperialToMetric[] imp = new ImperialToMetric[10];
String optionInput;
String result = "";
int a = 0, b = 0;
boolean quit = false;
while(!quit){
optionInput = JOptionPane.showInputDialog("Enter \n1: for metric to imperial \n2: for imperial to metric \nQ: to quit");
switch(optionInput){
case "1":
if(a < met.length){
met[a] = getMet();
a++;
}
else
JOptionPane.showMessageDialog(null, "Cannot enter more values for Metric to Imperial");
break;
case "2":
if(b < imp.length){
b++;
}
else
JOptionPane.showMessageDialog(null, "Cannot enter more values for Imperial to Metric");
break;
case "Q":
quit = true;
break;
} //Switch end
} //Quit loop end
}
public static MetricToImperial getMet(){
//List of variables
String option;
MetricToImperial newMet = new MetricToImperial();
option = JOptionPane.showInputDialog("""
What computation:
1: Kilometer to Miles
2: Kilometer to Feet
3: Meter to Feet
4: Centimeters to Inch
5: Milimeters to Inch
6: Liters to Quarts
7: Liters to Gallons
8: Milliliters to Cups
9: Milliliters to Ounces
10: Kilogram to Tons
11: Kilogram to Pounds
12: Grams to Ounces
13: Grams to Pounds
14: Milligrams to Ounces
15: Celsius to Fahrenheit""");
switch(option){
case "1":
newMet.setKmMi();
newMet.getKmMi();
break;
case "2":
newMet.setKmFt();
break;
case "3":
newMet.setMFt();
break;
case "4":
newMet.setCmIn();
break;
case "5":
newMet.setMmIn();
break;
case "6":
newMet.setLQt();
break;
case "7":
newMet.setLGal();
break;
case "8":
newMet.setMlC();
break;
case "9":
newMet.setMlOz();
break;
case "10":
newMet.setKgT();
break;
case "11":
newMet.setKgLb();
break;
case "12":
newMet.setGOz();
break;
case "13":
newMet.setGLb();
break;
case "14":
newMet.setMlOz();
break;
case "15":
newMet.setCF();
break;
}
return newMet;
}
}
```
`
I have a switch statement which works fine, but I feel like it's poorly formatted and redundant(many of the cases do the same thing). How can I improve this?
switch (number) {
case 10: // do 1
break;
case 12: // do 2
break;
case 13: // do 1
break;
case 15: // do 3
break;
case 17: // do 2
break;
case 18: // do 2
break;
}
I guess you can group the cases with the same output together. It is one way to improve this.
switch (number) {
case 10:
case 13:
// do 1
break;
case 12:
case 17:
case 18:
// do 2
break;
case 15:
// do 3
break;
}
break; is used to terminate a case in the switch statement. So, the cases will have same output when there is no break; between them.
You could improve it by removing a few break statement, like this
switch (number) {
case 10:
case 13: // do 1
break;
case 12:
case 17:
case 18: // do 2
break;
case 15: // do 3
break;
}
You may make it a bit more compact like this:
switch (number) {
case 10:
case 13:
System.out.println("do 1 + " + number);
break;
case 12:
case 17:
case 18:
System.out.println("do 2 + " + number);
break;
case 15:
System.out.println("do 3 + " + number);
break;
}
And the last break; is not required in your case but good practice anyways for defensive programming.
You can add default case. When provided cases are not equal to number default statement is executed. Code:
int number = 0;
switch (number) {
case 10: // do 1
break;
case 12: // do 2
break;
case 13: // do 1
break;
case 15: // do 3
break;
case 17: // do 2
break;
case 18: // do 2
break;
default:
System.out.println(number);
}
I finished a program which is to make a number between 1 to 3999 and change it to roman numerals and I got it to work but I have to put this in two classes a main class and a tester class how would I do this? I know this may be a really simple question but I can't seem to figure it out how to split them into a main class and a tester class.
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
String numberString="";
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
System.out.println(number + " in Roman numerals is " + numberString);
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
As #Vikrant Kashyap pointed out. You can break this down to the test class and the conversion class. I didn't have a chance to compile the code. Let me know if this works or not.
RomanNumeralsTest.java
public class RomanNumeralsTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
RomanNumerals rn = new RomanNumerals();
System.out.println("Welcome to integer to Roman numeral conversion program ");
System.out.println("------------------------------------------------------ ");
System.out.print("Please enter an integer in the range 1-3999 (both inclusive): ");
int number= scan.nextInt();
if (number<=1||number >3999)
{
System.out.println("Sorry, the number is outside the range. Good bye!");
System.exit(0);
}
System.out.println(number + " in Roman numerals is " + rn.convertToRomanNumeral(number));
System.out.println("Thanks for using my program. Good bye!");
System.exit(0);
}
}
RomanNumerals.java
public class RomanNumerals
{
public String convertToRomanNumeral(int number)
{
String numberString = "";
switch ((number%10000)/1000)
{
case 1: numberString += "M";
break;
case 2: numberString += "MM";
break;
case 3: numberString += "MMM";
break;
}
switch ((number%1000)/100)
{
case 1: numberString += "C";
break;
case 2: numberString += "CC";
break;
case 3: numberString += "CCC";
break;
case 4: numberString += "CD";
break;
case 5: numberString += "D";
break;
case 6: numberString += "DC";
break;
case 7: numberString += "DCC";
break;
case 8: numberString += "DCCC";
break;
case 9: numberString += "CM";
break;
}
switch ((number%100)/10)
{
case 1: numberString += "X";
break;
case 2: numberString += "XX";
break;
case 3: numberString += "XXX";
break;
case 4: numberString += "XL";
break;
case 5: numberString += "L";
break;
case 6: numberString += "LX";
break;
case 7: numberString += "LXX";
break;
case 8: numberString += "LXXX";
break;
case 9: numberString += "XC";
break;
}
switch (number%10)
{
case 1: numberString += "I";
break;
case 2: numberString += "II";
break;
case 3: numberString += "III";
break;
case 4: numberString += "IV";
break;
case 5: numberString += "V";
break;
case 6: numberString += "VI";
break;
case 7: numberString += "VII";
break;
case 8: numberString += "VIII";
break;
case 9: numberString += "IX";
break;
}
return numberString;
}
}
After I run this codes, it says BUILD SUCCESSFUL and it stops. What should I do to make it repeat to ask another input with same case? I did read tutorial on several web but I'm not helped by those.
Here's the code:
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
I appreciate any help that you can provide.
I would try using a while loop to encase the switch. So it keeps asking your input.
import java.util.Scanner;
public class SwitchTry {
public static void main(String[] args) {
while(true){
int mth;
String mthString;
Scanner scanner = new Scanner(System.in);
mth =scanner.nextInt();
switch (mth) {
case 1: mthString = "January";
break;
case 2: mthString = "February";
break;
case 3: mthString = "March";
break;
case 4: mthString = "April";
break;
case 5: mthString = "May";
break;
case 6: mthString = "June";
break;
case 7: mthString = "July";
break;
case 8: mthString = "August";
break;
case 9: mthString = "September";
break;
case 10: mthString = "October";
break;
case 11: mthString = "November";
break;
case 12: mthString = "December";
break;
default: mthString = "Error";
break;
}
System.out.println(mthString);
}
}
}
As suggested by MadProgarmmer do-while loop is the perfect use-case for such scenarios. You need to wrap your input line plus switch cases and print statement inside the do-while loop as follow:
do {
//so you know that terminal is asking for input
System.out.println("Input: ");
mth =scanner.nextInt();
//copy/paste your switch case with last System.out.println() here
} while (true);
I am worried about having execution that never ends. Maybe you want to end the loop when the user supplies -1, so change the while(true) to while (mth != -1) and that should be your termination input.
I have a double split up into an int array, however the problem I am facing, is that I need it to fill in from the back, like this:
int[] arrayA = new int[5];
int[] arrayB = new int[5];
x = 23456.08;
//code here
//what im left with:
arrayA [0,2,3,4,5,6];
arrayB [0,8];
Heres how im cutting up the double:
public static void main(System args[])
{
Scanner input = new Scanner(System.in);
String answer = "";
int count = 0;
int thatone = 0;
int[] splitD = new int[5]; //main num
int[] splitDec = new int[1]; //decimal
//Enter the Number
System.out.print("Enter a number to convert: ");
double num = input.nextDouble();
// convert number to String
String convert = num + "";
// split the number
String[] split = convert.split("\\.");
String firstPart = split[0];
char[] charArray1 = firstPart.toCharArray();
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
// the decimal part
if (split.length > 1)
{
String secondPart = split[1];
char[] charArray2 = secondPart.toCharArray();
splitDec = new int[charArray2.length];
for (int i = 0; i < charArray2.length; i++)
{
// convert char to int
splitDec[i] = Character.getNumericValue(charArray2[i]);
}
}
for(int i =0; i<count;i++)
{
if(i ==0) // x00000.00 or 000x00.00
{
if(splitD[0] != "0")
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else
{
answer+= "";
}
}
else if(i ==1)//this goes with i =2 //0x0000
{
if(splitD[i] == 1)
{
switch (splitD[i+1])
{
case 9: answer+="Nineteen"; break;
case 8: answer+="Eighteen"; break;
case 7: answer+="Seventeen"; break;
case 6: answer+="Sixteen"; break;
case 5: answer+="Fifteen"; break;
case 4: answer+="Fourteen"; break;
case 3: answer+="Thirteen"; break;
case 2: answer+="Twelve"; break;
case 1: answer+="ten"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
thatone = 1;
}
else
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirty"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
}
}
else if(i == 2) //00x000
{
if(thatone ==0)
{
switch (splitD[i])
{
case 9: answer+=" Nine"; break;
case 8: answer+=" Eight"; break;
case 7: answer+=" Seven"; break;
case 6: answer+=" Six"; break;
case 5: answer+=" Five"; break;
case 4: answer+=" Four"; break;
case 3: answer+=" Three"; break;
case 2: answer+=" Two"; break;
case 1: answer+=" One"; break;
default: answer+=""; break;
}
answer+= " Thousand ";
}
else
{
}
}
else if(i ==3)
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
answer+= " Hundred ";
}
else if(i ==4) //0000x0
{
switch (splitD[i])
{
case 9: answer+="Ninety"; break;
case 8: answer+="Eighty"; break;
case 7: answer+="Seventy"; break;
case 6: answer+="Sixty"; break;
case 5: answer+="Fifty"; break;
case 4: answer+="Fourty"; break;
case 3: answer+="Thirdy"; break;
case 2: answer+="Twenty"; break;
case 1: answer+=""; break;
default: answer+=""; break;
}
answer+= " ";
}
else if(i ==5) //00000x
{
switch (splitD[i])
{
case 9: answer+="Nine"; break;
case 8: answer+="Eight"; break;
case 7: answer+="Seven"; break;
case 6: answer+="Six"; break;
case 5: answer+="Five"; break;
case 4: answer+="Four"; break;
case 3: answer+="Three"; break;
case 2: answer+="Two"; break;
case 1: answer+="One"; break;
default: answer+=""; break;
}
}
}
if(splitDec[0] == 0)
{
answer += " and 00/100 Dollars";
}
else if(splitDec[1] == 0)
{
answer += " and " +splitDec[0] + "0/100 Dollars";
}
else
{
answer += " and " +splitDec[0] +splitDec[1] +" /100 Dollars";
}
System.out.println(answer);
}
}
What should I do to make the array add 0 in the appropriate places?
such as if I typed in 54.00 I would get:
int[] SplitD = {0,0,0,0,5,4};
Thanks!
Instead of re-initializing the array:
// recreate the array with size equals firstPart length
splitD = new int[charArray1.length];
for (int i = 0; i < charArray1.length; i++)
{
// convert char to int
splitD[i] = Character.getNumericValue(charArray1[i]);
count++;
}
Use the existing (with length = 5), iterating from the back:
if(charArray1.length > 5) // heavy hard-code, would prefer something better
throw new IllegalArgumentException("Maximum 5 digits, on both decimal sides.");
// watch out of ArrayIndexOutOfBoundsException
for (int i = charArray1.length - 1, j = splitD.length -1 ;
i >=0 && j >=0; i--, j--)
{
// convert char to int
splitD[j] = Character.getNumericValue(charArray1[i]);
count++;
}
Recall, int arrays are filled with zeros, at initialization.
Think creating an array of String, one String for each character (including the decimal point), would be a better approach:
String[] chars = String.valueOf(num).split("(?<=.)");
Job done.
Edit:
To use a switch, don't even split:
for (byte b : String.valueOf(num).getBytes()) {
switch(b) {
case '.':
case '1':
}
}