Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: GPA and average program

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Question GPA and average program

    I am new on this and I will be working in a gpa and average program.
    This is my code so far:
    Everytime that I try to add the gpa part it gives me errors or just duplicates whatever I've in average and displays it in gpa


    /**
     * This program shows a database of students in a school.
     * It gathers information as name, classes, grades, and calculates average and gpa average.
     * 
     */
    import javax.swing.JOptionPane;
     
    public class StudentReport 
    {
    	public static  void main(String[]args)
    	{
    		String name;
    		String schoolName;
    		String schoolGrade;
    		double numOfSubjects;
    		double grades=0;
    		double average;
    		String trash;
     
     
    		name=JOptionPane.showInputDialog("Name:");            
    		schoolGrade=JOptionPane.showInputDialog("Grade:");	   
    		schoolName=JOptionPane.showInputDialog("School:");	  
     
     
     
    		trash=JOptionPane.showInputDialog("Number of classes:");              
    		numOfSubjects=Double.parseDouble(trash);				     			                                                                  
     
     
     
     
    		//initialize average
    		 average=0.0; 
     
    		//get the grades added together in order to calculate the average
    		for (int count1 = 1; count1 <=6; count1++)
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + count1 + " : " );
    			count1=Integer.parseInt(trash);
     
    			average=grades/numOfSubjects; //calculate average
     
    				}
     
     
     
     
    		JOptionPane.showMessageDialog(null, "School name: " + schoolName + "\n"			
    						             + "Student: "+ name + " \n "                                    
    				                             + "School grade: " + schoolGrade + "\n"	                        
    						             + "Average: " + average + ".");	                         	    
     
     
    	}
    }
    Last edited by Gerock7; March 25th, 2014 at 10:09 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: GPA and average program

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    it gives me errors
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Gerock7 (March 25th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: GPA and average program

    I know what I have wrong but I don't know how to fix it. The incorrect part is when using grades and average, because at the end, I get average=o. to due to having declared grades=0 but I don't know how to declare it outside and then inside the loop.
    Right now it compiles without syntax errors but I cannot calculate the average correctly because the grades are not declare correctly.

  5. #4
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: GPA and average program

    First, your loop for (int count1 = 1; count1 <=6; count1++) does not execute 6 times, why? because of this statement:
    count1=Integer.parseInt(trash); so when you input a number greater than 6 in Test1 it will terminate the loop since count1 is greater than 6 after the input.

    You are confused why you always get an average of zero?
    look at this statement in your code:
    average=grades/numOfSubjects; //calculate average
    your grades variable has a initial value of zero right? and you never write any value on it. so when your program reads that statement, it will be: average = 0 / numOfSubjects; and zero divided by any number would result to zero that is why you always got an average of zero.

    I think this statement in your code :
    count1=Integer.parseInt(trash);
    should be like this:
    grades=Integer.parseInt(trash); just a guess.

  6. The Following User Says Thank You to dicdic For This Useful Post:

    Gerock7 (March 25th, 2014)

  7. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: GPA and average program

    haha yeah you are right I didn't see that. I changed that but how I make sure that it stops when telling it to add less of 6 grades. Like if the student says 3 and once he adds the third grade, the program calculate the average directly instead of keep asking the rest of the tests' grades.

    Also when calculating the average even when saying 100 to the 6 tests and get at the end 33.3333...etc.

    --- Update ---

    Actually it is dividing just one grade or the first one from the number of subjects idk how to declare the sum of unknown number of grades

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GPA and average program

    Post your updated code, a sample run (copied from the output), and describe what's wrong with the sample run, giving what it should be.

  9. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Average code not working

    I fixed some parts of my code but now it gives me an error at end saying:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at StudentReport.main(StudentReport.java:52)


    This my new code:

    /**
     * This program shows a database of students in a school.
     * It gathers information as name, classes, grades, and calculates average and gpa average.
     * 
     * @author Gerock7
     *
     */
    import javax.swing.JOptionPane;
     
    public class StudentReport 
    {
    	public static  void main(String[]args)
    	{
    		String name;
    		String schoolName;
    		String schoolGrade;
    		int numOfSubjects;
    		double grades;
    		double average;
    		String trash;
     
     
    		name=JOptionPane.showInputDialog("Name:");           
    		schoolGrade=JOptionPane.showInputDialog("Grade:");	  
    		schoolName=JOptionPane.showInputDialog("School:");	  
     
     
     
    		trash=JOptionPane.showInputDialog("Number of classes:");              
    		numOfSubjects=Integer.parseInt(trash);				              			                                                                  
     
    		trash=JOptionPane.showInputDialog("Test 1: ");
    		grades=Double.parseDouble(trash);
     
    		average=grades/numOfSubjects;
     
     
            double [] numbers = new double [ numOfSubjects];
     
    		double sum=0;
     
     
    		//get the grades added together in order to calculate the average
    		for (int i = 2; i <=numOfSubjects; i++) 
    				{
     
    			trash=JOptionPane.showInputDialog("Test " + i + " : " );
    			grades=Integer.parseInt(trash);
     
    					sum += numbers [i];
     
    				}
     
    		average=sum/numOfSubjects;
     
     
    		JOptionPane.showMessageDialog(null, "School name: " + schoolName + "\n"			
    						+ "Student: "+ name + " \n "                                   
    				                + "School grade: " + schoolGrade + "\n"	                                        
    						+ "Average: " + average + ".");	                         	   
     
     
    	}
     
    }

  10. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: GPA and average program

    okay, this.
    ask first of how many grades would the student will enter.
    input dialog must be : Enter no. of grades. - and store it in integer variable
    then create a loop whose condition must be less than the no. of grades
    for(int i = 1; i <= no. of grades; i++) or for(int i = 0; i < no. of grades; i++)
    inside that loop get all the grades and add it in variable, lets say total variable whose initial value is zero.
    something like total += grade something like that.
    so after the loop you have now the total grades the student inputted.
    then after the loop divide the total grades by no. of grade.
    so lets say the student's no. of grade is 3. then he provided the grades 85, 86 and 87.
    inside your loop your have now 85 + 86 + 87 = 258.
    outside the loop divide it by the no. of grades so:
    average = total / no. of grades
    average = 258 / 3
    average = 86.
    got it? hope it helps. that is just a simple idea, but you can do it in different or better way.

  11. The Following User Says Thank You to dicdic For This Useful Post:

    Gerock7 (March 25th, 2014)

  12. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: GPA and average program

    Please don't post multiple threads on the same topic.

    Threads merged.

  13. The Following User Says Thank You to GregBrannon For This Useful Post:

    Gerock7 (March 25th, 2014)

  14. #10
    Junior Member
    Join Date
    Mar 2014
    Posts
    25
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: GPA and average program

    thanks guys I made the proper changes and it is now working, I will still add some other things.
    Again thanks

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  2. How to make a java program that gets average test scores?
    By johnsonparkar224 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 2nd, 2013, 06:33 AM
  3. GPA Calculation Program using an input file
    By ddk1992 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 6th, 2011, 06:28 AM
  4. Help with JFrame program that calculates average
    By ePerKar3 in forum AWT / Java Swing
    Replies: 3
    Last Post: November 4th, 2011, 08:48 AM
  5. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 02:33 PM

Tags for this Thread