Results 1 to 12 of 12
  1. #1
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Question 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);

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: array or arraylist for reading file

    Please use
    Java Code:
     tags
    when posting code.

    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.

    ** This space for rent **

  3. #3
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: array or arraylist for reading file

    Quote Originally Posted by Tolls View Post
    Please use
    Java Code:
     tags
    when posting code.

    I'm not sure where you are stuck?
    You've read the values in, so is it how to save them to an array?
    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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default 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.

    ** This space for rent **

  5. #5
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: array or arraylist for reading file

    Quote Originally Posted by Tolls View Post
    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.
    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]);

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default 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.

    ** This space for rent **

  9. #9
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default Re: array or arraylist for reading file

    Quote Originally Posted by Tolls View Post
    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.
    My aim is to have the avarage of every students' grades(grd1,grd2,grd3) as in the example of my first post and make a new array for the avarage grades for every student.Then I should print out for every student in format of " Name Surname , AvarageGrd="

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default 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.

    ** This space for rent **

  11. #11
    denpetro is offline Member
    Join Date
    Nov 2012
    Posts
    6
    Rep Power
    0

    Default 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.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default Re: array or arraylist for reading file

    Java Code:
    String[] names = new String[<however many names you have>];
    int[] averages = new int[<as above>];
    ?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Reading from txt file to arraylist
    By artur in forum New To Java
    Replies: 4
    Last Post: 02-10-2012, 04:51 AM
  3. Reading a file into an array?
    By heycoa in forum New To Java
    Replies: 4
    Last Post: 07-07-2011, 04:22 AM
  4. Help with GUI, Array, and Reading File
    By bamagirl31 in forum New To Java
    Replies: 21
    Last Post: 07-05-2011, 01:14 AM
  5. Help with reading file into array
    By xkillswitchx14 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 10:24 PM

Posting Permissions

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