Solved

Java method help

Posted on 2013-11-08
5
1,175 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 74

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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now