Results 1 to 3 of 3
Thread: Reading a file into 5 arrays
- 10-29-2011, 06:05 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
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;
}
}
- 10-29-2011, 07:34 PM #2
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
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 10:18 PM.
- 10-30-2011, 09:07 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Reading txt files and store them in arrays
By NeverHide in forum New To JavaReplies: 5Last Post: 05-20-2010, 08:18 PM -
Reading arrays from different classes without getters and setters
By Psyclone in forum New To JavaReplies: 7Last Post: 02-02-2010, 11:01 AM -
Arrays and File Reading
By DaFlake in forum New To JavaReplies: 3Last Post: 08-12-2009, 05:28 PM -
[SOLVED] how to reading binary file and writing txt file
By tOpach in forum New To JavaReplies: 3Last Post: 05-09-2009, 11:31 PM -
Making arrays by reading user input
By apfroggy0408 in forum New To JavaReplies: 23Last Post: 04-30-2008, 01:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks