Results 1 to 3 of 3
  1. #1
    mossberg is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Reading a file into 5 arrays

    My LoadFile function will not work. Attempting to read schedules into 5 parallel sub arrays.
    I have attempted a few different methods which are commented out. I have 2 text files saved in the folder. tim and wendy.
    when I attempt to use LoadFile the window disappears for a moment then bring me back to GetUserChoice. however the array remains empty and i receive no compilation error??


    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.*;

    /**
    *
    *
    * purpose: This program is an employee scheduling and maintainance program.
    * It will allow the user to load an employees schedule from a file.
    * add a schedule entry
    * Display an entire schedule
    * Display a single schedule entry
    * Change a schedule entry
    * and create a new schedule entry
    */

    public class Main {


    public static void main(String[] args) throws IOException {
    char userChoice;
    String[] date;
    String[] start;
    String[] end;
    String[] location;
    String[] comments;
    date = new String[20];
    start = new String[20];
    end = new String[20];
    location = new String[20];
    comments = new String[20];
    int cnt;
    cnt = 0;


    do {
    userChoice = GetUserChoice();
    if (userChoice != 'Q') {
    PerformChoice(userChoice, cnt, date, start, end, location, comments);
    }
    } while (userChoice != 'Q');
    }

    public static char GetUserChoice() throws IOException {
    char userChoice;

    do {
    userChoice = JOptionPane.showInputDialog("What would you like" +
    "to do? (L)oad from File. (S)ave list to file. " +
    "(A)dd a schedule entry. (D)isplay entire Schedule. " +
    "D(I)splay single entry. Create (N)ew Schedule. (Q)uit ?").toUpperCase().charAt(0);
    } while (userChoice != 'L' && userChoice != 'S' && userChoice != 'A' &&
    userChoice != 'D' && userChoice != 'I');

    return userChoice;
    }

    public static void PerformChoice(char userChoice, int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) throws IOException {



    switch (userChoice) {
    case 'L':
    LoadFile(date, start, end, location, comments);
    break;
    case 'S':
    SaveListToFile(cnt, date, start, end, location, comments);
    break;
    case 'A':
    AddScheduleEntry(cnt, date, start, end, location, comments);
    break;
    case 'D':
    DisplayEntireSchedule(cnt, date, start, end, location, comments);
    break;
    case 'I':
    DisplaySingleEntry(cnt, date, start, end, location, comments);
    break;
    case 'N':
    CreateNewList(cnt);
    break;


    }
    }

    public static int LoadFile(String[] date, String[] start, String[] end, String[] location, String[] comments) throws IOException {
    File inFile;
    Scanner inFileSc;
    int cnt;
    cnt = 0;
    String date1, start1, end1, location1, comments1;
    inFile = new File("wendy.txt");
    inFileSc = new Scanner(inFile);
    //String fileName;
    //fileName = "null";
    //fileName = JOptionPane.showInputDialog("What is the employee/filename" +
    //"you would like to load?");

    while (inFileSc.hasNext()&& cnt < 20) {
    //date[cnt] = inFileSc.nextLine();
    //start[cnt] = inFileSc.nextLine();
    //end[cnt] = inFileSc.nextLine();
    //location[cnt] = inFileSc.nextLine();
    //comments[cnt] = inFileSc.nextLine();
    date1 = inFileSc.nextLine();
    start1 = inFileSc.nextLine();
    end1 = inFileSc.nextLine();
    location1 = inFileSc.nextLine();
    comments1 = inFileSc.nextLine();
    date[cnt] = date1;
    start[cnt] = start1;
    end[cnt] = end1;
    location[cnt] = location1;
    comments[cnt] = comments1;
    cnt++;
    }
    inFileSc.close();
    return cnt;
    }

    public static void SaveListToFile(int cnt, String[] date, String[] start,
    String[] end, String[] location, String[] comments) throws IOException {

    String fileName;
    int saveCnt;
    FileWriter outFileFW;
    PrintWriter outFile;
    Scanner kbd = new Scanner(System.in);
    saveCnt = 0;


    System.out.println("what is the name of the file you would like to write to?");
    fileName = kbd.nextLine();
    outFileFW = new FileWriter(fileName, true);
    outFile = new PrintWriter(outFileFW);

    for (saveCnt = 0; saveCnt < cnt; saveCnt++) {
    outFile.println(date[saveCnt]);
    outFile.println(start[saveCnt]);
    outFile.println(end[saveCnt]);
    outFile.println(location[saveCnt]);
    outFile.println(comments[saveCnt]);
    }
    outFile.close();


    }

    public static int AddScheduleEntry(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
    int userChoice;
    Scanner kbd = new Scanner(System.in);

    do {
    System.out.println("Enter the Date of the Job: Format(01/10/11)");
    date[cnt] = kbd.nextLine();
    System.out.println("Enter the start time of the Job: Format(09:00:AM)");
    start[cnt] = kbd.nextLine();
    System.out.println("Enter the end time of the Job: Format(05:00:PM)");
    end[cnt] = kbd.nextLine();
    System.out.println("Enter the location of the Job: Format(Watertown)");
    location[cnt] = kbd.nextLine();
    System.out.println("Enter the Comments of the Job: Format(Bring Hammer)");
    comments[cnt] = kbd.nextLine();
    cnt++;
    System.out.println("Would you like to: (1)Add another? or (2)Go back to" +
    "the main menu?");
    userChoice = kbd.nextInt();
    } while (userChoice < 2);
    return cnt;
    }

    public static void DisplayEntireSchedule(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
    int dispCnt;
    dispCnt = 0;

    for (dispCnt = 0; dispCnt < cnt; dispCnt++) {
    System.out.println("====Schedule " + dispCnt + "====");
    System.out.println("#" + dispCnt + ". Date: " + date[dispCnt]);
    System.out.println("Start time: " + start[dispCnt]);
    System.out.println("End time: " + end[dispCnt]);
    System.out.println("Location: " + location[dispCnt]);
    System.out.println("Comments: " + comments[dispCnt]);

    }

    }

    public static void DisplaySingleEntry(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
    int dispCnt;
    dispCnt = 0;
    Scanner kbd = new Scanner(System.in);

    System.out.println("Enter the Schedule number you would like to view: ?" +
    " 0-" + cnt);
    dispCnt = kbd.nextInt();

    System.out.println("====Schedule " + dispCnt + "====\n");
    System.out.println("#" + dispCnt + ". Date: " + date[dispCnt] + "\n");
    System.out.println(" Start time: " + start[dispCnt] + "\n");
    System.out.println(" End Time: " + end[dispCnt] + "\n");
    System.out.println(" Location: " + location[dispCnt] + "\n");
    System.out.println(" Comments: " + comments[dispCnt] + "\n");
    }

    public static int CreateNewList(int cnt) {
    cnt = 0;
    return cnt;
    }
    }

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default Re: Reading a file into 5 arrays

    First off, please always use code tags: [code][/code] around your code.

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    import java.io.*;
    
    /**
     *
     *
     * purpose: This program is an employee scheduling and maintainance program.
     * It will allow the user to load an employees schedule from a file.
     * add a schedule entry
     * Display an entire schedule
     * Display a single schedule entry
     * Change a schedule entry
     * and create a new schedule entry
     */
    
    public class Main {
    
       
        public static void main(String[] args) throws IOException {
            char userChoice;
            String[] date;
            String[] start;
            String[] end;
            String[] location;
            String[] comments;
            date = new String[20];
            start = new String[20];
            end = new String[20];
            location = new String[20];
            comments = new String[20];
            int cnt;
            cnt = 0;
    
    
            do {
                userChoice = GetUserChoice();
                if (userChoice != 'Q') {
                    PerformChoice(userChoice, cnt, date, start, end, location, comments);
                }
            } while (userChoice != 'Q');
        }
    
        public static char GetUserChoice()  throws IOException {
            char userChoice;
    
            do {
                userChoice = JOptionPane.showInputDialog("What would you like" +
                        "to do? (L)oad from File.  (S)ave list to file. " +
                        "(A)dd a schedule entry. (D)isplay entire Schedule. " +
                        "D(I)splay single entry. Create (N)ew Schedule. (Q)uit ?").toUpperCase().charAt(0);
            } while (userChoice != 'L' && userChoice != 'S' && userChoice != 'A' &&
                    userChoice != 'D' && userChoice != 'I');
    
            return userChoice;
        }
    
        public static void PerformChoice(char userChoice, int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) throws IOException {
    
    
    
            switch (userChoice) {
                case 'L':
                    LoadFile(date, start, end, location, comments);
                    break;
                case 'S':
                    SaveListToFile(cnt, date, start, end, location, comments);
                    break;
                case 'A':
                    AddScheduleEntry(cnt, date, start, end, location, comments);
                    break;
                case 'D':
                    DisplayEntireSchedule(cnt, date, start, end, location, comments);
                    break;
                case 'I':
                    DisplaySingleEntry(cnt, date, start, end, location, comments);
                    break;
                case 'N':
                    CreateNewList(cnt);
                    break;
    
    
            }
        }
    
        public static int [COLOR="#FF0000"]LoadFile[/COLOR](String[] date, String[] start, String[] end, String[] location, String[] comments) throws IOException {
            File inFile;
            Scanner inFileSc;
            int cnt;
            cnt = 0;
            String date1, start1, end1, location1, comments1;
            inFile = new File("wendy.txt");
            inFileSc = new Scanner(inFile);
            //String fileName;
            //fileName = "null";
            //fileName = JOptionPane.showInputDialog("What is the employee/filename" +
              //"you would like to load?");
    
            while (inFileSc.hasNext()&& cnt < 20) {
               //date[cnt] = inFileSc.nextLine();
               //start[cnt] = inFileSc.nextLine();
               //end[cnt] = inFileSc.nextLine();
               //location[cnt] = inFileSc.nextLine();
               //comments[cnt] = inFileSc.nextLine();
                date1 = inFileSc.nextLine();
                start1 = inFileSc.nextLine();
                end1 = inFileSc.nextLine();
                location1 = inFileSc.nextLine();
                comments1 = inFileSc.nextLine();
                date[cnt] = date1;
                start[cnt] = start1;
                end[cnt] = end1;
                location[cnt] = location1;
                comments[cnt] = comments1;
                cnt++;
            }
            inFileSc.close();
            return cnt;
        }
    
        public static void SaveListToFile(int cnt, String[] date, String[] start,
           String[] end, String[] location, String[] comments) throws IOException {
    
            String fileName;
            int saveCnt;
            FileWriter outFileFW;
            PrintWriter outFile;
            Scanner kbd = new Scanner(System.in);
            saveCnt = 0;
    
    
            System.out.println("what is the name of the file you would like to write to?");
            fileName = kbd.nextLine();
            outFileFW = new FileWriter(fileName, true);
            outFile = new PrintWriter(outFileFW);
    
            for (saveCnt = 0; saveCnt < cnt; saveCnt++) {
                outFile.println(date[saveCnt]);
                outFile.println(start[saveCnt]);
                outFile.println(end[saveCnt]);
                outFile.println(location[saveCnt]);
                outFile.println(comments[saveCnt]);
            }
            outFile.close();
    
    
        }
    
        public static int AddScheduleEntry(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
            int userChoice;
            Scanner kbd = new Scanner(System.in);
    
            do {
                System.out.println("Enter the Date of the Job: Format(01/10/11)");
                date[cnt] = kbd.nextLine();
                System.out.println("Enter the start time of the Job: Format(09:00:AM)");
                start[cnt] = kbd.nextLine();
                System.out.println("Enter the end time of the Job: Format(05:00:PM)");
                end[cnt] = kbd.nextLine();
                System.out.println("Enter the location of the Job: Format(Watertown)");
                location[cnt] = kbd.nextLine();
                System.out.println("Enter the Comments of the Job: Format(Bring Hammer)");
                comments[cnt] = kbd.nextLine();
                cnt++;
                System.out.println("Would you like to: (1)Add another? or (2)Go back to" +
                        "the main menu?");
                userChoice = kbd.nextInt();
            } while (userChoice < 2);
            return cnt;
        }
    
        public static void DisplayEntireSchedule(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
            int dispCnt;
            dispCnt = 0;
    
            for (dispCnt = 0; dispCnt < cnt; dispCnt++) {
                System.out.println("====Schedule " + dispCnt + "====");
                System.out.println("#" + dispCnt + ". Date:   " + date[dispCnt]);
                System.out.println("Start time:   " + start[dispCnt]);
                System.out.println("End time:   " + end[dispCnt]);
                System.out.println("Location:   " + location[dispCnt]);
                System.out.println("Comments:   " + comments[dispCnt]);
    
            }
    
        }
    
        public static void DisplaySingleEntry(int cnt, String[] date, String[] start, String[] end, String[] location, String[] comments) {
            int dispCnt;
            dispCnt = 0;
            Scanner kbd = new Scanner(System.in);
    
            System.out.println("Enter the Schedule number you would like to view: ?" +
                    " 0-" + cnt);
            dispCnt = kbd.nextInt();
    
            System.out.println("====Schedule " + dispCnt + "====\n");
            System.out.println("#" + dispCnt + ". Date:   " + date[dispCnt] + "\n");
            System.out.println("    Start time:   " + start[dispCnt] + "\n");
            System.out.println("    End Time:   " + end[dispCnt] + "\n");
            System.out.println("    Location:   " + location[dispCnt] + "\n");
            System.out.println("    Comments:   " + comments[dispCnt] + "\n");
        }
    
        public static int CreateNewList(int cnt) {
            cnt = 0;
            return cnt;
        }
    }
    Last edited by Solarsonic; 10-29-2011 at 11:18 PM.

  3. #3
    mossberg is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Reading a file into 5 arrays

    Did you make a change to the code, or just scold me?

Similar Threads

  1. Reading txt files and store them in arrays
    By NeverHide in forum New To Java
    Replies: 5
    Last Post: 05-20-2010, 09:18 PM
  2. Replies: 7
    Last Post: 02-02-2010, 12:01 PM
  3. Arrays and File Reading
    By DaFlake in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 06:28 PM
  4. Replies: 3
    Last Post: 05-10-2009, 12:31 AM
  5. Making arrays by reading user input
    By apfroggy0408 in forum New To Java
    Replies: 23
    Last Post: 04-30-2008, 02:23 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •