Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.  
  8. public static String[][] genFile() throws FileNotFoundException {
  9.  
  10. Scanner in = new Scanner(System.in);
  11.  
  12. System.out.println("Enter the file name: ");
  13.  
  14. String fileName = in.nextLine();
  15.  
  16. /*Create an ArrayList of String from the file name entered*/
  17.  
  18. ArrayList<String> allLines = readfile(fileName);
  19.  
  20. /*Create a 2D array that will hold all the information*/
  21.  
  22. String[][] result = new String[allLines.size() - 1][4];
  23.  
  24. /*This loop should run and capture all the data*/
  25.  
  26. int i = 1;
  27.  
  28. while (i < allLines.size()) {
  29.  
  30. /*Each line of the file should be put into the 2D array result*/
  31.  
  32. String[] line = allLines.get(i).split(",");
  33.  
  34. for (int j = 0; j < line.length; j++)
  35.  
  36. result[i - 1][j] = line[j];
  37.  
  38. i++;
  39.  
  40. }
  41.  
  42. return result;
  43.  
  44. }
  45.  
  46. public static ArrayList<String> readfile(String filename) throws FileNotFoundException {
  47.  
  48. File temp = new File(filename);
  49.  
  50. /*Create a Scanner object to read in our file */
  51.  
  52. Scanner iFile = new Scanner(temp);
  53.  
  54. ArrayList<String> result = new ArrayList<>();
  55.  
  56.  
  57.  
  58. /*Keep reading in each line of the file into the ArrayList*/
  59.  
  60. while (iFile.hasNextLine()) {
  61.  
  62. /*Add each line to the ArrayList*/
  63.  
  64. result.add(iFile.nextLine());
  65.  
  66. }
  67.  
  68. iFile.close();
  69.  
  70. return result;
  71.  
  72. }
  73.  
  74. public static void main(String[] args) throws FileNotFoundException {
  75.  
  76. String[][] g = genFile();
  77.  
  78. System.out.println("\nSecond row: ");
  79.  
  80. /*Print out the second row with info in the file*/
  81.  
  82. for (int i = 0; i < g[0].length; i++) {
  83.  
  84. System.out.printf("%s ", g[1][i]);
  85.  
  86. }
  87.  
  88. /*Print out Fri 4/3/2015 do not hard code*/
  89.  
  90. for (int i = 0; i < g.length - 1; i++) {
  91.  
  92. if (g[i][0].contains("Fri"))
  93.  
  94. System.out.println("\n\nDate: " + g[i][0]);
  95.  
  96. }
  97.  
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement