Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package edu.wit.cs.comp1000;
- import java.io.File;
- import java.io.PrintWriter;
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- //TODO: document this class
- public class PA8a {
- /**
- * Error to output when a file cannot be opened.
- */
- static final String E_NOT_FOUND = "Error! File not found!";
- /**
- * Reads all integers in input scanner,
- * outputs positive ones to output each on
- * its own line
- *
- * @param input input source
- * @param output output destination
- */
- public static void process(Scanner input, PrintWriter output) {
- String unSplitVal = "";
- while(input.hasNext())
- {
- unSplitVal = input.next();
- String[] splitVal = unSplitVal.split(" ");
- for(int i = 0;i<splitVal.length;i++)
- {
- if(splitVal[i].charAt(0) == '-')
- {
- }
- else
- output.println(splitVal[i]);
- }
- }
- // TODO: write your code here
- }
- /**
- * Program execution point:
- * input an input file name and an output file name,
- * for each positive number in the input file
- * print on its own line to the output file
- *
- * @param args command-line arguments (ignored)
- */
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in);
- System.out.printf("Enter the name of the input file: ");
- String inputName = s.next();
- System.out.printf("Enter the name of the output file: ");
- String outputName = s.next();
- /**
- * it attempts to make a scanner with the inputName, and then it attempts
- * to make a printWriter with the output to test if they both exist.
- * if successful it does what is inside of the brackets of the try statement
- */
- try(Scanner fin = new Scanner(new File(inputName));
- PrintWriter fout = new PrintWriter(new File(outputName));
- ){
- process(fin,fout);
- }
- catch(FileNotFoundException ex)
- {
- System.out.printf("%s%n", E_NOT_FOUND);
- System.exit(0);
- }
- // TODO: write your code here
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement