[Java] How to receive numerical data separated by spaces in standard input

Programming study diary

December 22, 2020 Continuing from yesterday, we will deal with standard input in Java. Shows how to receive the entered numerical values ​​separated by blanks as data.

How to use Scanner's nextInt method

import java.util.*;

public class Sample {
	public static void main(String[] args) {
		int[] points = new int[5];
		Scanner scan = new Scanner(System.in);
		
		for(int i = 0; i < points.length; i++) {
		    points[i] = scan.nextInt();
		    System.out.println(points[i]);
		}
	}
}

How to cast

import java.util.*;

public class Sample {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		String[] pointsStr = scan.nextLine().split(" ");
		int[] points = new int[pointsStr.length];

		for(int i = 0; i < points.length; ++i) {
		    points[i] = Integer.parseInt(pointsStr[i]);
		    System.out.println(points[i]);
		}
	}
}

References

[Java] How to get and output standard input

Recommended Posts

[Java] How to receive numerical data separated by spaces in standard input
[Java] How to get and output standard input
[ruby] How to receive values from standard input?
How to concatenate strings separated by commas using StringJoiner even in java7 environment
How to input / output IBM mainframe files in Java?
How to create a data URI (base64) in Java
[Java] Receive standard input while performing numerical checks, etc.
Read standard input in Java
How to use JSON data in WebSocket communication (Java, JavaScript)
How to connect the strings in the List separated by commas
Several ways to concatenate strings separated by commas in Java
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
Learning Ruby with AtCoder 11 How to receive frequently used standard input
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to add data to List (add, addAll)
How to do base conversion in Java
Receive joystick input in Java LW JFL
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to get the date in java
How to overwrite Firebase data in Swift
How to set tabs and spaces to be visible by using the tab key to insert spaces in Java files in Eclipse
How to display a web page in Java
How to get Class from Element in Java
How to hide null fields in response in Java
How to separate .scss by controller in Rails
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
[Java] How to get the key and value stored in Map by iterative processing
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
[Java] How to encrypt with AES encryption with standard library
[Java] How to get a request by HTTP communication
How to store Rakuten API data in a table
[Java] How to cut out a character string character by character
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
Summary of how to implement default arguments in Java
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
How to get the class name / method name running in Java
How to read your own YAML file (*****. Yml) in Java
Memo: [Java] How to check groupId etc. described in pom.xml
How to store a string from ArrayList to String in Java (Personal)
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
Create API to send and receive Json data in Spring
How to select a specified date by code in FSCalendar
How to dynamically switch JDK when building Java in Gradle
How to develop and register a Sota app in Java