Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Java Entering a Number or Letter (http://www.programmingforums.org/showthread.php?t=15553)

kewlgeye Apr 4th, 2008 8:51 PM

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 Apr 5th, 2008 12:13 AM

Re: Java Entering a Number or Letter
 
Quote:

Originally Posted by kewlgeye (Post 143493)
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);
        }
}


Jabo Apr 5th, 2008 12:17 AM

Re: Java Entering a Number or Letter
 
Quote:

Originally Posted by kewlgeye (Post 143493)
:

  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
:

  1.     if (number<5 && number >0)


Quote:

Originally Posted by kewlgeye (Post 143493)
:

  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.

kewlgeye Apr 5th, 2008 1:20 AM

Re: Java Entering a Number or Letter
 
Quote:

Originally Posted by Jabo (Post 143498)
This should be one statement using the AND operator to make sure it's both not below 1 and not above 4
:

  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. :)


:

  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 Apr 5th, 2008 1:23 AM

Re: Java Entering a Number or Letter
 
The equal signs are suppose to be == instead of =

sorry.

Ooble Apr 5th, 2008 1:50 AM

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);


kewlgeye Apr 5th, 2008 2:28 AM

Re: Java Entering a Number or Letter
 
1 Attachment(s)
Quote:

Originally Posted by Ooble (Post 143502)
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.

titaniumdecoy Apr 5th, 2008 2:53 AM

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").

Ooble Apr 5th, 2008 11:47 AM

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.

kewlgeye Apr 5th, 2008 4:55 PM

Re: Java Entering a Number or Letter
 
Quote:

Originally Posted by titaniumdecoy (Post 143504)
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.


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