Here I have some problem when I use while loop in switch statement using dialog boxes. Some statements are unreachable and dialog boxes not appeared. Please help me! And also can do some correction on my code.
This the simple code that I made:
public static void main(String[] args)
{
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
switch (num)
{
//display result
default: JOptionPane.showMessageDialog(null,"fail"); break;
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
}
}
The cases in a switch/case are evaluated in the order you put them. default matches all cases. Since you have that first and that case does something before breaking out of it, the other cases will never be reached. Try this instead:
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
Your code does not show a while loop anywhere. Perhaps you can update with the code you attempted.
switch (num)
{
case 1:
while(!your condition)
{
JOptionPane.showMessageDialog(null,"c=a+b");
}
break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); break;
}
Retype the code:
// prompt and read first number from user
String no = JOptionPane.showInputDialog(null, "Enter the number");
int num = Integer.parseInt(no); //convert string to number
while (num<=4)
{
if
switch (num)
{
//display result
case 1: JOptionPane.showMessageDialog(null,"c=a+b"); break;
case 2: JOptionPane.showMessageDialog(null,"c=a/b"); break;
case 3: JOptionPane.showMessageDialog(null,"c=a*b"); break;
case 4: JOptionPane.showMessageDialog(null,"c=a-b"); break;
default: JOptionPane.showMessageDialog(null,"fail"); continue;
}
}// end method main
}// end class abc
Related
I am VERY new to programming...
What can I do to make the switch statement start over when the default case is promted (for example when you enter "5"). Any help would be great!
I saw a similar question about this, but I couldn't use the answers.
int column= StdOut.println();
switch(column) {
case 0: StdOut.println("Good");
break;
case 1: StdOut.println("Ok");
break;
case 2: StdOut.println("Bad");
break;
default:
break;
}
Surround it in a loop and add a variable that breaks the loop when you reach something you like:
int column= StdOut.println();
boolean isBad = true;
do{
switch(column) {
case 0: StdOut.println("Good");
isBad = false;
break;
case 1: StdOut.println("Ok");
isBad = false;
break;
case 2: StdOut.println("Bad");
isBad = false;
break;
default:
isBad = true;
break;
}
}while(isBad);
while(true) {
....
switch (...) {
case ....:
....
break; // Exit switch statement, but not the loop
// More cases here
default:
continue; // Go to the next iteration of the loop
}
break; // Exit the loop
}
System.out.println("Please make a selection between 1 and 3");
int choice = s.nextInt();
switch (choice)
{
case 3: System.out.println("Can a brother get some fries with that?");
case 2: System.out.println("Cheeseburger it is.... fatso");
case 1: System.out.println("Good choice, you could use a salad");
break;
default: System.out.println("Not a valid selection bruh");
}
switch (choice) {
case 3: System.out.println("Can a brother get some fries with that?"); break;
case 2: System.out.println("Cheeseburger it is.... fatso"); break;
case 1: System.out.println("Good choice, you could use a salad"); break;
default: System.out.println("Not a valid selection bruh"); break;
}
I literally just added the breaks after each case so it will exit your switch if it encounters either 1,2,3 or none of them.
You are missing the break statements...
You need a break; after each of the cases.
switch (choice)
{
case 3: System.out.println("Can a brother get some fries with that?");
break;
case 2: System.out.println("Cheeseburger it is.... fatso");
break;
case 1: System.out.println("Good choice, you could use a salad");
break;
default: System.out.println("Not a valid selection bruh");
break;
}
I'm trying to make a switch statement that prints out different messages based on whether the user inputs a 1, 2, or 3 but I keep getting errors. Just the small snippet of code below throws about a dozen errors which is probably some sort of record for me. :/
int menuSelection = keyboard.nextint;
switch
case 1: int menuSelection = "1";
break;
case 2: int menuSelection = "2";
break;
case 3: int menuSelection = "3";
break;
The error message is
"case, default, or '>' expected"
but I don't know what that means or how to fix it.
The syntax for your switch statement is incorrect. You need to switch on the variable and also add brackets to enclose your cases:
switch (menuSelection) {
case 1:
break;
case 2:
break;
case 3:
break;
}
Based on the below observations, the switch statement may look like this:
int menuSelection = keyboard.nextint;
String menuSelectionDisplay;
switch(menuSelection)
{
case 1:
menuSelectionDisplay = "1";
break;
case 2:
menuSelectionDisplay = "2";
break;
case 3:
menuSelectionDisplay = "3";
break;
default:
menuSelectionDisplay = "?";
break;
}
Switch statements are required to be surrounded by curly braces {}
It's considered a good idea to have a "default" clause within your switch statement. It acts as a catch-all that gets executed when none of the cases match. In the above example, if the value of menuSelection happened to be 4, the default clause would be executed.
The switch statement takes in one parameter, which is used to determine which case to run. Think of it as a series of if-else statements.
int value = 5;
switch(value)
{
case 5: System.out.println("Hello!"); break;
case 6: System.out.println("Bye!"); break;
default: System.out.println("Huh?"); break;
}
is equivalent to this:
if(value === 5)
{
System.out.println("Hello!");
}
else if(value === 6)
{
System.out.println("Bye!");
}
else
{
System.out.println("Huh?");
}
In your cases, you appear to be setting the value of an int to a String. That's invalid; you would need to either
Set the value of a String to be a String (String s = "1";), or
Set the value of an int to be an int (int i = 1;).
Finally, you cannot re-declare a variable's type multiple times within the code (that's having int menuSelection = keyboard.nextint; and int menuSelection = 3; in the same scope)
int menuSelection = keyboard.nextint;
switch (menuSelection) {
case 1:
menuSelection = "1";
break;
case 2:
menuSelection = "2";
break;
case 3:
menuSelection = "3";
break;
default:
break;
}
How is it possible that the output is 1002,why is the last case being executed despite having a mismatch?
public class Main {
public static void main(String[] args) {
int i=0,j=0;
switch (i) {
case 2 : j++;
default: j+=2;
case 15 : j+=1000;
}
System.out.println("j="+j);
}
}
FALLTHROUGH:
Another point of interest is the break statement. Each break statement
terminates the enclosing switch statement. Control flow continues with
the first statement following the switch block. The break statements
are necessary because without them, statements in switch blocks fall
through: All statements after the matching case label are executed in
sequence, regardless of the expression of subsequent case labels,
until a break statement is encountered.
Your code should be:
case 2 : j++; break;
case 4: j+=10; break;
default: j+=2; break;
case 15: j+=1000;
}
FROM DOCS
public class Example{
public static void main(String[] args) {
java.util.ArrayList<String> futureMonths =
new java.util.ArrayList<String>();
int month = 8;
switch (month) {
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
default: break;
}
if (futureMonths.isEmpty()) {
System.out.println("Invalid month number");
} else {
for (String monthName : futureMonths) {
System.out.println(monthName);
}
}
}
}
This is the output from the code:
August
September
October
November
December
You have to break at the end of the case blocks. Otherwise all subsequent cases will be also executed.
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
int i=0,j=0;
switch (i){
case 2 : j++; break;
case 4: j+=10; break;
case 15 : j+=1000; break;
default: j+=2;
}
System.out.println("j="+j);
}
}
Because you are missing break;
and if I understand your confusion, The order of default doesn't matter. In below case,
int i=15,j=0;
switch (i){
case 2 :
j++;
break;
case 4:
j+=10;
break;
default:
j+=2;
break;
case 15 :
j+=1000;
break;
}
j will be having value 1000 even if default was before case 15
you don't have the 'break' keyword specified in each of your cases.
Should be like this:
switch (i){
case 2 :
j++;
break;
case 4:
j+=10;
break;
case 15 :
j+=1000;
break;
default:
j+=2;
break;
}
In this case case 2 and case 4 are not execute but default and case 15 are so the answer is 1002. Please put break statement for desired result.
Hope this helps.
I only need help with the input part. If the user inputs a number I need the program to read and output a case that equals the number that was input.
//This program will display the months of the year
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
int month;
System.out.println("Please enter a Month Number: ");
month = (int) System.in.read(); //Get an integer
switch (month) {
case 1: System.out.println("January");
break;
case 2: System.out.println("February");
break;
case 3: System.out.println("March");
break;
case 4: System.out.println("April");
break;
case 5: System.out.println("May");
break;
case 6: System.out.println("June");
break;
case 7: System.out.println("July");
break;
case 8: System.out.println("August");
break;
case 9: System.out.println("September");
break;
case 10: System.out.println("October");
break;
case 11: System.out.println("November");
break;
case 12: System.out.println("December");
break;
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}
Try using the Console class instead: http://docs.oracle.com/javase/6/docs/api/java/io/Console.html to obtain user input, then convert the String to a number with Integer.parseInt(String)
Also, be aware that the next version of Java (8) will support Strings in case blocks.
class vehicle
{
int passengers;
int fuelcap;
int mpg;
}
import java.io.*;
class Vehicle_Demo
{
public static void main (String args[]) throws java.io.IOException
{
vehicle obj1 = new vehicle();
obj1.passengers=12;
obj1.fuelcap=9;
obj1.mpg=78;
System.out.println(obj1.passengers, obj1.fuelcap, obj1.mpg);
}
}
You can use Scanner to read your System.in
Scanner input = new Scanner(System.in);
month = input.nextInt();
Output:
Please enter a Month Number:
1
January
See related
Edit:
as noahz pointed out there is a Console class that covers the same functionality. For an idea of the difference between the two, read this.
Try to use TextIO Input Functions
This is better than scanner input
You need to compile TextIO.java fist as a prerequisite so that TextIO.class must be found in the same folder.
use this input function instead:
month = TextIO.getChar();
Please let me know if you have problems on this method.
Thanks!
Correct me if I'm wrong, but I'm pretty sure it will still work with "system.in.read". You're switch cases do not have single quotes around the input you want to be processed. If you want case one to be run when you type '1' your case should be case '1', not case 1.
There problem here was Java have cast a character to an integer, therefore you get an ASCII value.
For instance, when you type 5 you've got 53 in ASCII. Check it taking the variable value in System.out.println();
I've tried to solve that changing month to a char type and then passing a char value to the switch control. But the problem is, getting System.in.read(); method you just can get a single character. At last you just can select between 1 to 9.
I suspect the solution is using a buffer reader. Until then, I leave you here my code:
public class MonthsOfTheYear {
public static void main(String[] args)
throws java.io.IOException{
char month;
System.out.println("Please enter a Month Number: ");
month = (char) System.in.read(); //Get an integer
//System.out.println("Actual value of :" + month);
switch (month) {
case '1': System.out.println("January");
break;
case '2': System.out.println("February");
break;
case '3': System.out.println("March");
break;
case '4': System.out.println("April");
break;
case '5': System.out.println("May");
break;
case '6': System.out.println("June");
break;
case '7': System.out.println("July");
break;
case '8': System.out.println("August");
break;
case '9': System.out.println("September");
break;
/*case '10': System.out.println("October");
break;
case '11': System.out.println("November");
break;
case '12': System.out.println("December");
break;*/
default: System.out.println("Invalid Month");
break;
}
System.out.println();
}
}