从2个文件中读取输入,并按升序写入第三个文件
我有2个包含一系列有序数字的文件,由空格(“”)
隔开。
编写一个程序,该程序产生第三个文件,该文件将包含数字上升序列。解决时,不允许您使用任何类型的集合。
文件1:1 18 40 100
文件2:0 10 15 80 1001
我设法将数字转换为string
,但是在输出文件中,我只有2个第一个数字:> 0 1
FileWriter outputFile;
Scanner sc1 = null;
Scanner sc2 = null;
try {
sc1 = new Scanner(new FileReader("Numbers1.txt"));
sc2 = new Scanner(new FileReader("Numbers2.txt"));
outputFile = new FileWriter("NumbersMerge.txt");
int c = sc1.nextInt();
int d = sc2.nextInt();
while (sc1.hasNext() && sc2.hasNext()) {
if (c < d) {
outputFile.write(Integer.toString(c));
sc1.nextLine();
} else if (c > d) {
outputFile.write(Integer.toString(d));
sc2.nextLine();
} else {
outputFile.write(Integer.toString(c));
outputFile.write(Integer.toString(d));
sc1.nextLine();
sc2.nextLine();
}
}
if (sc1.hasNext()) {
outputFile.write(Integer.toString(c));
sc1.nextLine();
}
if (sc2.hasNext()) {
outputFile.write(Integer.toString(d));
sc2.nextLine();
}
outputFile.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sc1 != null && sc2 != null) {
sc1.close();
sc2.close();
}
}
I have 2 files that each contain a series of ordered numbers, separated by spaces (" ")
.
Write a program that produces a third file that will contain the ascending sequence of numbers. When solving, you are not allowed to use any type of collection.
File 1: 1 18 40 100
File 2: 0 10 15 80 1001
I managed to convert the number to String
, but in the output file I've got the only 2 first numbers sorted : 0 1
FileWriter outputFile;
Scanner sc1 = null;
Scanner sc2 = null;
try {
sc1 = new Scanner(new FileReader("Numbers1.txt"));
sc2 = new Scanner(new FileReader("Numbers2.txt"));
outputFile = new FileWriter("NumbersMerge.txt");
int c = sc1.nextInt();
int d = sc2.nextInt();
while (sc1.hasNext() && sc2.hasNext()) {
if (c < d) {
outputFile.write(Integer.toString(c));
sc1.nextLine();
} else if (c > d) {
outputFile.write(Integer.toString(d));
sc2.nextLine();
} else {
outputFile.write(Integer.toString(c));
outputFile.write(Integer.toString(d));
sc1.nextLine();
sc2.nextLine();
}
}
if (sc1.hasNext()) {
outputFile.write(Integer.toString(c));
sc1.nextLine();
}
if (sc2.hasNext()) {
outputFile.write(Integer.toString(d));
sc2.nextLine();
}
outputFile.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sc1 != null && sc2 != null) {
sc1.close();
sc2.close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码的主要问题是您正在阅读和对每个文件中的前两个数字进行排序。之后,您正在阅读剩余的内容
string
withnextline()
,而不是使用nextint()
读取下一个int
。此外,每个erenine()
读取的内容也被丢弃,因为它甚至没有分配给任何变量。您要做的是分别替换每个
nextline()
和hasnext()
用nextint()
and hasnextint()< /代码>。附带说明,代码> 类,不仅是基本filewriter
来写int
值更有帮助。The main problem with your code is that you're reading and sorting only the first two numbers from each file. After that, you're reading the remaining content a
String
withnextLine()
, instead of usingnextInt()
to read the nextint
. Furthermore, the content read by eachreaLine()
is also discarded, as it's not even assigned to any variable.What you want to do is to respectively replace each
nextLine()
andhasNext()
withnextInt()
andhasNextInt()
. As a side note, thePrintWriter
class would be more helpful than just a basicFileWriter
to writeint
values.