Reputation: 13
i am trying to type some alphabet into two different arrs and then print some result message ,but i have to type 6 times and my result is the last twice
String str1[] =new String[100];
String str2[]=new String[100];
int count=0;
while(true) {
Scanner s1=new Scanner(System.in);
if(s1.nextLine()=="END") {
break;
}
str1[count]=s1.nextLine();
Scanner s2=new Scanner(System.in);
if(s2.nextLine()=="END") {
break;
}
str2[count]=s2.nextLine();
count++;
System.out.println("完成第" + count + "个依赖" + " " + s1.nextLine() + "->" + s2.nextLine());
}
Upvotes: 0
Views: 37
Reputation: 201487
Every time you call nextLine
you get a new line (that's why you must type everything so many times, you aren't saving the values you get). Compare String
instances with .equals
and you only need one Scanner
, don't throw away two per loop iteration. Something like,
Scanner s1 = new Scanner(System.in);
while (true) {
String a = s1.nextLine();
if (a.equals("END")) {
break;
}
str1[count] = a;
String b = s1.nextLine();
if (b.equals("END")) {
break;
}
str2[count] = b;
count++;
System.out.println("完成第" + count + "个依赖" + " " + a + "->" + b);
}
Upvotes: 1