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

Thread: Reading a file

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading a file

    Hey,

    What I need to do is read a file- it will have a name and several numbers after, with -1 as a sentinel number to signal the end of line, ex:

    Rogers 15 22 6 12 -1
    Myers 23 10 4 22 34 -1
    ...
    ....
    .....

    What I want to do is take the numbers, except -1, read them, and display an average of each name's numbers, alone with the name with the highest number.

    So far I can read the file and display it, but I'm not sure how to gather the numbers inside of the file and use them for displaying averages and maximum.

    import java.io.*;
    import java.util.Scanner;
    public class HW7
    {
       public static void main (String[] args) throws IOException {
        Scanner keyboard = new Scanner(System.in);
     
        System.out.println("What is the name of the file?");
        String fileName = keyboard.nextLine();
     
        File file = new File(fileName);
        Scanner inputFile = new Scanner(file);
     
        while (inputFile.hasNext())
        {
     
            String line = inputFile.nextLine();
            System.out.println(line);
     
    }
     
     
        }
     
     
        }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Reading a file

    You can parse through each line by using a StringTokenizer (Java Platform SE 6) From there you can parse a String representation of an integer to an int using Integer.parseInt().

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading a file

    Thank you,

    I should have mentioned however, that I'm trying to do this as practice for a class and I should only use the File reader and scanner, along with loops to do the aforementioned. I'm stuck when it comes to breaking apart the file and gathering the numbers.

    I know that for the maximum I should get the first number, assign that to the maximum, and while there are more numbers, get the next number, and if that is higher, it then becomes the maximum. I'm just lost when it comes to putting this into code.

Similar Threads

  1. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 03:43 AM
  2. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 03:14 AM
  3. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM
  4. [SOLVED] reading only certain lines from a .txt file
    By straw in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: March 7th, 2010, 07:49 PM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM