![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
I have some code here that I am having a really difficult time figuring out what I have done or am still doing wrong. I am trying to type this code for my class, and the code is suppose to allow a user to enter a number 1-4 and say Congratulations you have entered "number" or you have entered an invalid number.
However, it is also suppose to allow the user to enter a letter a-e and give a similar response. This is what I have coded so far, I have learned to use a scanner console = new scanner(system.in); statement and I used this in another attempt to figure this out but I still am having a difficult time comparing the a-e statements and I don't know why. I have been speaking with my colleagues and they tell me that I should try this simple approach using string methods and scanner.next methods, but I have read in my book and don't understand what I am doing still. Unfortunatelt my school is online and I don't have anyone to verbally speak too about this. Here it is. // user to enter a number between 1-4 OR the letters a-e // import javax.swing.JOptionPane; public class Entering_A_Number { public static void main(String[] args) { int number; String numString; numString = JOptionPane.showInputDialog("Enter a number or letter:"); number = Integer.parseInt(numString); if (number < 5) System.out.println("Congratulations you entered " + number); if (number > 0) System.out.println("Congratulations you entered " + number); else System.out.println("Sorry you entered an invalid number " + number); if (number == 'a') || (number == b) || (number == c) || (number == d) || (number == e) System.out.println("Congratulations you entered " + number); else System.out.println("Sorry you entered an invalid letter " + number); System.exit(0); } } |
|
|
|
|
|
#2 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Java Entering a Number or Letter
Quote:
I have found that this is working for me, but now I am continuiing to get the error that I have entered an invalid key, this is my message, so I am happy that I am at least seeing it, but the problem is when I type the 1 in their it tells me its not a valid key when it should be??? Whats wrong with this? import java.util.*;
public class Entering_A_Number
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Please enter a number or a letter: ");
String number = console.next();
if (number == "1")
System.out.println("Congratulations you entered " + number);
else
System.out.println("Sorry you entered an invalid key " + number);
System.exit(0);
}
} |
|
|
|
|
|
|
#3 | ||
|
Not a user?
Join Date: Sep 2007
Posts: 295
Rep Power: 2
![]() |
Re: Java Entering a Number or Letter
Quote:
java Syntax (Toggle Plain Text)
Quote:
The first error is understandable, the second one you should have seen if you had only looked. Are you using notepad for your editor? I suggest downloading Jedit at least for creating your files, as it will help you find errors like this quicker. It highlights it like PFO did on what I posted here. Also, post code in the Code brackets using the # above the post editor to make it readable. |
||
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Quote:
Yeah, I'm sorry the first post was messed up, I didn't want to submit that file, I was careless. I am using the second posting of code which I will post again. This one seems to work well for me, but I keep receiving an error with either a number or letter, its like their is something wrong with my if statements. I don't know, have a look.... And thanks for replying. ![]() java Syntax (Toggle Plain Text)
|
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Java Entering a Number or Letter
The equal signs are suppose to be == instead of =
sorry. |
|
|
|
|
|
#6 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Re: Java Entering a Number or Letter
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);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); |
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Java Entering a Number or Letter
Quote:
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. |
|
|
|
|
|
|
#8 |
|
Expert Programmer
|
Re: Java Entering a Number or Letter
You need to use the equals method when comparing Strings in Java. Replace str == "1" with str.equals("1").
|
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Re: Java Entering a Number or Letter
Ah, shite... that's what you get for coding in C#, I guess. That one always catches me out.
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Java Entering a Number or Letter
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Programming with Java: Tutorial | ReggaetonKing | Java | 7 | May 20th, 2008 11:58 AM |
| Number in Java | RainMan | Java | 5 | Mar 31st, 2008 8:38 PM |
| Special browser in Java (Project) | stalefish | Java | 3 | Feb 9th, 2008 5:22 PM |
| Guess a Number | 3n! | C++ | 7 | Dec 2nd, 2007 4:38 AM |
| number or letter??? | hipolito | C++ | 15 | Feb 18th, 2005 10:40 AM |