我有一个文本输入,我正在尝试扫描每三个数字。我只扫描每个数字,但不是特别是每隔三个数字
Scanner inFile = new Scanner(new FileReader("budgettracker.txt"));
int counter = 0;
PrintWriter outFile = new PrintWriter ("textout.text");
while(inFile.hasNextInt())
{
counter++;
inFile.nextInt();
}
Scanner inFile1 = new Scanner(new FileReader("budgettracker.txt"));
int a[] = new int[counter];
for (int p = 0; p<counter; p++)
{
if(a[p] == a[0] || a[p] == a[3] || a[p] == a[6])
{
a[p] = inFile1.nextInt();
outFile.print(a[p] + " ");
}
}
outFile.close();
inFile.close();
}
答案 0 :(得分:1)
我不完全确定你的意思。这是你在找什么?
int index = 0;
for (int p = 0; p<counter; p++)
{
if(p%3 == 0) // Every third number
{
a[index] = inFile1.nextInt();
outFile.print(a[index++] + " ");
}else{
inFile1.nextInt(); // Skip if the line isn't a multiple of 3
}
}