java input output

Java Input and Output | Java Tutorial

In this tutorial, you will learn how to display output to users and take user input in Java. With its I/O package, Java provides a variety of Streams that assist the user in performing all input-output operations.

MethodsDescription
int nextInt()It’s used to scan the input for the next token as an integer.
float nextFloat()As a float, it is used to scan the next token of the input.
double nextDouble()It’s used to scan the input’s next token as a double.
byte nextByte()It’s used to scan the input for the next token as a byte.
String nextLine()This scanner is advanced past the current line.
boolean nextBoolean()It scans the next token of the input and converts it to a boolean value.
long nextLong()It’s used to scan the input for the next token as a long.
short nextShort()As a Short, it is used to scan the next token of the input.
BigInteger nextBigInteger()As a BigInteger, it is used to scan the next token of the input.
BigDecimal nextBigDecimal()As a BigDecimal, it is used to scan the next token of the input.

Java Output

You can simply use in Java for output:

java input output,java input output,java input output,java input output,java input output,java input output,java input output,java input output

  • System.in: The standard input stream is used to read characters from the keyboard or any other standard input device.
  • System.out: This is the standard output stream that is used to display the output of a program on a display device such as a computer screen.

System.out.println(); or
System.out.printf(); or
System.out.print();
  • println(): In Java, this method is used to display text on the console. It prints the text to the console and moves the cursor to the beginning of the next line. The next printing begins with the next line.

System.out.println(“Developers Dome”);
class Sample {
    public static void main(String[] args)
    {
  // using println(), all are printed on a separate line.
        System.out.println("DevelopersDome");
        System.out.println("DevelopersDome");
        System.out.println("DevelopersDome");
    }
}

output:

DevelopersDome
DevelopersDome
DevelopersDome
  • print(): In Java, this method is used to display text on the console. This text is passed as a parameter to this method as a String. This method prints the text to the console while keeping the cursor at the end of the text. The next printing will begin right here.
System.out.print(“Developers Dome”);
class Sample {
    public static void main(String[] args)
    {
  // using print(), all are printed on the same line.
        System.out.print("DevelopersDome");
        System.out.print("DevelopersDome");
        System.out.print("DevelopersDome");
    }
}
DevelopersDome DevelopersDome DevelopersDome
  • printf(): This is the simplest method because it is similar to printf in C. It’s worth noting that System.out.print() and System.out.println() only take one argument, whereas printf() can take multiple arguments. In Java, this is used to format the output.
System.out.printf(“Developers Dome”);
  • System.err: This is the standard error stream that is used to output all error data thrown by a program on a computer screen or any standard output device.

Java Input

Java provides several methods that were used to collect user input. However, in this tutorial, you will learn how to get user input using the Scanner class’s object.

  • it is used to read primitive type input such as int, double, long, short, float, and byte. It is the simplest method for reading input in a Java program.
  • To use the Scanner object, we must import the java.util.Scanner package.
import java.util.Scanner;

Then we’ll need to make a Scanner class object. We can use the object to receive user input.

// create an object of Scanner
Scanner input = new Scanner(System.in);

// take input from the user
int number = input.nextInt();

Example: Get Integer Input From the User

import java.util.Scanner;

class Input {
    public static void main(String[] args) {
    	
        Scanner input = new Scanner(System.in);
    	
        System.out.print("Enter an integer value: ");
        int n = input.nextInt();
        System.out.println("You entered " + n);

        input.close();
    }
}

Output:

Enter an integer value: 48
You entered 48

In the above example, we created a Scanner class object named input. We then use the Scanner class’s nextInt() method to get an integer input from the user.

Example: Get float, double, and String Input

import java.util.Scanner;

class Sample {
    public static void main(String[] args) {
    	
        Scanner Sample = new Scanner(System.in);

        //  String input
        System.out.print("Enter text here: ");
        String mString = Sample.next();
        System.out.println("Text entered = " + mString);
    	
        // float input
        System.out.print("Enter float value : ");
        float mFloat = Sample.nextFloat();
        System.out.println("Float entered = " + mFloat);
    	
        // double input
        System.out.print("Enter double value: ");
        double mDouble = Sample.nextDouble();
        System.out.println("Double entered = " + mDouble);
    	
    }
}

output:

Enter text here: DevelopersDome
Text entered = DevelopersDome
Enter float: 454.5
Float entered = 454.5
Enter double: -265.6
Double entered = -265.6

We hope that this article will assist you in understanding all about Java Basic Input and Output. We have concentrated on making a basic, meaningful, and easy-to-learn guide to the concepts. Still, if you have any problems regarding this, please post them in the comment section, and we will be glad to assist you.

This Post Has 3 Comments

Leave a Reply