?
Solved

Java method help

Posted on 2013-11-08
5
Medium Priority
?
1,266 Views
Last Modified: 2013-11-12
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.
0
Comment
Question by:DOCDGA
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39634859
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
 

Author Comment

by:DOCDGA
ID: 39634876
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
 

Author Comment

by:DOCDGA
ID: 39634922
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39635227
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
 

Author Closing Comment

by:DOCDGA
ID: 39641759
kaufmed, was great at providing assistance to a total newbie. I appreciate his help and prompt responses.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Six Sigma Control Plans

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question