• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner related question of a newbie  RSS feed

 
LathaPanduranga Rao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have just started learning java.
I am trying to pass firstname and lastname thru Scanner(System.in) method and display the values. But I am getting some errors. I can pass only the firstname and successfully compile and run. But when I want to pass first and last name I am not able to do so. Is there any other way to read both names from the command line using Scanner?
Please help me....
Regards,
Latha
Here is the code snippet:

import java.util.Scanner;

public class ScanTest
{

public static void main(String[] args) {

System.out.println ("Please enter Your Firstname and Lastname: ");

Scanner scan = new Scanner(System.in);
while (scan.hasNext())
{
String fname = scan.next();
String lname = scan.next();
}


System.out.println ("Welcome " + fname + lname);


}

}
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't put it in a loop.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!