は、常にnextLine()を使用して入力を読み取ってから、その文字列を解析します。
next()
を使用すると、スペースの前に来るものだけが返されます。 nextLine()
は、現在の行を返した後に自動的にスキャナを下に移動します。
nextLine()
のデータを解析するのに便利なツールは、str.split("\\s+")
です。
public class Scientist {
private String name;
private String field;
private String greatIdeas;
public static void main(String[] args) {
String scientists[] = new String[100];
int scientistCount = 0;
Scanner input = new Scanner(System.in);
do{
String answer;
System.out.println("Enter the name of the Scientist: ");
scientists[scientistCount]=input.nextLine();
System.out.println(scientistCount);
System.out.println("Would You like to add another Scientist?");
scientistCount++;
}
while(input.nextLine().equalsIgnoreCase("Y"));
input.close();
}
}
正確にどのような問題がありますか? – Berger
私はdo-whileループを利用して配列を記入することができず、最初のループの後に科学者の名前を入力するよう求められなくなりました。 @Berger –