7 Replies - 2363 Views - Last Post: 21 December 2009 - 02:21 PM Rate Topic: -----

#1 dimdim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Java Menu Panel

Posted 20 December 2009 - 04:24 PM

Hi everbody,
i am trying create a menu with JOptionPane,and when the user enters wrong input to display an invalid message withount exiting.II have tried using while loops but sth i am doing wrong...Code is following:
import javax.swing.JOptionPane;
public class menu
{
public static void main (String args[])
{

String userWord= JOptionPane.showInputDialog("N-New game\nE-Exit game");

while (userWord.equalsIgnoreCase("e"))
{
if (userWord.equals("n"))
{
JOptionPane.showMessageDialog(null,"new game","newT game",JOptionPane.OK_OPTION);break;

}

else
{
userWord= JOptionPane.showInputDialog("N-New game\nE-Exit game");

}
JOptionPane.showInputDialog("N-New game\nE-Exit game");






}
System.exit(0);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Java Menu Panel

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Java Menu Panel

Posted 20 December 2009 - 04:38 PM

Please, :code:

Now in terms of your problem, I'm guessing valid inputs are "E" and "N" only, ignore case. So I would use an algorithm like:
String input <-- get input

while(input is not equal to "n" ignore case AND input is not equal to "e" ignore case) do
	display "invalid input. please enter new input"
	input <-- get input
end while




It looks like you are familiar with the syntax, so I'll leave it to you to implement. Good luck! :)

This post has been edited by macosxnerd101: 20 December 2009 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#3 dimdim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Re: Java Menu Panel

Posted 20 December 2009 - 05:57 PM

Thank you for your quick reply.Well i managed to do it like this:
import javax.swing.JOptionPane;
public class demo
{
public static void main(String args[])
{

String option=JOptionPane.showInputDialog("1----one player\n2----two player\npress -9 to exit");
int o=Integer.parseInt(option);
while(o!= -9)
{
if (o==1)
{
JOptionPane.showMessageDialog(null,"1p game","1p",JOptionPane.INFORMATION_MESSAGE);break;

}
else if (o==2)
{
JOptionPane.showMessageDialog(null,"2p game","2p",JOptionPane.INFORMATION_MESSAGE);break;
}
else
{
option=JOptionPane.showInputDialog("1----one player\n2----two player");
}

}

}







}
The only problem now is that when i enter a string it exits.I want to whenever asth different than the available options is entered to display again the input dialog.thats where i am stuck now.
Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Java Menu Panel

Posted 20 December 2009 - 06:05 PM

View Postmacosxnerd101, on 20 Dec, 2009 - 07:38 PM, said:

Please, :code:


In case you missed it the first time when I asked you to use code tags, please read above. Rule #4 of this forum states to use BBCode when appropriate.


String input <-- get input

while(input is not equal to "n" ignore case AND input is not equal to "e" ignore case) do
	display "invalid input. please enter new input"
	input <-- get input
end while



Now in terms of your code, take a closer look at my algorithm. Nowhere in the loop do I do anything more than get input. Notice how for the loop condition I validate to make sure the input is invalid before prompting for input again. It is after the loop that you want to execute the code corresponding the the option the user selects.
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Menu Panel

Posted 20 December 2009 - 06:17 PM

Also I would just like to add, 'o' is probably the worst possible name for a variable. o, O, and 0 can look very similar if you are not absolutely sure.

If you must use single character variable names, make it something distinct, such as a, b, or y.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Java Menu Panel

Posted 20 December 2009 - 06:20 PM

View Posterik.price, on 20 Dec, 2009 - 09:17 PM, said:

Also I would just like to add, 'o' is probably the worst possible name for a variable. o, O, and 0 can look very similar if you are not absolutely sure.

If you must use single character variable names, make it something distinct, such as a, b, or y.


+1. A more descriptive name for your variable might be optionInt, useOption, compareOption. You could also name the String for the JOptionPane input, and just name the parsedInt optionselected or just selected. In this way, your code is easier to understand because the variable names describe their functions or purposes in the program.
Was This Post Helpful? 0
  • +
  • -

#7 ralph_komer  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 71
  • Joined: 12-May 09

Re: Java Menu Panel

Posted 21 December 2009 - 08:39 AM

I hope the below code will help. But you must change your variable names as suggested by other members.

import javax.swing.JOptionPane;
public class demo
{
	public static void main(String args[])
	{
		String option = "";
		int o = 0;
		do
		{
			option=JOptionPane.showInputDialog("1----one player\n2----two player\npress -9 to exit");
			System.out.println("Option selected: " + option);
			try
			{
				o=Integer.parseInt(option);
			}catch(NumberFormatException nfe)
			{
			}
			switch(o)
			{
				case 1:
					JOptionPane.showMessageDialog(null,"1p game","1p",JOptionPane.INFORMATION_MESSAGE);
					o = 0;
					break;
				case 2:
					JOptionPane.showMessageDialog(null,"2p game","2p",JOptionPane.INFORMATION_MESSAGE);
					o = 0;
					break;
				case -9:
					System.exit(0);
			}
		}while(o!= -9);
	}
}


Was This Post Helpful? 0
  • +
  • -

#8 dimdim  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 09

Re: Java Menu Panel

Posted 21 December 2009 - 02:21 PM

Thank you everyone for your help,i have completely understood it now.You are right it is mych better if i change name variables.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1