![]() |
Java Entering a Number or Letter
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); } } |
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.*; |
Re: Java Entering a Number or Letter
Quote:
:
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. |
Re: Java Entering a Number or Letter
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. :) :
|
Re: Java Entering a Number or Letter
The equal signs are suppose to be == instead of =
sorry. |
Re: Java Entering a Number or Letter
Let's format that the way Java sees it (after we fix the = problem):
:
if (number == "1")Try this: :
if (number == "1" || number == "2" || number == "3" || |
Re: Java Entering a Number or Letter
1 Attachment(s)
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. |
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").
|
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.
|
Re: Java Entering a Number or Letter
Quote:
|
| All times are GMT -5. The time now is 4:18 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC