Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

View Poll Results: Which class do you use to input data?
Scanner 0 0%
BufferedReader 0 0%
TextIO 0 0%
JOptionPane.showInputDialog() and then convert... 0 0%
Other 0 0%
Voters: 0. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-25-2008, 10:59 PM
Member
 
Join Date: May 2008
Posts: 2
slayer_azure is on a distinguished road
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();
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-25-2008, 11:15 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
Try
Code:
int num=Integer.parseInt(kbRead.nextLine());
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-26-2008, 09:10 PM
Member
 
Join Date: May 2008
Posts: 2
slayer_azure is on a distinguished road
Quote:
Originally Posted by hardwired View Post
Try
Code:
int num=Integer.parseInt(kbRead.nextLine());
Thanks, it works now. Could you please explain why it was doing that originally?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-27-2008, 12:49 AM
Member
 
Join Date: May 2008
Posts: 2
Breadwinna is on a distinguished road
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:

Code:
kbRead.nextLine();
to consume the newline character and then proceed to read in the user's string
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with scanner whiterex New To Java 1 04-22-2008 03:41 PM
MixedTypeInput example (Scanner) Java Tip Java Tips 0 01-18-2008 04:11 PM
help with IP scanner tommy New To Java 1 08-06-2007 10:00 PM
how to take input and verify input in Java programs bilal_ali_java Advanced Java 0 07-21-2007 10:46 AM
problem with scanner class:incompatible types fred New To Java 1 07-20-2007 09:02 AM


All times are GMT +3. The time now is 05:12 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org