Reputation: 387
I need to combine two text files into one merged text file. The files only consist of numbers, and the numbers must then be listed in ascending order. I have coded it to do this, however, I can't get it to add the last number and it gives me a numberformatexception error. I believe this is because my last number doesn't have anything to compare it to so I can't add it to the list. I'm not sure how to add the last number when it has nothing to compare itself too (I'm pretty sure I need another if statement i'm just not sure how to do it) and I dont know what the right while statement is, however the program is running correctly with the while statement I used, sans the last number.
public static void main(String[] args)
{
FileReader file1 = null;
FileReader file2 = null;
BufferedReader readfile1 = null;
BufferedReader readfile2 = null;
FileWriter fileout = null;
PrintWriter dataout;
String fname1 = "list1.txt";
String fname2 = "list2.txt";
int md = 0;
int file1num = 0;
int file2num = 0;
String file1str;
String file2str;
try
{
file1 = new FileReader(fname1);
}
catch
(FileNotFoundException xyz)
{
System.out.println("File not found: " + fname1);
System.exit(-1);
}
catch
(IOException abc)
{
System.out.println("IOException: caught");
System.exit(-1);
}
readfile1 = new BufferedReader(file1);
try
{
readfile2 = new FileReader(fname2);
}
catch
(FileNotFoundException xyz)
{
System.out.println("File not found: " + fname2);
System.exit(-1);
}
catch
(IOException abc)
{
System.out.println("IOException: caught");
System.exit(-1);
}
readfile2 = new BufferedReader(file2);
try
{
fileout = new FileWriter("merged.txt");
}
catch(IOException adc)
{
System.out.println("file error");
System.exit(-1);
}
dataout = new PrintWriter(fileout);
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
file2str = file2.readLine();
file2num = Integer.parseInt(file2str);
while(md !=-1)
{
if(file1num<file2num)
{
md=file1num;
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
}
if(file2num<file1num)
{
md=file2num;
file2str = file2.readLine();
file2num = Integer.parseInt(file2str);
}
if(file1num==file2num)
{
md=file1num;
file1str = file1.readLine();
file1num = Integer.parseInt(file1str);
}
}
So I know that after the last int from file 1 is read it will come back with a null, which means that file 2 can't compare itself to anything else, and I believe that is where my problem is coming from. The problem is with the while statement and what is contained in it. Also I can't use any arrays or anything like that, it has to be a simple read the two files, compare, add the lowest number to the merge file.
Example input:
file1:
1
2
3
4
6
8
file2:
3
5
6
8
9
Expected output:
1
2
3
3
4
5
6
6
8
8
9
Upvotes: 2
Views: 3011
Reputation: 2535
you try to use trim()
Integer.parseInt(blabla.trim());
Because your digits have one or more space characters so parse wasn't succeed .
example code :
public static void main(String[] args) throws MalformedURLException, IOException {
File file1 = new File("C:\\Users\\Melih\\Desktop\\file1.txt");
File file2 = new File("C:\\Users\\Melih\\Desktop\\file2.txt");
File output = new File("C:\\Users\\Melih\\Desktop\\file3.txt");
BufferedReader file1Reader = new BufferedReader(new FileReader(file1));
BufferedReader file2Reader = new BufferedReader(new FileReader(file2));
PrintWriter writer = new PrintWriter(output);
String file1Num = file1Reader.readLine();
String file2Num = file2Reader.readLine();
while (file1Num != null && file2Num != null) {
int num1 = Integer.parseInt(file1Num.trim());
int num2 = Integer.parseInt(file2Num.trim());
if (num1 < num2) {
writer.println(num1);
file1Num = file1Reader.readLine();
} else if (num2 < num1) {
writer.println(num2);
file2Num = file2Reader.readLine();
} else {
writer.println(num1);
file1Num = file1Reader.readLine();
}
}
while (file1Num != null) {
writer.println(file1Num);
file1Num = file1Reader.readLine();
}
while (file2Num != null) {
writer.println(file2Num);
file2Num = file2Reader.readLine();
}
file1Reader.close();
file2Reader.close();
writer.close();
}
Upvotes: 0
Reputation: 1270
You'll need to consider what to do when one file, or the other, runs out.
BufferedReader.readLine() returns a null on EOF, so you'll be looking for nulls.
The logic looks something like this
str1 = read file1
num1 = parse( str1 ) //parse() should be a method that can properly handle a null
str2 = read file2 //and return something appropriate, like -1, when null
num2 = parse( str2 ) //is encountered
while str1 != null || str2 != null
if str2 == null || num1 < num2
write num1
str1 = read file1
num1 = parse( str1 )
else if str1 == null || num1 > num2
write num2
str2 = read file2
num2 = parse( str2 )
else if num1 == num2
write num1
write num2
str1 = read file1
num1 = parse( str1 )
str2 = read file2
num2 = parse( str2 )
Upvotes: 2
Reputation: 53535
Assuming you have two lists that hold the Integer values of the first and the second files, if you'll read both files into Lists, say lst1 and lst2 then the merge into lst3 should be easy:
Integer a1 = null;
Integer a2 = null;
while (!lst1.isEmpty() || !lst2.isEmpty()){
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){}
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){}
if(a1 == null && a2 != null) lst3.add(a2);
else if(a1 != null && a2 == null) lst3.add(a1);
else{
while(a1 < a2){
lst3.add(a1);
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){break;}
}
while(a1 >= a2){
lst3.add(a2);
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){break;}
}
while(a1 != null && lst2.isEmpty()){
lst3.add(a1);
try{
a1 = (Integer)lst1.remove(0);
}
catch(Exception e){break;}
}
while(a2 != null && lst1.isEmpty()){
lst3.add(a2);
try{
a2 = (Integer)lst2.remove(0);
}
catch(Exception e){break;}
}
}
}
Then you can write the content of lst3
into the output file.
Disclaimer: I wrote the code using notepad - you'll probably have to "patch" it a bit ;)
Upvotes: 0