[Java] Paiza's skill check is clogged with D if you can not master standard input

Overview

Since I did Progate lightly and have no Java experience, I am solving the skill check problem of paiza in the training, but I will make a note because it got stuck in an unexpected place on the first day.

As soon as you start the skill check

The letter N is entered, so output N

You will be instructed about the contents.

Output with System.out.print ~ and you're done! I think It would be a mistake to write it as it is.

Unlike Progate, which is followed in various ways In the skill check, you need to read the values entered here as well. If you don't know how to "read the input value" here, paiza can't even get D rank.

Method

Because it is easy, do you see Java standard input explanation posted on youtube? You can understand it by looking at paiza learning, but since it's a big deal, I'll write how to use it.

In the case of a problem like the beginning, it's a letter

//Define variables
Scanner sc = new Scanner(System.in);
//Get string
String t = sc.nextLine();

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

When numbers are entered

The numbers are almost the same.

//Get numbers
int t = sc.nextInt();

Note that it is "next Int".

Read one by one

For example, suppose a character 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 () ;, both N and B will be read together. It looks good to use next () ;.

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

Since next () reads up to half-width spaces, it can be pseudo-split without using split. You can use it in the same way for numbers.

Read multiple lines

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

1 N

In this case, it seems good to get them with nextInt (); and nextLine () ;, respectively. Writing as it is will result in an error.

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

Since nextInt () reads an integer, the newline character is actually left behind. 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 the newline character
sc.nextLine();
//Read the letters 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 it didn't work, so I was pretty stuck ...

You can also get it as a List, but if you can get the values and characters properly After that, you can concentrate on processing the logic. It is important to imagine how nextLine ();, next (); and nextInt (); are working respectively.

Summary

It's a simple story for those who know it. Perhaps because I was learning by relying on Progate and books, I stumbled on an unexpected part, so I hope it will be helpful. According to my seniors, it seems that it is not used much in the actual field, but it is important knowledge to capture the skill check of paiza.

Recommended Posts

[Java] Paiza's skill check is clogged with D if you can not master standard input
paiza skill check standard input / output summary [Java edition]
[Rails] If CSS is not applied in collection_select, check if class can be specified.
[Good news] You can still go with Java 8 !!