【发布时间】:2019-09-24 19:49:49
【问题描述】:
我正在尝试读取一个文本文件,其中包含不同行上的整数,这些整数已经从最小到最大排序。所述整数必须传输到另一个文本文件,但不能有任何重复,也不能使用任何类型的数组、数组列表、映射、集合或任何其他类型的数据结构。
目前我已经尝试从第一个文本文件中读取数字,并使用一个 while 循环来检查下一个数字是否相似,使用扫描仪。唯一的问题是循环也会获取下一个整数,因此该算法仅在所有数字都重复时才有效。如果有人至少能指出我正确的方向,这会让我很开心,在此先感谢!
示例文本文件一(所有整数都在新行上):5 5 5 5 5 8 8 9 9 9 9 10 10 11
我的输出:5 8 9 10 预期输出:5 8 9 10 11
public static void deduplicateFiles(String inputFileName,String outputFileName){
Scanner scan = null;
try{
scan = new Scanner(new FileInputStream(inputFileName) );
}catch(FileNotFoundException e){
System.out.println(e.getMessage() );
}
PrintWriter writer = null;
try{
writer = new PrintWriter(outputFileName);
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}
while(true){
int firstInt = scan.nextInt();
scan.nextLine();
//if(scan.nextInt() != firstInt)
int counter = 1;
while(scan.nextInt() == firstInt && scan.hasNext() != false){
System.out.println("counter" +counter);
counter++;
scan.nextLine();
}
System.out.println("The integers:" + firstInt);
writer.println(firstInt);
if(scan.hasNext() == false)
break;
}
writer.flush();
writer.close();
}
【问题讨论】: