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(System.in);
    String data = console.nextLine();
    
    String name = "";
    double sum;
    int numCount;
       
   
    
    for(int i = 0; i < data.length(); i++){
        char ch = data.charAt(i);
        if(Character.isLetter(ch)){
            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.
DOCDGAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
0
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.
0
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.
0
käµfm³d 👽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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DOCDGAAuthor Commented:
kaufmed, was great at providing assistance to a total newbie. I appreciate his help and prompt responses.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Theory

From novice to tech pro — start learning today.