Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Input and output

Input

In Java, input is the process of getting data from the user or from another source. There are two main ways to get input in Java:

Using the Scanner class Using the System.in object
The Scanner class is a class that provides methods for reading input from the console. It can be used to read input of different types, such as strings, integers, and doubles. The System.in object is an object that represents the standard input stream. It can be used to read input from the console, but it can only be used to read input of type String. Here is an example of how to use the Scanner class to get input from the user:

Java input from user example


import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String name = scanner.nextLine(); int age = scanner.nextInt(); double rank = scanner.nextDouble(); System.out.println("Your name is " + name); System.out.println("Your age is " + age); System.out.println("Your rank is " + rank); } }

Output

Your name is Name Your age is 20 Your rank is 20
In this code, the Scanner object is created and initialized with the System.in object. The nextLine() method is used to read a line of text from the console. The nextInt() method is used to read an integer from the console. The nextDouble() method is used to read a double from the console. Here is an example of how to use the System.in object to get input from the user:

Java


public class Main { public static void main(String[] args) { String name = System.console().readLine("Enter your name: "); int age = Integer.parseInt(System.console().readLine("Enter your age: ")); double rank = Double.parseDouble(System.console().readLine("Enter your rank: ")); System.out.println("Your name is " + name); System.out.println("Your age is " + age); System.out.println("Your rank is " + rank); } }
In this code, the System.console() method is used to get a reference to the console. The readLine() method is used to read a line of text from the console. The Integer.parseInt() method is used to convert a string to an integer. The Double.parseDouble() method is used to convert a string to a double. It is important to note that the System.console() method is only available on platforms that have a console, such as the Java Development Kit (JDK) for Windows and Linux. Here are some additional details about input in Java: - Input can be read from different sources, such as the console, files, and network connections. - Input can be read of different types, such as strings, integers, doubles, and objects. - There are different ways to read input, such as using the Scanner class and the System.in object. - It is important to understand how to get input in Java. By understanding these concepts, you can write code that can interact with users and other sources of data.

  📌TAGS

★input in Java ★ input ★Java input

Tutorials