Breaking down a given data file into seperate arrays
What I am trying/attempting to do it break down a given data file containing
one line of data into seperate arrays (first and last names, gender, and 5
grades). I am successful on the first pass, but on the second i get errored
out. I am first copying the data from my data file (please don't edit or
change it to fix the problem) followed by my program code.
MARY SMITH:F;100,33,90,80,70 BILL WILLIAMS:M;90,98,99,89,88 RALPH
SUMMERS:M;22,33,54,56,78 HOLLY MASTERS:F;70,72,76,80,100 JAMES
WATERMELLON:M;45,54,67,72,71 ANTHONY ROBERTSON:M;80,80,87,78,90 ALICIA
SOMERSET:F;24,67,67,55,90 MARIA TANZELLI:F;55,77,78,54,44 ROBERTA
KINK:F;32,66,90,4,12 MALCOLM MOLE:M;55,76,90,90,87
------------------------------------------
import java.io.*;
public class getData
{
private final static String filePath = "C:\\Documents and
Settings\\Owner\\My Documents\\Bub's
Documents\\Homework\\Java\\FirstAssignment\\DATA314.dat";
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader(filePath);
BufferedReader in = new BufferedReader(fileReader);
String data;
String line = in.readLine();
String temp = null;
String[] fName = new String [10];
String[] lName = new String [10];
char[] gender = new char [10];
int[] score1 = new int [10];
int[] score2 = new int [10];
int[] score3 = new int [10];
int[] score4 = new int [10];
int[] score5 = new int [10];
int arrayNum = 0;
int pos = 0, pos2 = 0, score;
in.close();
while (pos != line.length()) {
//get the first name
pos2 = pos2 + 1;
pos = line.indexOf(" ",pos2);
if (arrayNum != 0)
pos2 = pos2 + 2;
fName[arrayNum] = line.substring(pos2,pos);
//get the last name
pos2 = pos;
pos = line.indexOf(":",pos2);
lName[arrayNum] = line.substring(pos2+1,pos);
//get the gender
pos = line.indexOf(";",pos2);
pos2 = pos;
gender[arrayNum] = line.charAt(pos2-1);
//get the test scores
for(int num = 1; num<=5; num++) {
pos2 = pos + 1;
if (num == 1)
pos = line.indexOf(",",pos2);
else if (num == 5) {
pos = line.indexOf(" ",pos2);
}
else
pos = line.indexOf(",",pos2+1);
temp = line.substring(pos2,pos);
score = Integer.parseInt(temp);
switch (num) {
case 1: score1[arrayNum] = score;
case 2: score2[arrayNum] = score;
case 3: score3[arrayNum] = score;
case 4: score4[arrayNum] = score;
case 5: score5[arrayNum] = score;
}
}
System.out.print(fName[arrayNum] + " ");
System.out.print(lName[arrayNum] + " ");
System.out.print(gender[arrayNum] + " ");
System.out.print(score1[arrayNum] + " ");
System.out.print(score2[arrayNum] + " ");
System.out.print(score3[arrayNum] + " ");
System.out.print(score4[arrayNum] + " ");
System.out.print(score5[arrayNum] +"\n");
arrayNum++;
}
}
}