Results 1 to 12 of 12
- 11-11-2012, 11:40 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
array or arraylist for reading file
Hello friends.
I need help for my java homework. I have a data.dat file and have the following informations in it(student name and grades seperated with comma).
I searched for internet alot but I can not get the values one by one to the arraThe method I used is listing the one column completely.
I need to read each line and write into one array. second line to second array.third line to third array etc.
The goal is to make an avarage for each student and write the values to another array.what would you recommend for reeading file?
Name Surname Grd1 Grd2 Grd3
Margaret Burbidge 25 65 82
John White 45 27 56
Amelia Detweiler 54 65 99
My file reading method is;
public static void main(String[] args) throws Exception{
String dataFileName = "D:/data.dat";
BufferedReader bReader = new BufferedReader(
new FileReader(dataFileName));
String line;
while ((line = bReader.readLine()) != null) {
String datavalue[] = line.split(",");
String value1 = datavalue[0];
int value2 = Integer.parseInt(datavalue[1]);
int value3 = Integer.parseInt(datavalue[2]);
int value4 = Integer.parseInt(datavalue[3]);
System.out.println(value1);
- 11-12-2012, 11:49 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: array or arraylist for reading file
Please use
when posting code.Java Code:tags
I'm not sure where you are stuck?
You've read the values in, so is it how to save them to an array?Please do not ask for code as refusal often offends.
- 11-12-2012, 03:28 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: array or arraylist for reading file
Thanks for reply. As you see from my codes I read the values but in column format. I want to read with line format.
I want to make an avarage of the numbers in first line in my data.dat file.
When I say "System.out.println(value1); " in my example it prints out the first column via one variable(value1). I want to read one line(row) to one array.
- 11-12-2012, 04:38 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: array or arraylist for reading file
Can you give an example of how you would expect that data to look after you have processed it?
That might help.
For me, I would start by creating a class called Student that contained attributes representing those 5 fields (firstname, lastname, and 3 grades).Please do not ask for code as refusal often offends.
- 11-13-2012, 10:32 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: array or arraylist for reading file
I want my first array to record first row of my file.So my first array(value1 in my code) should look like value1[0]=Margaret Burbidge, value1[1]=25,value1[2]=65 ,value[3]= 82
So that I can later have an avarage of the numbers of the first row of my data file.The problem is value1 is a variable not an array. When I want to print out the value1 variable,it prints out the first column of my data file.I do not want column I need row.
- 11-13-2012, 11:14 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: array or arraylist for reading file
So declare value1 as an array.
Your problem is going to be the fact that you want to store Strings and ints in the same array, so it would have to be an Object[].
This is bad practice, which is why I suggested creating a class to hold the data from a single row.Please do not ask for code as refusal often offends.
- 11-14-2012, 10:15 AM #7
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: array or arraylist for reading file
I could not declare the value1 as array below in my example, it produces error. I can not understand the logic. The value1 variable has information of the first column of my data file but I can not access each value in value1 variable.Could you please show a small example for accessing each value in the value1 or value2.Thanks.
1|String datavalue[] = line.split(",");
2|String value1 = datavalue[0];
3|int value2 = Integer.parseInt(datavalue[1]);
4|int value3 = Integer.parseInt(datavalue[2]);
5|int value4 = Integer.parseInt(datavalue[3]);
- 11-14-2012, 11:10 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: array or arraylist for reading file
Sorry, this is not making any sense.
You've given an example of the data you are reading in.
I still have no idea what it is you are trying to do with this data.
I do not mean what you are trying to do in that little bit of code above, but what the application is supposed to be doing with this data as a whole.Please do not ask for code as refusal often offends.
- 11-14-2012, 11:53 AM #9
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: array or arraylist for reading file
- 11-14-2012, 11:57 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Re: array or arraylist for reading file
So create a class then.
But, if you have to do this as parallel arrays, then create two arrays, one of Strings and one of ints.
The first holds the names, and the second holds the averages.Please do not ask for code as refusal often offends.
- 11-14-2012, 12:32 PM #11
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: array or arraylist for reading file
Thanks for quick reply. Ok I will create two arrays for printout .But the problem is how to read values in the file and store in arrays.My code reads the file and stores the to the arrays(datavalue[]) but I can not access values inside the arrays(value1,value2,value3,value4) because I can not declare value1,value2,value3,value4 as arrays only I can declare as variable.
- 11-14-2012, 01:59 PM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Reading from txt file to arraylist
By artur in forum New To JavaReplies: 4Last Post: 02-10-2012, 04:51 AM -
Reading a file into an array?
By heycoa in forum New To JavaReplies: 4Last Post: 07-07-2011, 04:22 AM -
Help with GUI, Array, and Reading File
By bamagirl31 in forum New To JavaReplies: 21Last Post: 07-05-2011, 01:14 AM -
Help with reading file into array
By xkillswitchx14 in forum New To JavaReplies: 2Last Post: 04-28-2011, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks