Scanner input problem

S

slayer_azure

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. 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();
}
}
 
A

Andreas Leitgeb

slayer_azure said:
... 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.
int num=kbRead.nextInt();
fname=kbRead.nextLine();

When you entered the integer, you surely hit the Enter-key afterwards.
that line-end is not consumed through reading the integer, but it is
already entered by the time where you do the nextLine() call.

To verify this, enter:
42 Anonymous
in one line and press Enter afterwards.
It'll probably still not wait for a second return, but take
Anonymous as the name.

If you're sure, that you always press Enter directly after the
number, you can insert a (dummy) "kbRead.nextLine();" directly
after the nextInt(). Or you could check for an empty name, and
redo the nextLine() only in that case.
 
S

slayer_azure

When you entered the integer, you surely hit the Enter-key afterwards.
that line-end is not consumed through reading the integer, but it is
already entered by the time where you do the nextLine() call.

To verify this, enter:
  42 Anonymous
in one line and press Enter afterwards.
It'll probably still not wait for a second return, but take
Anonymous as the name.

If you're sure, that you always press Enter directly after the
number, you can insert a (dummy) "kbRead.nextLine();" directly
after the nextInt(). Or you could check for an empty name, and
redo the nextLine() only in that case.

Thanks it worked perfectly. Appreciate your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

My Status, Ciphertext 2
Help in hangman game 1
Void problem 1
Java matrix problem 3
Console Input Error-2 0
Scanner annoyances 11
School Project 1
Need Help: Program to Accept 2 Matrices and Show their Sum 0

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top