Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Having trouble splitting a text file into multiple arrays.

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

     
    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.

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

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

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble splitting a text file into multiple arrays.

    Quote Originally Posted by Norm View Post
    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.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Having trouble splitting a text file into multiple arrays.

    After using split() you will need to convert the Strings to int values
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

     
    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?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by orbin; June 19th, 2012 at 04:29 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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:
    System.out.println(Arrays.toString(<arraynamehere>));
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble splitting a text file into multiple arrays.

     
    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

    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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

     
    [Ljava.lang.String;@19821f
    [Ljava.lang.String;@addbf1
    [Ljava.lang.String;@42e816
    [Ljava.lang.String;@9304b1
    [Ljava.lang.String;@190d11

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Having trouble splitting a text file into multiple arrays.

    Please post the code that printed out what you posted in post#15.

    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.
    Last edited by Norm; June 19th, 2012 at 05:06 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble splitting a text file into multiple arrays.

     
    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]);
    }
        }
    }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.
    Last edited by orbin; June 19th, 2012 at 05:16 PM.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,014
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: June 18th, 2012, 04:54 AM
  2. [SOLVED] Splitting a txt file with multiple spaces
    By macko in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: November 1st, 2011, 01:03 PM
  3. Create Multiple Arrays from a text file
    By chris11kgf in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2011, 12:40 AM
  4. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 07:54 PM
  5. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 06:57 PM