Make a new project and class in Eclipse called R15, with a main method.
Set up the Run Configuration so that R15 has the program arguments:
input.txt output.txt
This is the same as reading command line arguments from the terminal.
Here's some code that provides you some examples. It's not meant to be run as a program. You can add this code to your R15 somewhere and comment it out so you have it as a reference:
// Example of reading a file try { // make sure TA explains this line and the try/catch block Scanner fileReader = new Scanner(new File(args[0])); // Examples: (not meant to be run as a program) String example1a = fileReader.next(); String example1b = fileReader.nextLine(); double example2 = fileReader.nextDouble(); int example3 = fileReader.nextInt(); char example4 = fileReader.next().charAt(0); if (fileReader.hasNext()) { // I can grab a string token safely String example5 = fileReader.next(); } if (fileReader.hasNextDouble()) { // I can grab a double safely double example6 = fileReader.nextDouble(); } if (fileReader.hasNextInt()) { // I can grab a int safely int example7 = fileReader.nextInt(); } fileReader.close(); } catch (FileNotFoundException e) { System.out.println("ERROR!"); System.exit(0); } // Example of writing to a file try { // make sure TA explains this line and the try/catch block PrintWriter fileOutput = new PrintWriter(new File(args[1])); // Examples: fileOutput.println("Hey..."); fileOutput.print("I've seen..."); fileOutput.print("this stuff before.\n"); fileOutput.printf("%.3f\n", 3.456789); // Important! Save the file fileOutput.close(); } catch (FileNotFoundException e) { System.out.println("ERROR!"); System.exit(0); }
The TA will explain the lines that create the Scanner and PrintWriter and why we need a try-catch (and what that is).
public static void readFile( String inputFile ){ }
public static void writeFile( String outputFile ){ }
Within the main method, call the readFile method with the first command line argument (args[0]),
then call the writeFile method with the second command line argument (args[1]).
stop
(uppercase or lowercase) by itself on its own line, the method
should terminate.
Enter text:
Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!
stop
Hello! Here are some words.
stop can be written here, but it still makes it in.
so will this stop.
stop!
public static double computeAreaCircle( double radius ){ }
public static double computeAreaRectangle( double height, double width ){ }
public static double computeAreaTriangle( double height, double base ){ }
Math.PI