Results 1 to 9 of 9
  1. #1
    jh7468 is offline Member
    Join Date
    Jan 2011
    Location
    Park Ridge, IL
    Posts
    10
    Rep Power
    0

    Default using a "char" sentinel value for "int" input

    If you're writing code that is asking for integer input, but need a sentinel value that is not an integer (i.e., "please enter a number or x to exit"), how would you do it?

    Thanks:o

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    All input from the terminal/gui is as a String. So, you could read the string and cast/parse to convert to a different data type.

  3. #3
    jh7468 is offline Member
    Join Date
    Jan 2011
    Location
    Park Ridge, IL
    Posts
    10
    Rep Power
    0

    Default

    Even from a command window?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yes.

    If you use a Scanner and the nextInt method, it just hides that fact that it reads a String and calls the Integer.parseInt method.

  5. #5
    jh7468 is offline Member
    Join Date
    Jan 2011
    Location
    Park Ridge, IL
    Posts
    10
    Rep Power
    0

    Default

    Here is the code -

    import java.util.Scanner;

    public class W1
    {
    public static void main(String[] args)
    {

    int number;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("enter a number or \"n\" to end");
    number = keyboard.nextInt();

    while (number != 'n')
    {
    System.out.println("hello");
    System.out.println("enter a number or 'n' to end");
    number = keyboard.nextInt();
    }

    System.out.println("bye");
    }
    }

    If I put in a number, the loop works fine. When I put in anything other than a number, it just crashes.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Because n is not an int so if you enter that the nextInt method will throw an exception. The advice given above may not have been clear. Get user input as a String. Check if that String is "n". If it isn't convert String to int.

  7. #7
    jh7468 is offline Member
    Join Date
    Jan 2011
    Location
    Park Ridge, IL
    Posts
    10
    Rep Power
    0

    Default Now, it just loops forever, even if you enter n

    import java.util.Scanner;

    public class W1
    {
    public static void main(String[] args)
    {

    String number;
    String input;
    Scanner keyboard = new Scanner(System.in);

    System.out.println("enter a number or \"n\" to end");
    number = keyboard.nextLine();

    while (number != "n")
    {
    System.out.println("hello");
    System.out.println("enter a number or 'n' to end");
    number = keyboard.nextLine();
    }

    System.out.println("bye");
    }
    }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Do not compare Strings or objects using == or !=, use the equals method instead.
    Java Code:
    if(str1.equals(str2))
    
    if( ! str1.equals(str2))

  9. #9
    jh7468 is offline Member
    Join Date
    Jan 2011
    Location
    Park Ridge, IL
    Posts
    10
    Rep Power
    0

    Default

    A million thanks. It works.

    One day, I hope to give back as much as I have received on this site.

    Thanks again.

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  2. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  3. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •