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"
It became like.
If you change the part of nextLine to next
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