[Java 8] Until converting standard input that can be used in coding tests into a list or array

If you want to know the conversion method quickly, please see the conclusion at the end of the page.

background

Continuing from the last time, it is a coding test material again. [Java 8] Sorting method in alphabetical order and string length order that can be used in coding test --Qiita

Obviously, in coding tests, most of the flow is "acquisition of value by standard input"-> "conversion for easy processing"-> "some kind of calculation processing". I don't want to spend time on "acquiring values by standard input" and "converting for my own processing" in order to spend even a little time on "some kind of calculation processing".

This time, as the title suggests, I will summarize how to convert standard input to a list or array.

Operating environment

Common standard input in problems

A common standard input in question is an image like the one below.

1,AIUEO,Kakikukeko

Suppose that the problem is to get characters separated by commas (,) and output the characters after them by the number entered first. In the above example, the first number is 1, so the output is

AIUEO

Is the correct answer.

2,AIUEO,Kakikukeko,SA Shi Su Se So

If is a standard input

AIUEO
Kakikukeko

Is the correct answer.

In such cases, I would like to read the string first and store it in a comma separated list.

From getting standard input to converting to List

First, get the standard input as follows.

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();

There seems to be a method of acquisition by Scanner, but I am using a pattern that uses java.io.BufferedReader and java.io.InputStreamReader. The character string for one line is stored in line.

Next, let's try the process of storing in the List separated by commas (,) in the main subject.

List<String> list = Arrays.asList(line.split(","));

It's easy, but that's it.

split splits a string at a position that matches the specified regular expression. The return value of this is String []. It is a string type array. Since it is split by a regular expression, it can be split by anything other than a comma (,). The next step is to use the java.util.Arrays.asList method to convert the array to a list.

It may be independent as a method as shown below.

/**
 *Convert from String to List
 * @param str Converted String
 * @return Converted List
 */
private static List<String> stringToList(String line) {
  return Arrays.asList(line.split(","));
}

Common standard input answers to problems

If you answer the problem that you have thought about, the source code will be as follows. ~~ It's okay to use an array instead of a List! !! And You don't need to convert it to List! !! Please do not say "Tsukomi" ......... ~~

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;


public class Main {
	public static void main(String[] args) throws IOException {
    try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
			String line = br.readLine();
			List<String> list = Arrays.asList(line.split(","));
			int cnt = Integer.parseInt(list.get(0));
			for (int i = 1; i <= cnt; i++) {
				System.out.println(list.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Conclusion

If it is a List,

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
List<String> list = Arrays.asList(line.split(","));

If it is an array

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] strArray = line.split(",");

Recommended Posts

[Java 8] Until converting standard input that can be used in coding tests into a list or array
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
Write a class that can be ordered in Java
Summary of ORM "uroboroSQL" that can be used in enterprise Java
Java file input / output processing that can be used through historical background
[Android Studio] Description that can be continuously input in SQLite Database [Java]
Technology excerpt that can be used for creating EC sites in Java training
Convenient shortcut keys that can be used in Eclipse
Syntax and exception occurrence conditions that can be used when comparing with null in Java
List of devices that can be previewed in Swift UI
Create a page control that can be used with RecyclerView
Create a jar file that can be executed in Gradle
Java (super beginner edition) that can be understood in 180 seconds
Object-oriented design that can be used when you want to return a response in form format
Read standard input in Java
[Java] How to search for a value in an array (or list) with the contains method
How to make a key pair of ecdsa in a format that can be read by Java
Reference memo / In-memory LDAP server that can be embedded in Java
Static analysis tool that can be used on GitHub [Java version]
Note that system properties including JAXBContext cannot be used in Java11
I made a question that can be used for a technical interview
SwiftUI View that can be used in combination with other frameworks
Cast an array of Strings to a List of Integers in Java
How to override in a model unit test so that Faker can be used to generate random values
[Spring Boot] List of validation rules that can be used in the property file for error messages
Ruby array methods that can be used with Rails (other than each)