View Poll Results: Which class do you use to input data?

Voters
0. You may not vote on this poll
  • Scanner

    0 0%
  • BufferedReader

    0 0%
  • TextIO

    0 0%
  • JOptionPane.showInputDialog() and then convert...

    0 0%
  • Other

    0 0%
Results 1 to 4 of 4
  1. #1
    slayer_azure is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Question Scanner input problem

    This is a simple program that I have written in Grade 12 Information Technology, and we were instructed to do the following, which is to ask the user to enter an integer, then call a method and asked a user to enter their full name. Input is done using the methods from the Scanner class of the java.util.* package. When I run the program, the first part runs correctly and I enter a number but after that it calls the name() method and prints "Enter FULL name: " but then the program ends without giving me the chance to enter a name. If I use kbRead.next() instead it works with only one name but I need to use nextLine() to enter a name and surname. What am I doing wrong? Help will be appreciated.

    import java.util.*;
    public class test
    {
    static Scanner kbRead=new Scanner(System.in);
    static String fname;
    public static void main (String[]args)
    {
    System.out.println("Enter a number: ");
    int num=kbRead.nextInt();
    name();

    }
    public static void name()
    {
    System.out.print("Enter FULL name:");
    fname=kbRead.nextLine();
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Try
    Java Code:
            int num=Integer.parseInt(kbRead.nextLine());

  3. #3
    slayer_azure is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by hardwired View Post
    Try
    Java Code:
            int num=Integer.parseInt(kbRead.nextLine());
    Thanks, it works now. Could you please explain why it was doing that originally?

  4. #4
    Breadwinna is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    Because after the nextInt() method pulls the value from the keyboard buffer, it leaves the newline character that was entered. So when you use the nextLine() method next, it reads in the newline character and stops like its suppose to.

    Unlike other Scanner methods, nextLine() doesn't ignore the first newline character. You could just write a statement like this:

    Java Code:
    kbRead.nextLine();
    to consume the newline character and then proceed to read in the user's string

Similar Threads

  1. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 PM
  2. MixedTypeInput example (Scanner)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-18-2008, 02:11 PM
  3. help with IP scanner
    By tommy in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 08:00 PM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM
  5. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:02 AM

Posting Permissions

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