[Java] How to get and output standard input

Programming study diary

December 21, 2020 I've used standard input in C, but never in Java. I will summarize what I studied today.

What is standard input / standard output?

Simply put, keyboard input is called standard input. In Java, get the standard input in the in field of the Sysytem class.

The standard output is like a device for displaying data from a program, which is displayed on a display. Java uses the out field of the Syste class.

How to use Scanner

The following methods are provided in the java.util.Scanner class.

--NextLine method to get one line of input --Next method to get input up to whitespace --NextInt method to get numeric input

How to use the nextLine method

The nextLine method can get the input for one line up to the line feed.

Sample code


import java.util.Scanner;

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

    System.out.println(str);
    scan.close();
  }
}

How to use the next method

The next method can get the input up to the whitespace character.

Sample code


import java.util.Scanner;
 
public class Sample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str1 = scan.next();
        String str2 = scan.next();
 
        System.out.println(str1);
        System.out.println(str2);
        scan.close();
    }
}

How to use nextInt

You can get an int type number by using nextInt. There are also nextDouble and nextFloat methods to get the floating point type.

Sample code


import java.util.Scanner;
 
public class Sample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int num1 = scan.nextInt();
        int num2 = scan.nextInt();

        int sum = num1 + num2;
        //Outputs the addition of the entered numbers
        System.out.println(num1 + " + " + num2 + " = " + sum);
        scan.close();
    }
}

References

Class Scanner [Introduction to Java] How to get and output standard input (Explanation of Scanner)

Recommended Posts

[Java] How to get and output standard input
[Java] How to output and write files!
How to get and study java SE8 Gold
How to input / output IBM mainframe files in Java?
How to use Java HttpClient (Get)
[Java] I want to test standard input & standard output with JUnit
How to output Java string to console screen
[Java] How to get the current directory
How to get the date in java
[Java] Color the standard output to the terminal
From terminal to Ruby (standard input / output)
How to read a file and treat it as standard input
[Java] How to convert from String to Path type and get the path
How to output Excel and PDF using Excella
[Java] How to use FileReader class and BufferedReader class
[Java] How to get the current date and time and specify the display format
[Java] How to receive numerical data separated by spaces in standard input
How to get Class from Element in Java
How to record JFR (Java Flight Recorder) and output a dump file
[Java] How to get random numbers excluding specific numbers
[Java] How to get the redirected final URL
[Rails] How to get success and error messages
[Java] How to get the authority of the folder
[ruby] How to receive values from standard input?
How to access Java Private methods and fields
How to create your own annotation in Java and get the value
[Java] How to use Calendar class and Date class
[Java] How to get the URL of the transition source
[Ruby] How to use standard output in conditional branching
[Java] Types of comments and how to write them
[Java] How to encrypt with AES encryption with standard library
[Java] How to get the maximum value of HashMap
Java memory management and how to read GC Viewer
[Java] How to get a request by HTTP communication
How to output standard from an array with forEach
As of April 2018 How to get Java 8 on Mac
How to convert A to a and a to A using AND and OR in Java
paiza skill check standard input / output summary [Java edition]
[Ruby] How to get the tens place and the ones place
Gzip-compress byte array in Java and output to file
[Kotlin] How to get IP address and user agent
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable
How to use java Optional
How to minimize Java images
How to write java comments
How to use java class
[Java] How to use Optional ②
Read standard input in Java
[Java] How to use removeAll ()
[Java] How to display Wingdings
[Java] How to use string.format
How to use Java Map
How to set Java constants
Java standard log output sample
How to use Java variables
How to convert Java radix
[Java] How to implement multithreading