Skip to content

Instantly share code, notes, and snippets.

@shalk
Created June 27, 2018 01:59
Show Gist options
  • Save shalk/c84f1a2f599cc35557becb1c4a3c9f5d to your computer and use it in GitHub Desktop.
Save shalk/c84f1a2f599cc35557becb1c4a3c9f5d to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class Add {
public static void main(String[] args) throws IOException {
BufferedReader reader1 = new BufferedReader(new FileReader("1.txt"));
BufferedReader reader2 = new BufferedReader(new FileReader("2.txt"));
//BufferedWriter writer = new BufferedWriter(new FileOutputStream("3.txt"));
while(true) {
int sum = 0;
String line1 = reader1.readLine();
String line2 = reader2.readLine();
if (line1 == null && line2 == null) {
break;
}
if (line1 != null) {
sum += Integer.valueOf(line1);
}
if (line2 != null) {
sum += Integer.valueOf(line2);
}
System.out.println(sum);
}
reader1.close();
reader2.close();
}
}
@shalk
Copy link
Author

shalk commented Jun 27, 2018

import java.io.*;
import java.util.*;

public class Add {
    public static void main(String[] args) throws IOException {
        Scanner in1 = new Scanner(new FileReader("1.txt"));
        Scanner in2 = new Scanner(new FileReader("2.txt"));
        //BufferedWriter writer = new BufferedWriter(new FileOutputStream("3.txt"));
        while(in1.hasNext() || in2.hasNext()) {
            int sum = 0;
            if (in1.hasNext()) {
                sum += Integer.valueOf(in1.nextLine());
            }
            if (in2.hasNext()) {
                sum += Integer.valueOf(in2.nextLine());
            }
            System.out.println(sum);

        }
        in1.close();
        in2.close();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment