Java method help

Write a method named printGPA that calculates a student's grade point average. The user will type a line of input containing the student's name, then a number of scores, followed by that many integer scores. Here are two example dialogues:

Enter a student record: Maria 5 72 91 84 89 78
Maria's grade is 82.8
Enter a student record: Jordan 4 86 71 62 90
Jordan's grade is 77.25
public void printGPA(){
    System.out.print("Enter a student record: ");
    Scanner console = new Scanner(;
    String data = console.nextLine();
    String name = "";
    double sum;
    int numCount;
    for(int i = 0; i < data.length(); i++){
        char ch = data.charAt(i);
            name = name +data.charAt(i);
    System.out.println(name + "'s grade is "+(sum/(double)numCount));

Open in new window

I'm  new to programming. I need help resolving this problem this is not homework. I am trying to learn how to program on my own. I purchased the book from Amazon and use the Practice-It! site to enforce concepts.

Anyway I'm able to obtain the name but am having problems getting the numbers. I know I should look for the starting space and ending space to determine a number and add that number to the cumulative sum. Any help that can point me in right direction is greatly appreciated.
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Also should I be trying to accept input with 2 different scanner objects. If so I can do that but I'm not sure how to do it with one scanner.
No, you don't need to. You can use the same Scanner, but use different methods to get the data. I suggested the two methods above because next will give you a token, which will be a string. nextInt will also give you a token, but it will convert the token to an int for you.
käµfm³d 👽Commented:
You might find it easier to use the next and nextInt methods for this task. By default, the delimiter for the Scanner class is any whitespace character, which is what is separating your names, and each subsequent grade. You can call the former to get the name, and then call the latter to get the grades.

You certainly can go about it the way you are currently doing, so if your requirement is to do it that way, then let us know.
DOCDGAAuthor Commented:
I appreciate your help. I'll look into next and nextInt methods. The requirements didn't say anything about using nextLine method. I used it because it accepts Strings.
DOCDGAAuthor Commented:
kaufmed, thanks for the help

But I still not sure exactly what to do. Don't give me the answer to the exercise but do I need to change current method nextLine. Also should I be trying to accept input with 2 different scanner objects. If so I can do that but I'm not sure how to do it with one scanner.

I do understand that the name and grades are separated by white space. Just don't know how to setup my code.
DOCDGAAuthor Commented:
kaufmed, was great at providing assistance to a total newbie. I appreciate his help and prompt responses.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.