Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Main {
- public static String[][] genFile() throws FileNotFoundException {
- Scanner in = new Scanner(System.in);
- System.out.println("Enter the file name: ");
- String fileName = in.nextLine();
- /*Create an ArrayList of String from the file name entered*/
- ArrayList<String> allLines = readfile(fileName);
- /*Create a 2D array that will hold all the information*/
- String[][] result = new String[allLines.size() - 1][4];
- /*This loop should run and capture all the data*/
- int i = 1;
- while (i < allLines.size()) {
- /*Each line of the file should be put into the 2D array result*/
- String[] line = allLines.get(i).split(",");
- for (int j = 0; j < line.length; j++)
- result[i - 1][j] = line[j];
- i++;
- }
- return result;
- }
- public static ArrayList<String> readfile(String filename) throws FileNotFoundException {
- File temp = new File(filename);
- /*Create a Scanner object to read in our file */
- Scanner iFile = new Scanner(temp);
- ArrayList<String> result = new ArrayList<>();
- /*Keep reading in each line of the file into the ArrayList*/
- while (iFile.hasNextLine()) {
- /*Add each line to the ArrayList*/
- result.add(iFile.nextLine());
- }
- iFile.close();
- return result;
- }
- public static void main(String[] args) throws FileNotFoundException {
- String[][] g = genFile();
- System.out.println("\nSecond row: ");
- /*Print out the second row with info in the file*/
- for (int i = 0; i < g[0].length; i++) {
- System.out.printf("%s ", g[1][i]);
- }
- /*Print out Fri 4/3/2015 do not hard code*/
- for (int i = 0; i < g.length - 1; i++) {
- if (g[i][0].contains("Fri"))
- System.out.println("\n\nDate: " + g[i][0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement