Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package edu.wit.cs.comp1000;
  2. import java.io.File;
  3. import java.io.PrintWriter;
  4. import java.util.Scanner;
  5. import java.io.FileNotFoundException;
  6.  
  7. //TODO: document this class
  8. public class PA8a {
  9.  
  10. /**
  11. * Error to output when a file cannot be opened.
  12. */
  13. static final String E_NOT_FOUND = "Error! File not found!";
  14.  
  15. /**
  16. * Reads all integers in input scanner,
  17. * outputs positive ones to output each on
  18. * its own line
  19. *
  20. * @param input input source
  21. * @param output output destination
  22. */
  23. public static void process(Scanner input, PrintWriter output) {
  24. String unSplitVal = "";
  25.  
  26. while(input.hasNext())
  27. {
  28. unSplitVal = input.next();
  29. String[] splitVal = unSplitVal.split(" ");
  30. for(int i = 0;i<splitVal.length;i++)
  31. {
  32. if(splitVal[i].charAt(0) == '-')
  33. {
  34.  
  35. }
  36. else
  37. output.println(splitVal[i]);
  38. }
  39. }
  40.  
  41. // TODO: write your code here
  42. }
  43.  
  44. /**
  45. * Program execution point:
  46. * input an input file name and an output file name,
  47. * for each positive number in the input file
  48. * print on its own line to the output file
  49. *
  50. * @param args command-line arguments (ignored)
  51. */
  52. public static void main(String[] args) {
  53. Scanner s = new Scanner(System.in);
  54. System.out.printf("Enter the name of the input file: ");
  55. String inputName = s.next();
  56. System.out.printf("Enter the name of the output file: ");
  57. String outputName = s.next();
  58. /**
  59. * it attempts to make a scanner with the inputName, and then it attempts
  60. * to make a printWriter with the output to test if they both exist.
  61. * if successful it does what is inside of the brackets of the try statement
  62. */
  63. try(Scanner fin = new Scanner(new File(inputName));
  64. PrintWriter fout = new PrintWriter(new File(outputName));
  65. ){
  66. process(fin,fout);
  67.  
  68. }
  69. catch(FileNotFoundException ex)
  70. {
  71. System.out.printf("%s%n", E_NOT_FOUND);
  72. System.exit(0);
  73. }
  74. // TODO: write your code here
  75. }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement