很抱歉点击诱饵标题,但这是我的问题,我无法在不丢失问题的情况下改变措辞。
我有以下代码,用于选择文件,阅读并找到它的模式,我想我已经完成了,但我有一个问题
public class ModeFinder
{
public static int countDoubles(File file) throws FileNotFoundException
{
Scanner reader = new Scanner(file);
int count = 0;
while (reader.hasNextDouble())
{
count++;
reader.nextDouble();
}
reader.close();
return count;
}
public static void main(String args[]) throws FileNotFoundException
{
String filename;
FileDialog filePicker = new FileDialog(new JFrame());
filePicker.setVisible(true);
filename = filePicker.getFile();
String folderName = filePicker.getDirectory();
filename = folderName + filename;
System.out.println("filename = " +filename);
File inputFile = new File(filename);
Scanner fileReader = new Scanner (inputFile);
int maxValue = 0,
maxCount = 0;
int[] a = new int[countDoubles(inputFile)];
while (fileReader.hasNextInt())
{
for (int i = 0; i < a.length; i++)
{
int count = 0;
for (int j = 0; j < a.length; j++)
{
if (a[j] == a[i])
count++;
}
if (count > maxCount)
{
maxCount = count;
maxValue = a[i];
}
}
}
System.out.println("The most common grade is: " +maxValue);
}
}
最常见成绩的最后一点甚至不打印,我也不知道为什么。
答案 0 :(得分:1)
您没有调用nextInt从文件中获取值,因此您的while循环将永远循环。你需要这样的东西:
while (fileReader.hasNextInt())
{
int value = fileReader.nextInt();
...