最後の入力行に問題があります。 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で読み込みます。 2番目のステップとして整数を解析してください –
試みましたが、残念ながらそれは仕事をしませんでした。おそらく、行末や何かを待ちます。 –
これを試してみようhttp://stackoverflow.com/questions/5868369/how-to-read-a -large-text-file-line-using-javaは助けてください –