Very simple input reception in Java

I hadn't written it for a while, so I completely forgot. It is a memo that receives input by brain death.

Common items

Call the Scanner with the import statement.

python


import java.util.*;

Pass System.in to the Scanner class.

python


Scanner sc = new Scanner(System.in);

Receiving strings

Let $ S $ be a character string.

python


String S = sc.next();
String S = sc.nextLine();

However, next recognizes the input value up to the line feed, and nextLine recognizes up to the blank. For example

S_1 S_2 S_3

Is input, next reads $ S_1 $ $ S_2 $, and nextLine reads $ S_1 $.

Reading numbers

Let $ x $ be a number. It seems that parsing with ʻInteger.parseInt () is about twice as fast.

python


int x = Integer.parseInt(sc.nextLine());
int x = sc.nextInt();

Read array

Let $ V_1, V_2, \ cdots V_n $ be the array V.

python


Integer V[] = new Integer[N];
for (int i=0; i<N; i++) {
    V[i] = sc.nextInt();
}

Readability is low if:

python


String a = sc.nextLine();
String[] b = a.split(" ");
int[] c = Stream.of(b).mapToInt(Integer::parseInt).toArray();

Read List

Let $ V_1, V_2, \ cdots V_n $ be ListV.

python


ArrayList<String> V = new ArrayList<String>();
for (int i=0; i<N; i++) {
    V[i] = sc.nextInt();
    ary.add(word);
}

It seems that you can expect higher speed by making your own Scanner.

Recommended Posts

Very simple input reception in Java
Simple htmlspecialchars in Java
Read standard input in Java
2 Implement simple parsing in Java
3 Implement a simple interpreter in Java
A simple sample callback in Java
1 Implement simple lexical analysis in Java
Receive joystick input in Java LW JFL
Console input in Java (understanding the mechanism)
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[java] sort in list
Read JSON in Java
Interpreter implementation in Java
Make Blackjack in Java
I made a simple calculation problem game in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
Combine arrays in Java
"Hello World" in Java
Callable Interface in Java
How to input / output IBM mainframe files in Java?
Comments in Java source
Azure functions in java
Format XML in Java
[Personal memo] Make a simple deep copy in Java
Boyer-Moore implementation in Java
Hello World in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Ping commands in Java
Various threads in java
Heapsort implementation (in java)
Zabbix API in Java
ASCII art in Java
Compare Lists in Java
POST JSON in Java
Express failure in Java
Create JSON in Java
Date manipulation in Java 8
What's new in Java 8
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
Initializing HashMap in Java
java Scanner loop input
Get weather forecasts from Watson Weather Company Data in simple Java
Make your own simple server in Java and understand HTTP
Sorting in ascending order in Java (bubble sort: simple exchange algorithm)
Talk about using Java input wait (Scanner) in VS Code
Try using RocksDB in Java
Read binary files in Java 1
Avoid Yubaba's error in Java
Get EXIF information in Java
Save Java PDF in Excel