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 10 of 10

Thread: Java Program with text file - Help!

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Program with text file - Help!

    I've been given a coding problem that I'm having trouble with. I've been given the code below along with a text file. The text file contains the folowing:

    Chen Ruolin 9.2 9.3 9 9.9 9.5 9.5 9.6 9.8
    Emilie Heymans 9.2 9.2 9 9.9 9.5 9.5 9.7 9.6
    Wang Xin 9.2 9.2 9.1 9.9 9.5 9.6 9.4 9.8
    Paola Espinosa 9.2 9.3 9.2 9 9.5 9.3 9.6 9.8
    Tatiana Ortiz 9.2 9.3 9 9.4 9.1 9.5 9.6 9.8
    Melissa Wu 9.2 9.3 9.3 9.7 9.2 9.2 9.6 9.8
    Marie-Eve Marleau 9.2 9.2 9.2 9.9 9.5 9.2 9.3 9.8
    Tonia Couch 9.2 9 9.1 9.5 9.2 9.3 9.4 9.6
    Laura Wilkinson 9.7 9.1 9.3 9.4 9.5 9.4 9.6 9.2


    import java.io.*;
    import java.util.*;
     
    public class readFile {
     static Scanner infile = null;
     
     public static void main(String[] args) throws IOException {
     
      infile  = new Scanner(new FileReader("diving_data.txt"));
     
            String str = null;
            double score = 0.0;
            //I know there are 9 lines of data
            for(int l=0; l<9; l++) {
       //System.out.println("The data from line " + l);
       str = infile.next();
       System.out.print(str + " ");
       str = infile.next();
       System.out.print(str + " ");
       //I know there are 8 scores per diver
       for(int s=0; s<8; s++) {
        score = infile.nextDouble();
           System.out.print(score + " ");
       }
       System.out.println();
      }
     
     }
     
    }

    I've been given the following instructions along with this... to be honest I'm not even really sure as to what is being asked of me:

    Per the following UML diagram you are to write a Diver class. Then using the attached data file I want another program that reads the data processing one line at a time to instantiate your Diver class, populate the Diver's data attributes, and then outputting that Diver's information by calling it's toString method.

    In a diving competition, each contestant's score is calculated by dropping the lowest and highest scores and then adding the remaining scores. Write a program that reads the provided data file formatted as depicted in the following table. For each diver output the diver's name and total score using the above scoring rules. Format each diver's total score to two decimal places. So for example, the output for Chen Ruolin below would be: Chen Ruolin – 56.90 points.

    I'm just wondering what exactly I should be doing for this code, and how I should go about completing it. I've never been this lost on any of the programs I've dont before. Help is greatly appreciated.


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Java Program with text file - Help!

    Just a quick comment on the code because I don't have a lot of time ... use split(" ") to split the String into an array, deliminated by a space " " character.

    str = infile.nextLine();
    String[] record = str.split(" ");
     
    // Now each field in the line can be accessed as an array
    String firstName = record[0];
    String lastName = record[1];
    double score1 = Double.parse(record[2]);
    //etc...

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program with text file - Help!

    Ahhh so I need to use an array to split up the data in the txt file. Is there a specific spot in the code where the array needs to go?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program with text file - Help!

    Use the split() method after a line has been read from the file and before the code needs to access the data in the line. The split() method creates the array and fills it the the tokens that were on the line. To see what is in the array, use the Arrays class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program with text file - Help!

    import java.io.*;
    import java.util.*;
     
    public class readFile {
     static Scanner infile = null;
     
     public static void main(String[] args) throws IOException {
     
      infile  = new Scanner(new FileReader("diving_data.txt"));
     
            String str = null;
            double score = 0.0;
     
             str = infile.nextLine();
    String[] record = str.split(" ");
     
    // Now each field in the line can be accessed as an array
    String firstName = record[0];
    String lastName = record[1];
    double score1 = Double.parseDouble(record[2]);
     
            //I know there are 9 lines of data
            for(int l=0; l<9; l++) {
       //System.out.println("The data from line " + l);
       str = infile.next();
       System.out.print(str + " ");
       str = infile.next();
       System.out.print(str + " ");
       //I know there are 8 scores per diver
       for(int s=0; s<8; s++) {
        score = infile.nextDouble();
           System.out.print(score + " ");
       }
       System.out.println();
      }
     
     }
     
    }

    I've attempted to implement the code into my code to test it out, but I'm getting some crazy errors. I'm very lost as to what the issue is.

    Output:

    java.lang.NumberFormatException: For input string: "9.2 9.3 9 9.9 9.5 9.5 9.6 9.8"
    at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
    at java.lang.Double.parseDouble(Unknown Source)
    at readFile.main(readFile.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program with text file - Help!

    java.lang.NumberFormatException: For input string: "9.2 9.3 9 9.9 9.5 9.5 9.6 9.8"
    at java.lang.Double.parseDouble(Unknown Source)
    at readFile.main(readFile.java:20)
    That String can not be converted to a double. It needs to be split into separate tokens like: "9.2" and "9.3" which can be converted to double values.

    What character is between the numbers in that String?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program with text file - Help!

    It is a space in between each number.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program with text file - Help!

    Are you sure? Do you have a hex editor that you can look at the file with to see what is in it?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Program with text file - Help!

    If I open the file with a text editor it seems to be a space in between each number. If it was not a space would i just have to change the split(" ") to whatever the character is?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Program with text file - Help!

    Look at the file in a hex editor to see what is between the numbers.

    have to change the split(" ") to whatever the character is?
    Yes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. Program that selectively outputs data from a text file.
    By MJjavapf in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 23rd, 2012, 04:26 PM
  3. simple program for reading text file
    By johnpipes in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 21st, 2011, 05:55 PM
  4. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM

Tags for this Thread