Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Summary - Scanner

The main use of java.util.Scanner is to read values from System.in or a file.

Many Scanner methods fit a simple pattern: nextXYZ() scans and returns a value of type XYZ. hasNextXYZ() returns true if something of type XYZ is available to be read next.

Additional types. The summary below just shows methods for ints and doubles because these are most common, but Scanner also supports BigDecimal, BigInteger, Float (returns float), Boolean (returns boolean), Long (returns long), Short (returns short), and Byte (returns byte). The XYZ in the prototypes below stands of for one of these additional types.

Scanner methods

Assume: int i, double d, String s, boolean b, Scanner sc, and x stands for a type that should be clear from context.

Constructors
sc = new Scanner(System.in); Creates a Scanner which reads from System.in.
sc = new Scanner(s); Creates a Scanner which reads from String s.
Most common "next" input methods.
s = sc.next() Returns next "token", which is more or less a "word".
s = sc.nextLine() Returns next integer value.
i = sc.nextInt() Returns next integer value.
d = sc.nextDouble() Returns next double value.
x = sc.nextXYZ() Returns value of type XYZ (primitive value if possible), where XYZ is one of BigDecimal, BigInteger, Boolean, Byte, Float, or Short.
Methods that test for availability of legal input for loops, optional elements, error checking.
b = sc.hasNext() True if another token is available to be read.
b = sc.hasNextLine() True if another line is available to be read.
b = sc.hasNextInt() True if another int is available to be read.
b = sc.hasNextDouble() True if another double is available to be read.
b = sc.hasNextXYZ() XYZ stands for one of the input types available above.
Numerical input methods other than decimal
x = sc.nextXYZ(radix) Returns next value where XYZ is one of Int, Long, Short, Byte, or BigInteger. Converts the input from base/radix specified in the parameter.
Other
sc.close() Closes the input stream. Many programs omit this if they are reading from the console, but it's a good habit to always close a Scanner when you're finished with all reading. Scanner is often used for file input. Even when you think you're reading the console, System.in may have been redirected to come from a file. Close the Scanner object so that the file is no longer locked for reading. This is even more important if you are reading many files, because each open file uses system resources, and closing the Scanner will release those resources.

And much more

Scanner has a lot of other features, with support for regular expressions, delimiter definitions, skipping input, locales, searching in a line, reading from inputs including String, ...

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.