Jump to content

Java: Switch Case + JOptionPane.showMessageDialog output

Hey guys,

 

I have the following problem, I'd like to create a program where I input a number from 1 to 12. Each number is the number of a month. 

When I type in 3 (march), the output should be "this month is from the 1 quarter of the year". 

And input 6 = 2 quarter of the year, etc.

 

I want the output with JOptionPane.showMessageDialog.

 

This code is working already:

public class numbers
{
	
	public static void main(String[] args)
	{
		/*
		
		String input, output;
		int inputNumber;
		
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		
	
		switch(inputNumber)
		{
			case 1:
			case 2:
			case 3: JOptionPane.showMessageDialog(null, "Q: 1");
				break;
			case 4:
			case 5:
			case 6: JOptionPane.showMessageDialog(null, "Q: 2");
				break;
			case 7:
			case 8:
			case 9: JOptionPane.showMessageDialog(null, "Q: 3");
				break;
			case 10:
			case 11:
			case 12: JOptionPane.showMessageDialog(null, "Q: 4");
				break;
			
			
			default: JOptionPane.showMessageDialog(null, "wrong number(1-12)!");
		}

 

But now I want to make this code also running:

String input, output, quarter;
		int inputNumber;
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
		
		switch(inputNumber)
		{
			case 1: 
			case 2: 
			case 3: quarter = "1";
				break;
			case 4:
			case 5:
			case 6: quarter = "2";
				break;
			case 7:
			case 8:
			case 9: quarter ="3";
				break;
			case 10:
			case 11:
			case 12: quarter = "4";
				break;
				
			default: output = "wrong input!";
		}
		
		

I know there are some wrong lines already so please help me in this second code.

I would have an output line that just takes the number of the quarter and fills the gap in the output.

 

Thank you in advance!!

Link to comment
Share on other sites

Link to post
Share on other sites

I think this line:

JOptionPane.showMessageDialog(null, "Quarter: " + quarter);

Should be after the switch block.

Like this:

		String input, output, quarter;
		int inputNumber;
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		
		switch(inputNumber)
		{
			case 1: 
			case 2: 
			case 3: quarter = "1";
				break;
			case 4:
			case 5:
			case 6: quarter = "2";
				break;
			case 7:
			case 8:
			case 9: quarter ="3";
				break;
			case 10:
			case 11:
			case 12: quarter = "4";
				break;
				
			default: output = "wrong input!";
		}
        
        JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
		

Because else quarter won't have a value at the point where you show the dialog.

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, MrMG said:

I think this line:


JOptionPane.showMessageDialog(null, "Quarter: " + quarter);

Should be after the switch block.

Like this:


		String input, output, quarter;
		int inputNumber;
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		
		switch(inputNumber)
		{
			case 1: 
			case 2: 
			case 3: quarter = "1";
				break;
			case 4:
			case 5:
			case 6: quarter = "2";
				break;
			case 7:
			case 8:
			case 9: quarter ="3";
				break;
			case 10:
			case 11:
			case 12: quarter = "4";
				break;
				
			default: output = "wrong input!";
		}
        
        JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
		

Because else quarter won't have a value at the point where you show the dialog.

error: variable quarter might not have been initialized

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that your default case in the switch block does not assign anything to quarter, but instead stores "wrong input!" in output. You either have to assign something to quarter in the default case or you do something like this:

String input, output, quarter;
		int inputNumber;
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		quarter = "";
		switch(inputNumber)
		{
			case 1: 
			case 2: 
			case 3: quarter = "1";
				break;
			case 4:
			case 5:
			case 6: quarter = "2";
				break;
			case 7:
			case 8:
			case 9: quarter ="3";
				break;
			case 10:
			case 11:
			case 12: quarter = "4";
				break;
				
			default: output = "wrong input!";
		}
        
        JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
		

Else what could happen is that the user puts in a number bigger than 12 and your program ends up trying to show a dialog with the value of quarter, but quarter never got a value assigned.

Link to comment
Share on other sites

Link to post
Share on other sites

String input, output, quarter;
		int inputNumber;
		
		input = JOptionPane.showInputDialog(null, "put in a number (1-12).");
		inputNumber = Integer.parseInt(input);
		quarter = "";
        
 
        
		switch(inputNumber)
		{
			case 1: 
			case 2: 
			case 3: quarter = "1";
				break;
			case 4:
			case 5:
			case 6: quarter = "2";
				break;
			case 7:
			case 8:
			case 9: quarter ="3";
				break;
			case 10:
			case 11:
			case 12: quarter = "4";
				break;
				
			default: JOptionPane.showMessageDialog(null,"wrong input!");
		}
        

 

Link to comment
Share on other sites

Link to post
Share on other sites

If the user does not enter a number between 1 and 12, then what will happen is that the default case in your switch block will run, and the other cases won't.

That means that the text stored in quarter is just "". Next a message dialog gets created, but quarter still has only "" stored. That's why you get "Quarter: " as the text of that dialog. You could try the following:

if(!quarter.equals("")){
	JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
}

This should make the message dialog only pop up if the text in quarter is not "" .

And FYI whenever you compare strings always use string1.equals(string2). The == operator will not work with Strings.

 

And also:

if(inputNumber < 1 || > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

Are you sure it works? I feel like it should look like this:

if(inputNumber < 1 || inputNumber > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

16 minutes ago, MrMG said:

If the user does not enter a number between 1 and 12, then what will happen is that the default case in your switch block will run, and the other cases won't.

That means that the text stored in quarter is just "". Next a message dialog gets created, but quarter still has only "" stored. That's why you get "Quarter: " as the text of that dialog. You could try the following:


if(!quarter.equals("")){
	JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
}

This should make the message dialog only pop up if the text in quarter is not "" .

And FYI whenever you compare strings always use string1.equals(string2). The == operator will not work with Strings.

 

 

Quote

And also:


if(inputNumber < 1 || > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

Are you sure it works? I feel like it should look like this:


if(inputNumber < 1 || inputNumber > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

 

Have You changed anything here? I have it the same way you have it, sorry I forgot to write it down. It works for numbers at least, but If I type in something like "abc" I don't get any window pop up at all.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, I changed the "|| > 12" to "|| inputNumber > 12" .

 

The reason you are not getting any pop up's when you write "abc" is because of this line:

inputNumber = Integer.parseInt(input);

If input is not a number but an actual text, then parseInt function will throw an exception which will make your program stop prematurely. If you don't want that, then you could try to work with try-catch blocks.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, MrMG said:

Yes, I changed the "|| > 12" to "|| inputNumber > 12" .

 

The reason you are not getting any pop up's when you write "abc" is because of this line:


inputNumber = Integer.parseInt(input);

If input is not a number but an actual text, then parseInt function will throw an exception which will make your program stop prematurely. If you don't want that, then you could try to work with try-catch blocks.

Let's keep it simple for now, because I don't know anything about try catch blocks yet. ?  

Is there any way to put the 

if(inputNumber < 1 || inputNumber > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

somehow into the default? 

Isn't it redundant to have both?

Link to comment
Share on other sites

Link to post
Share on other sites

Quote

Is there any way to put the 


if(inputNumber < 1 || inputNumber > 12)
        {
        	JOptionPane.showMessageDialog(null, "Wrong input!");
        }

somehow into the default? 

Isn't it redundant to have both?

Sure. You could do the following:

switch(inputNumber)
        {
            case 1:
            case 2:
            case 3: quarter = "1";
                break;
            case 4:
            case 5:
            case 6: quarter = "2";
                break;
            case 7:
            case 8:
            case 9: quarter ="3";
                break;
            case 10:
            case 11:
            case 12: quarter = "4";
                break;

            default: JOptionPane.showMessageDialog(null, "Wrong input!");
        }

 

Link to comment
Share on other sites

Link to post
Share on other sites

if(inputNumber >= 1 || inputNumber <= 12)
{
	JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
}

 

I found the solution:

 

if(inputNumber >= 1 && inputNumber <= 12)
{
    JOptionPane.showMessageDialog(null, "Quarter: " + quarter);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×