Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 4th, 2008, 7:51 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Question 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);
}
}
kewlgeye is offline   Reply With Quote
Old Apr 4th, 2008, 11:13 PM   #2
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 kewlgeye View Post
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);
}
}

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);
	}
}
kewlgeye is offline   Reply With Quote
Old Apr 4th, 2008, 11:17 PM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 256
Rep Power: 2 Jabo is on a distinguished road
Re: Java Entering a Number or Letter

Quote:
Originally Posted by kewlgeye View Post
java Syntax (Toggle Plain Text)
  1. // user to enter a number between 1-4 OR the letters a-e //
  2. {
  3. if (number < 5)
  4. if (number > 0)
  5. }
This should be one statement using the AND operator to make sure it's both not below 1 and not above 4
java Syntax (Toggle Plain Text)
  1. if (number<5 && number >0)

Quote:
Originally Posted by kewlgeye View Post
java Syntax (Toggle Plain Text)
  1. {
  2. if (number == 'a') || (number == b) || (number == c) || (number == d) || (number == e)
  3. else
  4. }
Here you've put single quotes around a, but none of the others.

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.
Jabo is offline   Reply With Quote
Old Apr 5th, 2008, 12:20 AM   #4
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Question Re: Java Entering a Number or Letter

Quote:
Originally Posted by Jabo View Post
This should be one statement using the AND operator to make sure it's both not below 1 and not above 4
java Syntax (Toggle Plain Text)
  1. if (number<5 && number >0)



Here you've put single quotes around a, but none of the others.

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.

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)
  1. import java.util.*;
  2.  
  3. public class Entering_A_Number
  4. {
  5.  
  6. static Scanner console = new Scanner(System.in);
  7.  
  8. public static void main(String[] args)
  9. {
  10.  
  11. String number;
  12.  
  13. System.out.println("Please enter a number or a letter: ");
  14. number = console.nextLine();
  15.  
  16.  
  17.  
  18. if (number = "1")
  19. if (number = "2")
  20. if (number = "3")
  21. if (number = "4")
  22. if (number = "a")
  23. if (number = "b")
  24. System.out.println("Congratulations you entered " + number);
  25. else
  26. System.out.println("You have entered an invalid key " + number);
  27.  
  28.  
  29.  
  30. System.exit(0);
  31. }
  32. }
kewlgeye is offline   Reply With Quote
Old Apr 5th, 2008, 12:23 AM   #5
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 54
Rep Power: 1 kewlgeye is on a distinguished road
Re: Java Entering a Number or Letter

The equal signs are suppose to be == instead of =

sorry.
kewlgeye is offline   Reply With Quote
Old Apr 5th, 2008, 12:50 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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);
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);
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
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
Old Apr 5th, 2008, 1:53 AM   #8
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 843
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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").
titaniumdecoy is online now   Reply With Quote
Old Apr 5th, 2008, 10:47 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 5th, 2008, 3:55 PM   #10
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 titaniumdecoy View Post
You need to use the equals method when comparing Strings in Java. Replace str == "1" with str.equals("1").
You were right, that worked. Thank you all who have helped me along the way figuring this problem out, i have added to your reps.
kewlgeye is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming with Java: Tutorial ReggaetonKing Java 7 May 20th, 2008 10:58 AM
Number in Java RainMan Java 5 Mar 31st, 2008 7:38 PM
Special browser in Java (Project) stalefish Java 3 Feb 9th, 2008 4:22 PM
Guess a Number 3n! C++ 7 Dec 2nd, 2007 3:38 AM
number or letter??? hipolito C++ 15 Feb 18th, 2005 9:40 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC