java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.util arrow How to read input from console
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4092
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to read input from console E-mail
User Rating: / 1101
PoorBest 

JDK 5.0 provides a feature to read input from console. Java.util,Scanner is used for this purpose. This code read a String and an Integer from the console and stores them in the variables.

import java.util.Scanner;

public class InputExp {

   public static void main(String[] args) {

       String name;
       int age;
       Scanner in = new Scanner(System.in);

       // Reads a single line from the console 
       // and stores into name variable
       name = in.nextLine();

       // Reads a integer from the console
       // and stores into age variable
       age=in.nextInt();
       in.close();            

       // Prints name and age to the console
       System.out.println("Name :"+name);
       System.out.println("Age :"+age);

    }
}

 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.