Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Little Help with my program..

807600Nov 8 2007 — edited Nov 9 2007
Ok... I am not completely finished with the program but I need to figure out something before I move on. This program is reading in information from a file. The thing I am having a problem with is how I can get it to add the GPA for each group of people, instead of adding the GPA for everyone in the file!! This program is basically reading in, the "groups living group", the name of each student, and their GPA. If you run my program, you will see a printout of "GPA sum in group" and GPA sum overall, I figured out how to get the sum overall for all the groups, but I cant figure out how to get the sum for each individual group! I hope this makes sense!!! If you need more clarification just ask!!! Thanks in advance! Also, I have compiled the program already and it runs just fine..!


I will give you the information from the file being read in. Here it is

Kappa Alpha Tau Sorority
Hollander 3.347
LaFarge 1.110
EndOfGroup
Taylor 2C Dormitory Section
Rapoport 2.2956
Smith 3.87
Scarl 3.48
Granville 2.946
EndOfGroup
Richards 4B Dormitory Section
Horstmann 2.769
Cole 3.47
Barney 2.23
EndOfGroup



Here is my code!!!!

import java.util.Scanner;
import java.text.DecimalFormat;
public class GPA
{
public static void main(String[] args)
{
final String SENTINEL = "EndOfGroup";
final double MAX_GPA = 4.0;
final Double MIN_GPA = 0.0;
final boolean DEBUG = true;
String livingG, studentName, stuName, wrongFormat;
double gpa = 0.0, gpaSum = 0.0, dVal = 0.0, maxGpa = 0.0,
avgGpa = 0.0, minGpa = 0.0, overMax = 0.0, overMin = 0.0,
dMin = 0.0, dMax = 0.0;
int counter = 0, grpNum = 0, totNum = 0;
DecimalFormat form = new DecimalFormat("0.00%");
Scanner scan = new Scanner(System.in);

while(scan.hasNext())
{
livingG = scan.nextLine();
System.out.println("Living Group: " + livingG);
stuName = scan.next();
grpNum = 0;
while(!stuName.equals(SENTINEL))
{
if(!scan.hasNextDouble())
{
wrongFormat = scan.next();
System.out.println("ERROR: USER INPUT NOT IN PROPER " +
"FORMAT " + wrongFormat);
}
else
{
gpa = scan.nextDouble();
System.out.println(" Student: " + stuName + " GPA = " +
gpa);
if(DEBUG == true)
{
grpNum++;
counter++;
gpaSum = gpaSum + gpa;
totNum++;
System.out.println("DEBUG:" + "\n" + " Number in group = " +
grpNum + " GPA sum in group = " + gpa + "\n" + " Total " +
"number = " + totNum + " GPA sum overall = " + gpaSum + "\n" +
" Minimum GPA in group = " + dMin + " Maximum GPA " +
"in group = " + MAX_GPA + "\n" + " Overall minimum GPA = " +
overMin + " Overall maximum GPA = " + overMax);
System.out.println();
stuName = scan.next();
}
else
{
stuName = scan.next();
}
}
}
System.out.println("Average GPA for this group = " + gpaSum / counter);
System.out.println("Minimum GPA for this group = " + minGpa);
System.out.println("Maximum GPA for this group = " + maxGpa);
System.out.println();
livingG = scan.nextLine();
}

}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2007
Added on Nov 8 2007
13 comments
98 views