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 display the original data, which I know how to do, 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.

This is practice for a Java class, I know this will be on the next exam and I just can't figure it out. We aren't allowed to use arrays or any such, just iterations and filereader.

Thank you guys in advance for your help!

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

    }
  }
       
}

So far, the code will read the file, and display it. That's as far as I can get, it's super frustrating not being able to do this.

Scanner can not only read lines from a file, but it can also be used to parse a string. You could create another Scanner called parser outside of your loop and use it inside your loop to get the data from the input line like this:

parser = new Scanner(line);  // do this after reading the line from the file

Then you can call parser.next() to get a string and/or parser.nextInt() to get an int from the string line.

Comments
Introduced me to using Scanner to parse a string.
Helpful suggestion.

Sorry, this is a little confusing for me. I can't seem to figure out how to use the parser, where to put it, ect. Could you give me an example?

Couldn't see a way to edit posts, so I'm double posting.

I think with the parser I can do what I need to do.

This is my new code, it shows the player's name underneath their original data, and I want it to show the average also, but I can't seem to get that far.

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);
    int sum;
    double average, total=0;
    
    String line,name;
    while (inputFile.hasNext())
    {
        line = inputFile.nextLine();
        Scanner parser = new Scanner(line);
  
        System.out.println(line);
        name= parser.next();
    
            average=total;
            System.out.println(name+" average score:"+average);
    }
  }
}

Right now average just prints 0.0 (which I expected). How can I gather up the Ints for only the one line at a time, add them, and then divide by the number of Ints that I added?

Thanks for your help, I'm getting there.

I just used a

while (parser.hasNextInt())

and it did infact add the numbers, but it added them continually for each person, by the end, the last had 280 or something points.

It should only add the numbers in the line that you have passed it for that player. You will need to reset total to 0 between players.

Thanks!

I seem to have gotten it, I can display the average for all players now. I need to find the max score also and the person it belongs to, then display that. I'll see what I can do about that and if I have trouble I'll come back to ask :)

Edited 6 Years Ago by soccer13: more information.

After fiddling around with it for a while, I can get the maximum score, but I would like to announce the person's name with it. How can I "link" the maximum score and the String it is associated with?

Here is my code for reference:

/**
 * 
 * 
 * 
 */
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);
    int score=0, maxScore=0;
    double average, total=0, i;
    String line,name;
    while (inputFile.hasNext())
  {
        line = inputFile.nextLine();
        Scanner parser = new Scanner(line);
        name= parser.next();
        System.out.println(line);
        total=0;
        i=0;   
        
        while (parser.hasNextInt())
        {
           score= parser.nextInt();
           if (maxScore<score)
                maxScore=score;
           i++;
            total=score+total;
        }   
        
            
            average=(total+1)/(i-1);
            System.out.println(name+" average score:"+average);
  
}

            System.out.println("\n\n\nThe highest scoring player is"+maxScore);
}
}

I get what you mean, but I don't know how to do it. Is there a way to grab a previous string (which would be the name)?

You already have a section where you are updating the maxScore. You can update more than one thing in that if() block if you add some braces (which you should always use anyway).

You don't need to grab a previous string - you just need a new variable to hold the max score player name.

Comments
Helped me through all problems in my program

Duh, completely forgot that I had a variable for the name already!

Goes to show that it's the stupid mistakes that hold you back. I've finally finished this, and learned how to (sort of) use parsing in my java programs.

Thanks a ton!

This question has already been answered. Start a new discussion instead.