[Java] [Java] Paiza’s skill check is blocked by D unless you can use standard input

2 minute read

Overview

I’ve been playing Progate lightly and have no Java experience, so I am solving the paiza skill check problem in the training, but I made a note because I was stuck unexpectedly on the first day.

As soon as you start the skill check

Input the letter N, so output N

You will be instructed what to do.

It ends with outputting with System.out.print~! I think It is a mistake to write it as it is.

Unlike Progate which is followed variously It is necessary to read the value entered in the skill check here. If you don’t know how to read the input value, paiza cannot even get D rank.

Method

Is it easy? Do you see Java standard input explanation posted on youtube? You can understand it by looking at paiza learning, but since it is a pain, I will write the usage.

In the case of a problem like the one at the beginning, because it is a character

// define a variable
Scanner sc = new Scanner(System.in);
// get the string
String t = sc.nextLine();

If you read it in with this, you can output it.

When numbers are entered

The numbers are almost the same.

// get the number
int t = sc.nextInt();

Please note that it is “nextInt”.

read one by one

For example, suppose a letter is given in the form of one line N B and needs to be N + B.

N B

In this case, if you use nextLine();, N and B will be read together. It seems good to use next();.

// read N
String x = sc.next();
// load B
String y = sc.next();

next() reads up to half-width space, so pseudo splitting is possible without using split. You can use the same with numbers. *However, since it is read as “character”, it is necessary to specify the type of String.

read multiple lines

Suppose the number 1 is given on the first line and the letter N is given on the second line.

1 N

In this case, it seems to be better to get each with nextInt();,nextLine(); Writing this way will result in an error.

// error example
int t = sc.nextInt();
String a = sc.nextLine();

Because nextInt() reads an integer, the newline character actually remains. With this process, the second line cannot be read. So you need to add nextLine(); and skip it.

// read the integer on the first line
int t = sc.nextInt();
// skip newline characters
sc.nextLine();
// read the character on the second line
String a = sc.nextLine();

Paiza has quite a few problems of this type, so it’s worth remembering. I didn’t know why I wasn’t working, so I was pretty busy.

You can also get it as a List, but if you can get the values and characters properly Now you can concentrate on processing the logic. It’s important to visualize what nextLine();, next(); and nextInt(); are doing.

Summary

It’s a simple story for anyone I know. Probably because I was learning based on Progate and books, I stumbled on the unexpected part, so I hope it will be helpful. According to my senior, it seems that it is not used much in the actual field, but it is an important knowledge to capture paiza’s skill check.