Powered by Blogger.

Java Scan for JavaScript use Scanner - Java Solution

Scanner:-

The scanner class provides formated input functionality. It is a part of the java.util package.

Example for Scanner:-

 import java.io.*;
 import java.util.Scanner;

 public class ScannerTest{
   public static void main(String[]args){
      Scanner s = new Scanner(System.in);
      String str = s.next();
      System.out.println(str);
      int num = s.next Int();
      System. out.println(num);
      s.close();
   }
 }

Console Input: Scanner in JavaScript Program

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.

  
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 uses for Scanner in a loop:-

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);
    }
}

Example for Scanner Program 2:-

This example demonstrates using Scanner to read a file containing lines of structured data. Each line is then parsed using a second Scanner and a simple delimiter character, used to separate each line into a name-value pair. The Scanner class is used only for reading, not for writing.

import java.io.*;
import java.util.Scanner;

public class ReadWithScanner {

  public static void main(String... aArgs) throws FileNotFoundException {
    ReadWithScanner parser = new ReadWithScanner("C:\\Temp\\test.txt");
    parser.processLineByLine();
    log("Done.");
  }
 
  /**
   Constructor.
   @param aFileName full name of an existing, readable file.
  */
  public ReadWithScanner(String aFileName){
    fFile = new File(aFileName); 
  }
 
  /** Template method that calls {@link #processLine(String)}.  */
  public final void processLineByLine() throws FileNotFoundException {
    //Note that FileReader is used, not File, since File is not Closeable
    Scanner scanner = new Scanner(new FileReader(fFile));
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
        processLine( scanner.nextLine() );
      }
    }
    finally {
      //ensure the underlying stream is always closed
      //this only has any effect if the item passed to the Scanner
      //constructor implements Closeable (which it does in this case).
      scanner.close();
    }
  }
 
  /**
   Overridable method for processing lines in different ways.
   
   <P>This simple default implementation expects simple name-value pairs, separated by an
   '=' sign. Examples of valid input :
   <tt>height = 167cm</tt>
   <tt>mass =  65kg</tt>
   <tt>disposition =  "grumpy"</tt>
   <tt>this is the name = this is the value</tt>
  */
  protected void processLine(String aLine){
    //use a second Scanner to parse the content of each line
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if ( scanner.hasNext() ){
      String name = scanner.next();
      String value = scanner.next();
      log("Name is : " + quote(name.trim()) + ", and Value is : " + quote(value.trim()) );
    }
    else {
      log("Empty or invalid line. Unable to process.");
    }
    //no need to call scanner.close(), since the source is a String
  }
 
  // PRIVATE
  private final File fFile;
 
  private static void log(Object aObject){
    System.out.println(String.valueOf(aObject));
  }
 
  private String quote(String aText){
    String QUOTE = "'";
    return QUOTE + aText + QUOTE;
  }
}

2 comments:

  1. Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!Cheer!

    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete
  2. This is one awesome blog article. Much thanks again.
    I really enjoy the blog.Much thanks again. Really Great.


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete