Java User Input and Output

Java User Input and Output

In this article, I am going to discuss Java User Input and Output with examples. Please read our previous article, where we discussed Methods in Java with examples. At the end of this article, you will understand how to accept input from the user and display output to the users in java application. 

The User Input: Scanner Class

One really useful class that handles input from a user is called the Scanner class. Scanner class is present in “java.util” package, so we import this package into our program. It also converts the Bytes (from the input stream) into characters using the platform’s default charset. To use the Scanner class, you need to reference it in your code. This is done with the keyword import. We create an object of the class to use its methods.

import java.util.Scanner;

The import statement needs to go just above the Class statement: The Syntax is given below.
import java.util.Scanner;
public class StringVariables {
}

This tells Java that you want to use a particular Scanner class which is located in the java.util library. The next thing you need to do is to create an object from the Scanner class.

Syntax to create a new Scanner object:

Scanner sc = new Scanner( System.in ); Here, sc is the name of the object, the new keyword is used to allocate memory, and System.in is the input stream.

Sample Program for Java Input:

In the below example we are getting input String, integer, and a float number. For this we are using the following methods:
1) public String next(): For getting the next String of text that a user types
2) public int nextInt(): For integer input
3) public float nextFloat(): For float input

package Demo;
import java.util.Scanner;
class UserInputDemo
{
   public static void main(String args[])
   {
      int a;
      float b;
      String s;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter an integer");
      a = sc.nextInt();
      System.out.println("You entered integer " + a);
      System.out.println("Enter a float");
      b = sc.nextFloat();
      System.out.println("You entered float " + b);  
   
      System.out.println("Enter a string");
      s = sc.next();
      System.out.println("You entered string " + s);
   }
}

Now run your program.

Java User Input and Output

You can see, Java is now pausing until you enter something on your keyboard. It won’t progress until you hit the enter key. So type an integer and then press “Enter”. After you hit the enter key, java will take whatever was typed and store it in the variable name. In this program, the variable_name to store the integer is: a

The program then moves on to the next line of code:

Sample Program for Java Input

And so on….. Until all the user inputs have been finished and the rest of the program executes. And the final result looks like this:

Methods Of Java scanner Class:

So we used the Scanner class to get input from a user. Whatever was typed was stored in variables. The result was then printed to the Output window.

Methods Of Java scanner Class:
  • int nextInt(): It is used to scan the next token of the input as an integer.
  • float nextFloat(): It is used to scan the next token of the input as a float.
  • double nextDouble(): It is used to scan the next token of the input as a double.
  • byte nextByte(): It is used to scan the next token of the input as a byte.
  • String nextLine() : Advances this scanner past the current line.
  • boolean nextBoolean(): It is used to scan the next token of the input into a boolean value.
  • long nextLong(): It is used to scan the next token of the input as a long.
  • short nextShort(): It is used to scan the next token of the input as a Short.
  • BigInteger nextBigInteger(): It is used to scan the next token of the input as a BigInteger.
  • BigDecimal nextBigDecimal(): It is used to scan the next token of the input as a BigDecimal.
Java Output:

In Java, to display output to the users you can simply use System.out.println(); or System.out.print(); It will simply send output to the standard output screen.

What is System.out.println() in Java?

In Java, System.out.println() is a statement that prints the argument passed to it.

What is System.out.println() in Java?

Here,

System: is a final class in java.lang package. The facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables.

out: is an instance of PrintStream type, which is a public and static member field of the System class. Its access specifiers are public final. This gets instantiated during startup and gets mapped with the standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data.

println(): is a method of java.io.PrintStream. This method is overloaded to print the message to the output destination, which is typically a console and a new line.

Java User Input and Output with examples

Sample Program to output a line in Java:
package Demo;

public class OutputDemo {
 public static void main(String[] args) {
  System.out.println("Java is very interesting Programming Language.");
 }
}

Output :

Program to output a line in Java

Java print() Method:

It prints string inside the quotes. The print() method does not move the cursor to the next line after printing the result. It is used when you want the result in one separate line.

Sample Program to demonstrate the working of print():
package Demo;

public class PrintDemo {
 public static void main(String[] args) {
  System.out.print("Hello");
  System.out.print("World");	
 }
}

Output : Java User Input and Output with Examples

It means the first print() method displays the string “Hello” and retains the cursor at the same line. The second print() method also displays the string “World” at the same line adjacent to the previous string.

Java println() Method:

It prints string inside the quotes similar to the print() method. Then the cursor moves to the beginning of the next line. It is used when you want the result in two separate lines. It is called with “out” object. It is also an overloaded method of the PrintStream class. It throws the cursor to the next line after displaying the result.

Sample Program to demonstrate the working of println():
package Demo;

public class PrintlnDemo {
 public static void main(String[] args) {
  System.out.println("Hello");
  System.out.println("World");
 }
}

Output:

Java User Input and Output with Examples

It means the first println() method displays the string “Hello” and moves the cursor to the next line. The second print() method displays the string “World” at the next line.

Difference Between print() and println() Methods in Java

print()

println()

A method in Java that is used to display a piece of text on the console which allows the cursor to remain in the same position. A method in Java that is used to display a piece of text on the console which allows the cursor to go to the new line.
The cursor remains on the same line after printing the text. The cursor moves to the next line after printing the text.
The print method only works with arguments otherwise it will cause a syntax error. The println method can work without arguments.

In the next article, I am going to discuss Pass By Value and Pass By Reference in Java. Here, in this article, I try to explain Java User Input and Output with examples and I hope you like this Java User Input and Output with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this Java User Input and Output with Examples article.

Leave a Reply

Your email address will not be published. Required fields are marked *