View Single Post
Old Apr 5th, 2008, 1:28 AM   #7
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Re: Java Entering a Number or Letter

Quote:
Originally Posted by Ooble View Post
Let's format that the way Java sees it (after we fix the = problem):
if (number == "1")
	if (number == "2")
		if (number == "3")
			if (number == "4")
				if (number == "a")
					if (number == "b")
						System.out.println ("Congratulations you entered " + number);
else
	System.out.println("You have entered an invalid key " + number);
Understand why it's broken now? You're checking to see whether number == "1", and then checking to see whether number == "2"... obviously both of these can't be true, so it fails.

Try this:
if (number == "1" || number == "2" || number == "3" ||
    number == "4" || number == "a" || number == "b")
	System.out.println("Congratulations you entered " + number);
else
	System.out.println("You have entered an invalid key " + number);

Thank you very much for your help, but unfortunately this didn't work, I tried it with your code and this is the error. You can see it in the attached pic.
Attached Images
File Type: jpg errorjavaenteringanumber.jpg (38.2 KB, 3 views)
kewlgeye is offline   Reply With Quote