-
Having trouble splitting a text file into multiple arrays.
Hey all, working on trying to read in a text file so that I can split it into multiple arrays. Text file looks like this
Code :
Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48
The name, the student id, and a set of their 4 grades must be split into seperate arrays so that they can be used later to calculate avg's and so on. I'm having trouble getting them to go into different arrays.
This is what I have so far.
Code :
import java.io.*;
public class Program2 {
public static void main(String[] args) throws IOException {
File dataFile = new File("G:\\COSC 1337\\Handouts 6-14\\Student Data.txt");
BufferedReader br = new BufferedReader(new FileReader(dataFile));
String str;
String[] names = new String[5];
int[] grades = new int[20];
String[] studentID = new String[20];
for (int i = 0; i < 5; i++) {
str = br.readLine();
names[i] = str;
str = br.readLine();
studentID[i] = str;
str = br.readLine();
grades[i] = Integer.parseInt(str);
}
I keep getting a numberFormatexception, I assume from trying to parse the string for grades... but I'm unsure how to go about getting all the grades into an array, if everything else is only strings.
-
Re: Having trouble splitting a text file into multiple arrays.
The line with the grades has more than one number on it with spaces between the numbers. parseInt() does not like those spaces and throws the exception.
How are you going to store those four numbers? Initially you could store the whole String as you do with the other lines. Do you know about two dimensional arrays? This would be a place for one.
-
Re: Having trouble splitting a text file into multiple arrays.
It's not very clear in the assignment but it's listed below. I assume I have to save each set of 4 grades into it's own little box, and calculate an avg from that...but it seems very difficult. We've only learned about one dimensional arrays so far.
Code :
Create ONE 1-D Array to hold a set of four test scores for five students.
Create parallel arrays to hold student names, student id, average
score, and a letter grade based on standard grade scale.
Populate the arrays from the file Student Data.txt
Write a program to do the following tasks:
1. Calculate average score for each student.
2. Assign letter grade for each student.
3. Display name, id, scores, average, and grade for each student.
4. calculate the class average score and display.
-
Re: Having trouble splitting a text file into multiple arrays.
Do you have a technique for getting the each of the four grades from the line that they are on?
-
Re: Having trouble splitting a text file into multiple arrays.
You can use the split method on the String containing the numbers in order to get an array of all the numbers. Also, you may want to store each array of numbers in a List as an alternative.
-
Re: Having trouble splitting a text file into multiple arrays.
What technique to use depends on what the OP knows and/or what the assignment requires.
-
Re: Having trouble splitting a text file into multiple arrays.
Quote:
Originally Posted by
Norm
Do you have a technique for getting the each of the four grades from the line that they are on?
I was going to use what he stated above. I've used the split method, but was going to create a integer array and just hold all the grades in each of their elements. Then when it comes to calculating avg, I was going to just take array[0] to [3] on each person and add those and divide by 4. I think. easier said then done.
-
Re: Having trouble splitting a text file into multiple arrays.
After using split() you will need to convert the Strings to int values
-
Re: Having trouble splitting a text file into multiple arrays.
I'm having trouble breaking the 4 sets of numbers apart so that I can parse them into ints.
Code :
import java.io.*;
public class Program2 {
public static void main(String[] args) throws IOException {
File dataFile = new File("G:\\COSC 1337\\Handouts 6-14\\Student Data.txt");
BufferedReader br = new BufferedReader(new FileReader(dataFile));
String str;
String[] placeHolder = new String[5];
String[] names = new String[5];
int[] grades = new int[20];
String[] studentID = new String[20];
for (int i = 0; i < 5; i++) {
str = br.readLine(); //read first line
names[i] = str; // put data into name array
str = br.readLine(); // read second line
studentID[i] = str; // put data into studentID array
str = br.readLine(); //read third line
str.split(" ");
Integer.parseInt(str);
}
}
}
This is what I came up with... but it's trying to parse a set of 4 numbers still. How can I get it to parse each individual number if it's reading in the whole line at once?
-
Re: Having trouble splitting a text file into multiple arrays.
Do it in separate steps:
1) read the line with the numbers into a String
2) split the String into parts. The split() method returns an array: See the String class's API doc.
3) parse each String to an int using a loop thru the array elements
-
Re: Having trouble splitting a text file into multiple arrays.
Step 2 is what I'm having trouble with. I understand that once I do the command str.split(" "); it splits the numbers into seperate entities which I do placeHolder = str.split(" ");, but when I do a println on placeHolder[i] to check what the array is... I get random numbers picked from the data.
When I do the split method and it returns an array... what type of array is it returning? I think my misunderstanding is what the array looks like. If I have the first line of numbers (97 86 78 95) split by the method and then kept in placeHolder "array" ... are all the numbers stored in one element? Because I thought that it's taking each individual number and putting it into each element.
-
Re: Having trouble splitting a text file into multiple arrays.
Please post the code and what is printed out.
A good way to format the contents of an array for printing is to use the Arrays class's toString() method:
Code :
System.out.println(Arrays.toString(<arraynamehere>));
-
Re: Having trouble splitting a text file into multiple arrays.
Code :
import java.io.*;
public class Program2 {
public static void main(String[] args) throws IOException {
File dataFile = new File("G:\\COSC 1337\\Handouts 6-14\\Student Data.txt");
BufferedReader br = new BufferedReader(new FileReader(dataFile));
String str = new String();
String[] placeHolder = new String[5];
String[] names = new String[5];
int[] grades = new int[20];
String[] studentID = new String[20];
String[] setsOfGrades = new String[5];
for (int i = 0; i < 5; i++) {
str = br.readLine(); //read first line
names[i] = str; // put data into name array
str = br.readLine(); // read second line
studentID[i] = str; // put data into studentID array
str = br.readLine(); //read third line
placeHolder = str.split(" ");
for (int j = 0; j < placeHolder.length; j++) {
grades[i] = Integer.parseInt(placeHolder[j]);
}
}
for(int i=0; i<grades.length; i++)
{
System.out.println(grades[i]);
}
}
}
OUTPUT
Code :
95
87
77
67
48
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
It's just picking random numbers to save and I'm not sure why.
-
Re: Having trouble splitting a text file into multiple arrays.
Print out the value of the String: str to see what is there to be split.
Check the indexes you are using in the arrays. How many elements does the array placeHolder have? How many does grades have? Why are they different sizes?
-
Re: Having trouble splitting a text file into multiple arrays.
I printed out the String str and it of course prints the first four correct grades that should be read in. It prints out "97 86 78 95", which it should. Everything seems to go wrong when I do the split. The reason I put the array placeHolder to have and index of 5 is because I thought it was saving each SET of 4 grades into one element...hence I put an index of 5. What I can't figure out... is the splitting of the group of 4 numbers... that's what has me confused. I printed out the placeHolder array using your toString method and it just prints out
Code :
[Ljava.lang.String;@19821f
[Ljava.lang.String;@addbf1
[Ljava.lang.String;@42e816
[Ljava.lang.String;@9304b1
[Ljava.lang.String;@190d11
-
Re: Having trouble splitting a text file into multiple arrays.
Please post the code that printed out what you posted in post#15.
Quote:
saving each SET of 4 grades into one element
No, each element in an array holds one value.
Did you compare the numbers you printed with the numbers that are in the input file?
See if there is a pattern between the two.
-
Re: Having trouble splitting a text file into multiple arrays.
Code :
import java.io.*;
public class Program2 {
public static void main(String[] args) throws IOException {
File dataFile = new File("G:\\COSC 1337\\Handouts 6-14\\Student Data.txt");
BufferedReader br = new BufferedReader(new FileReader(dataFile));
String str = new String();
String[] placeHolder = new String[21];
String[] names = new String[5];
int[] grades = new int[5];
String[] studentID = new String[20];
String[] setsOfGrades = new String[5];
for (int i = 0; i < 5; i++) {
str = br.readLine(); //read first line
names[i] = str; // put data into name array
str = br.readLine(); // read second line
studentID[i] = str; // put data into studentID array
str = br.readLine(); //read third line
placeHolder = str.split(" ");
System.out.println(placeHolder.toString());
for (int j = 0; j < placeHolder.length; j++) {
grades[i] = Integer.parseInt(placeHolder[j]);
}
}
for(int i=0; i<grades.length; i++)
{
//System.out.println(grades[i]);
}
}
}
-
Re: Having trouble splitting a text file into multiple arrays.
Where is the code that I showed you to use to format the array for printing?
You were to copy the code and replace: <arraynamehere> with the name of the array you wanted to format for printing.
-
Re: Having trouble splitting a text file into multiple arrays.
Ah, okay, However, when I used that line it marked Arrays as red and could not find the symbol.
-
Re: Having trouble splitting a text file into multiple arrays.
You were to copy the code in post#12 and replace: <arraynamehere> with the name of the array you wanted to format for printing.
-
Re: Having trouble splitting a text file into multiple arrays.
Yes, I have done that...but it keeps telling me it cannot find the symbol with red lining under Arrays. (I'm using Netbeans btw.) Sorry for being confusing. Trying my best to figure out these errors/logic.
-
Re: Having trouble splitting a text file into multiple arrays.
When you get that message when using a java class, you need to go to the API doc and find what package the class is in and add an import statement for it so the compiler can find the class's definitiom. The Arrays class is in the java.util package.
Also think through how you are going to put all of the students' grades in one array. It's possible but your code will have to handle it.