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 Notes

Console Input: Scanner

The java.util.Scanner class (added in Java 5) allows simple console and file input. Of course, your program should eventually have a GUI user interface, but Scanner is very useful for reading data files.

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
// File   : introductory/IntroScanner.java
// Purpose: Write to and read from the console.
// Author : Michael Maus
// Date   : 2006-01-20

import java.util.*;                //Note 1

public class IntroScanner {

    public static void main(String[] args) {
        //... Initialization
        String name;               // Declare a variable to hold the name.
        Scanner in = new Scanner(System.in);

        //... Prompt and read input.
        System.out.println("What's your name, Earthling?");
        name = in.nextLine();      // Read one line from the console.
        in.close();                //Note 2

        //... Display output
        System.out.println("Take me to your leader, " + name);
    }
}

Notes

  1. Altho we only need the Scanner class from the java.util package, the most common programming style is to make all classes (*) visible.
  2. Closing the console isn't really necessary, but it's a good habit. If we had been reading a file, which is common with Scanner, closing it would be important.

Example using Scanner in a loop

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
// File   : introductory/ScannerLoop.java
// Purpose: Read from the console in a loop using Scanner.
// Author : Michael Maus
// Date   : 2006-01-20

import java.util.*;

public class ScannerLoop {

    public static void main(String[] args) {
        //... Initialization
        double n;                // Holds the next input number.
        double sum = 0;          // Sum of all input numbers.
        Scanner in = new Scanner(System.in);

        //... Prompt and read input in a loop.
        System.out.println("Will add numbers.  Non-number stops input.");
        
        while (in.hasNextDouble()) {
            n = in.nextDouble();
            sum = sum + n;
        }
        in.close(); 

        //... Display output
        System.out.println("The total is " + sum);
    }
}
Facing Programming Problem?
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:

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.

  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.