Help with the Scanner class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help with the Scanner class

  1. #1
    Join Date
    Jul 2006
    Posts
    3

    Help with the Scanner class

    This is my simple Java program:

    -----------------------------------------------------------------
    import java.util.*;

    public class Person{
    public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    sc.useDelimiter("\r");
    String name;
    int age;

    System.out.println("Enter a person's name: ");
    name = sc.next();
    System.out.println("Enter a person's age: ");
    age = sc.nextInt();

    System.out.println("Name: " + name + "\nAge: " + age);
    }
    }

    ------------------------------------------------------

    Now, after I enter an integer for the age I get an InputMismatchException and it says the error is on Line 13 [ age=sc.nextInt(); ]
    Can someone explain to me why this happens and how do I correct it?

    I've tried changing the data type of age to String and it works just fine.
    I also tried removing the sc.useDelimiter("\r") line and it works fine but of course I cannot enter names like "Jane Smith". Does this mean the Scanner cannot recognize the integer I entered or took the "\r" as my data?

    By the way, I'm pretty sure the problem is with the Scanner class but if I'm wrong, please correct me.

  2. #2
    Join Date
    Jul 2006
    Posts
    1
    The problem is that the 'next' method returns the next whitespace surrounded token, i.e. if you enter "Joe Bob" it only returns "Joe". The 'nextInt' method thereafter tries to intepret "Bob" as an integer.

    You could do something like this:
    Code:
            System.out.println("Enter a person's name: ");
            name = sc.next();
            if (!sc.hasNextInt()) {
                name += " " + sc.next();
            }
            System.out.println(name + " enter your age: ");
            age = sc.nextInt();
    Easier and more robust would be just to read a complete line as the name:
    Code:
           
           System.out.println("Enter a person's name: ");
           name = sc.nextLine();
           System.out.println(name + " enter your age: ");
           age = sc.nextInt();
    Cheers,
    Martin.

  3. #3
    Join Date
    Jul 2006
    Posts
    3
    Ah, now I understand! Thanks!

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  3. Replies: 5
    Last Post: 10-31-2005, 01:50 AM
  4. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  5. Replies: 5
    Last Post: 10-17-2002, 01:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center