
Stream Input/Output
Now that you now the format of a Java program, it is time to learn how to print to stream and read from stream.
Ummm..What is Stream?
To make things simple, for now stream is the console in your Operating System. For example, in Windows the stream would be the command prompt. On Linux, stream would be the Terminal. In this tutorial, you will learn how to print and format text to the stream. Also, you will learn how to read from stream using the Java Scanner class.
Stream Output
Remember the System.out.println()? Well this is exactly how we print to stream. However, more printing methods are available.
System.out.println("Welcome to Printing");
println:
This method is called the print line method. It prints any text in the quotations and moves the cursor to the next line. Check the code below for an example on using print line
public class Printing
{
public static void main(String args[])
{
System.out.println("Welcome to Java Judge");
System.out.println("I am printed on a new Line now!!");
}
}
Output:
Welcome to Java Judge I am printed on a new Line now!!
print:
System.out.print("Welcome to Printing");
This method is called normal printing. It also prints any text in the quotations, but it doesn't move the cursor to the next line. Want to know what we mean? see the code below.
public class Printing
{
public static void main(String args[])
{
System.out.print("Welcome to Java Judge");
System.out.print(" I am printed on the same line now!!");
}
}
Output:
Welcome to Java Judge I am printed on a the same line now!!
printf:
System.out.printf("%d %f %c \n", number1, number2, letter);
This method is called the formatted printing method. It gives the programmer more control over how the output should look like. To ebalorate more, check the table below for how printf() is used.
| Code | Description |
|---|---|
| %d | Printing and Formatting an Integer |
| %f | Printing and Formatting a Double |
| %c | Printing a Character |
Take a look at the code below for an example on how to use formatted printing.
public class Printing
{
public static void main(String args[])
{
int x = 2;
double y = 5.5;
char c = 'A';
System.out.printf("I will print an integer which is %d \n", x);
System.out.printf("I will print a double which is %f \n", y);
System.out.printf("I will print a chracter which is %c \n", c);
}
}
Output:
I will print an integer which is 2 I will print a double which is 5.5 I will print a character which is A
You may have noticed that we left the \n unexplained. No worries, all will be explained now. This is called an escape sequence. Note that these can be used with any of the printing methods and not just printf(). Check the table bellow for a list of escape sequences and how they are used.
| Escape Sequence | Description |
|---|---|
| \n | New Line which positions the screen cursor on a new line |
| \t | Horizontal tab which moves the cursor to the next tab stop |
| \\ | Backslash which prints a backslash \ |
| \" | Double quote which is used to print a double quote " |
public class Printing
{
public static void main(String args[])
{
System.out.println("I will print and then move two lines down\n");
System.out.println("I will print a tab\tlike this");
System.out.println("I will print backslashes \\");
System.out.println("I will print a double quote \"");
}
}
Output:
I will print and then move two lines down
I will print a tab like this
I will print backslashes \
I will print a double quote "
Using + With Printing
If we want to print a variable, we used printf as you may have noticed. However, using printf is a little bit complicated when we have so many variables. Therefore, we use the plus operator when printing so that the variable will be printed. Let's look at the program below to understand more.
public class Printing
{
public static void main(String args[])
{
int x = 10;
System.out.println("The value of x is "+x);
}
}
Output:
The value of x is 10
Using the plus here added the value of x to be printed next to "The value of x is ". Therefore, whenever you want to print a variable, you just close the quotations and add a + then the variable name.
Stream Input
It's time to learn how to read input from the user. Java provides a tool for reading input which is the Scanner. Whether you want to read an int, double, or String, the Scanner class can help you do so. Enough introductions and lets get started.
Well let's get started. First of all check the code below and let's explain how everything works.
import java.util.Scanner; public class Reading
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Congrats! you created a Scanner");
}
}
Now let's explain this code step by step.
import java.util.Scanner;
This is all new. You may not have seen an import statement yet but don't worry we will explain it now.
import is a way of telling your program that you will be using predefined Java Libraries or tools. This is exactly what we did here. We imported the Scanner to our program so that we may use it.
OK..but what is java.util.?
Well here we come to the concept of packages. Let's take a real world example. Let's say you want to watch a DVD movie and that movie is inside a DVD case. Also, the DVD case is in your drawer. Therefore, you have to first open the drawer, then open the DVD case, then use the DVD movie. Well, let's compare this to the Scanner. First we need to open up java, then we have to check the utilities within it, then we find the Scanner and use it.
Scanner input = new Scanner(System.in);
Here we are creating a Scanner to be used and we are giving it a name "input". Note that you can name it whatever you like. It can be Scanner x, Scanner scan, or even Scanner icecream. For now, just memorize how to create a Scanner and we will explain the new keyword in the Classes and Objects tutorial. However, we will tell you that the "System.in" tells the Scanner to read from stream.
Now that we have created a Scanner, it's time to use it. Check the table below for the Scanner methods and what they are used for.
| Methods | Description |
|---|---|
| nextInt() | Read an integer |
| nextDouble() | Read a double |
| next() | Read a word |
| nextLine() | Read a line |
Now let's look how to code these methods.
public class Reading
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int x;
double y;
String word;
String line;
System.out.print("Please enter an integer: ");
x = input.nextInt();
System.out.println("You entered: "+x);
System.out.print("Please enter a double: ");
y = input.nextDouble();
System.out.println("You entered: "+y);
System.out.print("Please enter a word: ");
word = input.next();
System.out.println("You entered: "+word);
System.out.print("Please enter a line: ");
line = input.nextLine();
System.out.println("You entered: "+line);
}
}
Output:
Please enter an integer: 5 You entered: 5 Please enter a double: 3.2 You entered: 3.2 Please enter a word: Welcome You entered: Welcome Please enter a line: Java Judge You entered: Java Judge
Congratulations
You now know how to read input and how to print output. Move on to the next tutorial and check the notes below.
Important Notes
Remember Java is Case Sensitive
As we said in the previous tutorial, Java differentiates between uppercase and lowercase letters. Therefore, if you write Import Java.util.Scanner;, this will result in an error because the "i" in import is uppercase and the "j" in java is also uppercase. Also, writing scanner input = new scanner(System.in); will result in an error because the "S" in Scanner is lowercase. Please pay attention to these common mistakes.