Note: next ・ nextLine (paiza learning Java introduction 9: # 06)

Today I wrote the following code in paiza's Java Primer 9.

Input is below

6 Recovery agents shield crystal crystal sword sword

The output is below


//Output images in order
import java.util.*;

public class Main {
    public static void main(String[] args) {
        //Image hash
        HashMap<String, String> itemImages  = new HashMap<String, String>();
        itemImages.put("sword", "http://paiza.jp/learning/images/sword.png ");
        itemImages.put("shield", "http://paiza.jp/learning/images/shield.png ");
        itemImages.put("Recovery agents", "http://paiza.jp/learning/images/potion.png ");
        itemImages.put("crystal", "http://paiza.jp/learning/images/crystal.png ");

        //Write below from here
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        
        String[] itemOrders = new String[num];
        
        for (int i = 0 ; i < num; i++) {
            itemOrders[i] = sc.next();
        }
        
        for (String itemName : itemOrders) {
            System.out.println("<img src = '" + itemImages.get(itemName) + "'>");
            System.out.println("<br>");
        }
    }
}

Here above

for (int i = 0 ; i < num; i++) {
            itemOrders[i] = sc.next();
        }

Part of

for (int i = 0 ; i < num; i++) {
            itemOrders[i] = sc.nextLine();
        }

Anyway

Read the first "6" of the input value and Maybe because I didn't read the last "sword"

スクリーンショット 2019-08-23 20.09.14.png

It became like.

If you change the part of nextLine to next

スクリーンショット 2019-08-23 20.09.35.png

And succeeded.

As a result of investigating various causes Due to the nature of nextLine, influenced by the previous nextInt It seems that the part of the number read by nextInt was read as a blank.

The reason why I fixed it to next is that unlike nextLine, which recognizes spaces, it is recognized by line breaks. I thought it was because next didn't recognize the part read by nextInt.

... But I don't know if this is there, so if anyone knows it, I would be very grateful if you could tell me ...! !! !! !! !! !! !! !! !! !! !! !! !!

Recommended Posts

Note: next ・ nextLine (paiza learning Java introduction 9: # 06)
Java learning (0)
[Java] Introduction
Deep Learning Java from scratch Chapter 1 Introduction
Learning for the first time java [Introduction]
For JAVA learning (2018-03-16-01)
Ruby Learning # 1 Introduction
Quick learning Java "Introduction?" Part 1 Building an environment
Java learning day 5
Difference between next () and nextLine () in Java Scanner
[Java] Introduction to Java
Introduction to java
java learning day 2
java learning day 1
Quick learning Java "Introduction?" Part 2 Let's write the process
Quick learning Java "Introduction?" Part 3 Talking away from programming
Java Performance Chapter 1 Introduction
Java abstract modifier [Note]
[Java] Internal Iterator Note
Java learning 2 (learning calculation method)
java learning (conditional expression)
Java learning memo (method)
Java Learning (1)-Hello World
Java JUnit brief note
[java] Java SE 8 Silver Note
JAVA learning history interface
[Note] Java: String survey
Java learning memo (basic)
My Study Note (Java)
Java learning memo (interface)
Introduction to java command
Java learning memo (inheritance)
java: Add date [Note]
Introduction to Effective java by practicing and learning (Builder pattern)