Problem Statement

Write a program that accepts three files as command line arguments. The first two represent input files, and the third one represents the desired output file. If there aren't three arguments provided, either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program. The output file may be an existing file, since it will be overwritten.

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit. As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

Whaaaa?

Image Credit: Mashable via Giphy

Let's break it down into smaller parts


public class Example{
  public static void main(String[] args){
} }

Write a program that accepts three files as command line arguments.

If there aren't three arguments provided, ... print "Invalid Arguments" and exit the program.



public class Example{
  public static void main(String[] args){
    if(args.length != 3){
      System.out.println("Invalid Arguments");
      return;
    }
  }
}

Write a program that accepts three files as command line arguments.

If there aren't three arguments provided, ... print "Invalid Arguments" and exit the program.


public class Example{
  public static void main(String[] args){
    if(args.length != 3){
      System.out.println("Invalid Arguments");
      return;
    }
  }
}

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program



import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program



import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program



import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program




import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program





import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program




import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; } } }

The first two represent input files, and the third one represents the desired output file.

...either input file is not an existing file, or the output file is an existing directory, print "Invalid Arguments" and exit the program







    

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.





while(scanner1.hasNext()){
  String line = scanner1.nextLine().trim();
}
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim();
}

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.





while(scanner1.hasNext()){
  String line = scanner1.nextLine().trim();
  int input = Integer.parseInt(line);
}
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim(); int input = Integer.parseInt(line);
}

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.






while(scanner1.hasNext()){
  String line = scanner1.nextLine().trim();
  int input = Integer.parseInt(line);
}
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim(); int input = Integer.parseInt(line);
}

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.





import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; } } }

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.




import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; }catch(NumberFormatException e){ System.out.println("Invalid Input"); return; } } }

The program should open each input file and read the contents. Each input file will consist of a list of whole numbers, one per line. If there are any errors parsing the contents of either file, the program should print "Invalid Input" and exit.





while(scanner1.hasNext()){
  String line = scanner1.nextLine().trim();
  int input = Integer.parseInt(line);
  if(input % 2 == 0){
    //even
  }else{
    //odd
  }
}
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim(); int input = Integer.parseInt(line); if(input % 2 == 0){ //even }else{ //odd } }

As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.



int countEven = 0;
int countOdd = 0;
int sumEven = 0;
int sumOdd = 0;
while(scanner1.hasNext()){ String line = scanner1.nextLine().trim(); int input = Integer.parseInt(line); if(input % 2 == 0){ countEven += 1; sumEven += input; }else{ countOdd += 1; sumOdd += input; } }
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim(); int input = Integer.parseInt(line); if(input % 2 == 0){ countEven += 1; sumEven += input; }else{ countOdd += 1; sumOdd += input; } }

As the input is read, the program should keep track of both the count and sum of all even inputs and odd inputs.




int countEven = 0;
int countOdd = 0;
int sumEven = 0;
int sumOdd = 0;
while(scanner1.hasNext()){ String line = scanner1.nextLine().trim(); int input = Integer.parseInt(line); if(input % 2 == 0){ countEven += 1; sumEven += input; }else{ countOdd += 1; sumOdd += input; } }
while(scanner2.hasNext()){ String line = scanner2.nextLine().trim(); int input = Integer.parseInt(line); if(input % 2 == 0){ countEven += 1; sumEven += input; }else{ countOdd += 1; sumOdd += input; } }
/* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.




writer.write("" + countEven);
writer.newLine();
writer.write("" + sumEven);
writer.newLine();
writer.write("" + countOdd);
writer.newLine();
writer.write("" + sumOdd);
writer.newLine();

Once all input is read, the program should create the output file and print the following four items, in this order, one per line: number of even inputs, sum of even inputs, number of odd inputs, sum of odd inputs.



writer.write("" + countEven);
writer.newLine();
writer.write("" + sumEven);
writer.newLine();
writer.write("" + countOdd);
writer.newLine();
writer.write("" + sumOdd);
writer.newLine();

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!



writer.write("" + countEven);
writer.newLine();
writer.write("" + sumEven);
writer.newLine();
writer.write("" + countOdd);
writer.newLine();
writer.write("" + sumOdd);
writer.newLine();
System.out.println("Complete");

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!



writer.write("" + countEven);
writer.newLine();
writer.write("" + sumEven);
writer.newLine();
writer.write("" + countOdd);
writer.newLine();
writer.write("" + sumOdd);
writer.newLine();
System.out.println("Complete");

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!



import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedWriter;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.io.IOException;
import java.lang.NumberFormatException;
public class Example{ public static void main(String[] args){ if(args.length != 3){ System.out.println("Invalid Arguments"); return; } try( Scanner scanner1 = new Scanner(Paths.get(args[0])); Scanner scanner2 = new Scanner(Paths.get(args[1])); BufferedWriter writer = Files.newBufferedWriter( Paths.get(args[2])) ){ /* -=-=-=-=- MORE CODE GOES HERE -=-=-=-=- */ }catch(InvalidPathException e){ System.out.println("Invalid Arguments"); return; }catch(NoSuchFileException e){ System.out.println("Invalid Arguments"); return; }catch(IOException e){ System.out.println("Invalid Arguments"); return; }catch(NumberFormatException e){ System.out.println("Invalid Input"); return; } } }

Finally, when the program is done, it should simply print "Complete" and exit. Don't forget to close any open files!

Already taken care of using
Try With Resources