Java Input Statement

 

 

One of the most frustrating thing for the beginning Java programmer is that Java has no simple way to provide input to a program. You read through the examples and all have their data already in the program and there's no way to change it. "C" has a "scanf" statement and "BASIC" has an "INPUT" statement but the folks at Sun don't think you deserve one.

K. N. King solved all this by writing a program that adds a simple input statement to the Java language. When you follow these directions, the statement "String userInput=SimpleIO.readline();" will result in whatever you type in, being put in the userInput string! Just what the doctor ordered!

He also has a "SimpleIO.prompt" statement and a "Convert.toDouble" statement that you can learn to use by studying the FtoC9 program.

 

DIRECTIONS

Download and unzip the following zip file.

jpb.zip

Put the unzipped folder in your C/JDK1.3/bin.

Read the html file and the examples. The FtoC9 is a good temperature converter and a great example

 


After writing the above, I found John Smileys book " Learn to Program with Java ". Smileys version of a console type input, Java program is below:

//Example4_3, Page 144

 

import java.io.*;
class Example4_3 {
public static void main (String[] args) {
    BufferedReader stdin = new BufferedReader (newInputStreamReader(System.in));
    String response;
    System.out.println ("What is your favorite programming language?");
    response = stdin.readLine();
    System.out.println ("You have great taste. " + response + " is a great language");
  }
}

   

And finally here are the new Java5 Formatted Input classes that are avalible now in the new Java5 download from Sun.

The scanner API provides basic input functionality for reading data from the system console or any data stream. The following example reads a String from standard input and expects a following int value.

The Scanner methods like next and nextInt will block if no data is available. If you need to process more complex input, then there are also pattern-matching algorithms, available from the java.util.Formatter class.

Scanner s= new Scanner(System.in);
String param= s.next();
int value=s.nextInt();
s.close();

 

 

 

Back to Main Page