最後一行輸入有問題。由於未完成,我無法強制Scanner從最後一行讀取數字。當我在前一行最後一行復制一行時,沒有任何問題,但是我不能在那裏有額外的行。輸入如何強制掃描儀讀取未完成的行
例子:
2
1 1
9 9
4
4 5
6 5
2 5
3 4
-1
我試過sc.nextInt()
,sc.next()
,sc.nextLine()
,沒有什麼可以到達-1
。即使sc.hasNext
,sc.hasNextInt
等正在等待。
while(true)
{
line = sc.nextLine();
try {
a = Integer.parseInt(line);
}
catch (NumberFormatException e) {
}
if(a == -1)
{
break;
}
rr = new int[a][2];
for(int i = 0; i < a; i++)
{
line = sc.nextLine();
numbers = line.split(" ");
try {
rr[i][0] = Integer.parseInt(numbers[0]);
rr[i][1] = Integer.parseInt(numbers[1]);
}
catch (NumberFormatException e) {
}
}
b = sc.nextInt();
sc.nextLine();
tt = new int[b][2];
for(int i = 0; i < b; i++)
{
line = sc.nextLine();
numbers = line.split(" ");
try {
tt[i][0] = Integer.parseInt(numbers[0]);
tt[i][1] = Integer.parseInt(numbers[1]);
}
catch (NumberFormatException e) {
}
}
sc.nextLine();
}
使用nextLine讀取所有行。解析整數(s)作爲第二步 –
嘗試,不幸的是它沒有工作...可能它等待線結束或什麼 –
試試這個http://stackoverflow.com/questions/5868369/how-to-read-a -large-text-file-line-by-line-using-java should help –