This is an introduction to Console I/O. In other words, basic interactive programs.
I am going to show you the methods
print() and
println() first, as these will either inform the user of the output or prompt them for what they need to enter.
Let's get to the code!
The print() and println() Methodsjava
public class test
{
public static void main(String[] args)
{
// the print() method simply outputs text to the screen
System.out.print("This is text.");
}
}
This will output
This is text." to the screen. Now, a common mistake I find in beginners with this is that they wanna know how to put the next text on the next line. This next one is how you do this. It's as simple as changing the method.
java
public class test
{
public static void main(String[] args)
{
// the println() method will output whatever's in the
// parentheses, then (basically) hits <Enter>, or
// terminates the current line.
System.out.println("This is text.");
}
}
This will output the same thing as the last one, BUT it will terminate the line after it does. Anything else that is outputted to the screen right after this will be on the next line.
_______________________________________________________________
Inputting DataThis is fairly simple. I'll show you 2 different ways, the old way and the new (better) way.
BufferedReaderThe first way will be the old way, using
BufferedReader and
InputStreamReader. This is the old way and it's not as good as the new way because it lets you enter anything, even if it's not what you prompted for, which causes errors/Exceptions during runtime.
java
// MAKE SURE TO IMPORT java.io.*!!!!
import java.io.*;
public class test
{
public static void main(String[] args)
{
// this line is not necessary, but most people use it
// System.in is the keyboard input stream
InputStreamReader rdr = new InputStreamReader(System.in);
// use the InputStreamReader as the parameter to read from keyboard
BufferedReader reader = new BufferedReader(rdr);
// prompt for what to enter
System.out.print("Enter a number: ");
// get the input (ALWAYS A STRING)
String in = reader.readLine();
// transfer that String to an integer
int number = Integer.parseInt(in);
// output the number
System.out.println(number);
}
}
As you can see, the
InputStreamReader object going INSIDE the
BufferedReader constructor can be a little confusing to a beginner, and it was a big thing I always missed.
When you use
BufferedReader, the input is
ALWAYS a
String...ALWAYS. You have to
parse the String into a number or whatever you want it to be to use it in anything mathematical.
The New Way (Scanner)The new way, Scanner, prevents the
BufferedReader's old mistake of letting the user input
anything. It's also MUCH easier to boot!
java
// does NOT use java.io
import java.util.Scanner;
public class test
{
public static void main(String[] args)
{
// much easier declaration
Scanner scan = new Scanner(System.in);
// prompt for input
System.out.print("Enter a number: ");
// get the input, NO PARSING.
// The nextInt() method prevents the user
// from crashing the program here...
// As it only accepts number(s) as input
int number = scan.nextInt();
// output the number
System.out.println(number);
}
}
This code sample does the EXACT same thing as the first one, except you can only enter numbers!

The fact that it protects against faulty input makes it a much better alternative to the primitive
BufferedReader, HOWEVER...
You must have JAVA 5 (JDK 1.5) or later to use Scanner!