Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Please Help!

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry if this isn't the right place to ask this question, so go easy on me.

I'm very novice with Java, but I'm still trying to learn from a few books and online tutorials here and there.

Please keep in mind, it may not be very organized, so don't crucify me for that, but any constructive critics will be welcomed!

I'm having issues when compiling. I keep getting the error:


variable gpa might not have been initialized
System.out.println("Your GPA is " + gpa);
^


For the life of me, I cannot find out what I am doing wrong.
The point of this little program is to take an average of all of your class scores, and create a GPA for it.
Anything that you can help with me would be so much appreciated!

Here is the code:




//Computes the Average of a series of scores

import jpb.*;

public class avgScores{
public static void main(String[]args){


//prompt user to enter a number of scores
SimpleIO.prompt("Enter the number of classes you want averaged: ");
String userInput = SimpleIO.readLine().trim();
int numberOfScores = Integer.parseInt(userInput);
System.out.println();

//create and array to hold scores
int [] scores = new int [numberOfScores];


//Prompt user to enter scores, then store in array

for (int i = 0; i < scores.length; i++) {
SimpleIO.prompt("Enter score for class " + (i + 1) + ": ");
userInput = SimpleIO.readLine().trim();
scores [i] = Integer.parseInt(userInput);
}

//Compute the Sum of Scores
int sum = 0;
int avgScore = sum / scores.length;
for (int i = 0; i < scores.length; i++)
sum += scores[i];

//Display Avg Score

System.out.println("\nAverage Score is " + sum / scores.length);





//Calculate GPA

double gpa;




if (avgScore <= 59) gpa = 0;
else if (avgScore <= 60) gpa = 0.7;
else if (avgScore <= 61) gpa = 0.8;
else if (avgScore <= 62) gpa = 0.8;
else if (avgScore <= 63) gpa = 0.9;
else if (avgScore <= 64) gpa = 1.0;
else if (avgScore <= 65) gpa = 1.0;
else if (avgScore <= 66) gpa = 1.1;
else if (avgScore <= 67) gpa = 1.2;
else if (avgScore <= 68) gpa = 1.3;
else if (avgScore <= 69) gpa = 1.4;
else if (avgScore <= 70) gpa = 1.5;
else if (avgScore <= 71) gpa = 1.6;
else if (avgScore <= 72) gpa = 1.7;
else if (avgScore <= 73) gpa = 1.8;
else if (avgScore <= 74) gpa = 1.9;
else if (avgScore <= 75) gpa = 2.0;
else if (avgScore <= 76) gpa = 2.1;
else if (avgScore <= 77) gpa = 2.2;
else if (avgScore <= 78) gpa = 2.3;
else if (avgScore <= 79) gpa = 2.4;
else if (avgScore <= 80) gpa = 2.5;
else if (avgScore <= 81) gpa = 2.6;
else if (avgScore <= 82) gpa = 2.7;
else if (avgScore <= 83) gpa = 2.8;
else if (avgScore <= 84) gpa = 2.9;
else if (avgScore <= 85) gpa = 3.0;
else if (avgScore <= 86) gpa = 3.1;
else if (avgScore <= 87) gpa = 3.2;
else if (avgScore <= 88) gpa = 3.3;
else if (avgScore <= 89) gpa = 3.4;
else if (avgScore <= 90) gpa = 3.5;
else if (avgScore <= 91) gpa = 3.5;
else if (avgScore <= 92) gpa = 3.6;
else if (avgScore <= 93) gpa = 3.7;
else if (avgScore <= 94) gpa = 3.8;
else if (avgScore <= 95) gpa = 3.9;
else if (avgScore <= 96) gpa = 3.9;
else if (avgScore <= 97) gpa = 4.0;
else if (avgScore <= 98) gpa = 4.0;
else if (avgScore <= 99) gpa = 4.0;
else if (avgScore <= 100) gpa = 4.0;



//Print GPA

System.out.println("Your GPA is " + gpa);
System.out.println("Thanks for using my program!");


 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What happens if the avgscore is greater than 100? Will the gpa variable be set in that case?

Henry
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Local variables are not automatically initialized. "double gpa;" is not initialized, and there is a chance that none of the conditions (the huge if/else) will be met. We usually initialize local variables when we declare them.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please UseAMeaningfulSubjectLine next time.
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ryon Groff wrote:I'm sorry if this isn't the right place to ask this question, so go easy on me.



this is the right place

Ryon Groff wrote:

I'm very novice with Java, but I'm still trying to learn from a few books and online tutorials here and there.

Please keep in mind, it may not be very organized, so don't crucify me for that, but any constructive critics will be welcomed!



dont hesistate to raise questions here.. we are all here for learning purpose only.


Ryon Groff wrote:
I'm having issues when compiling. I keep getting the error:


variable gpa might not have been initialized
System.out.println("Your GPA is " + gpa);
^






try to study the variables... local variable must be initialized with some values... hope the follow will clear your problem

double gpa=0.0;

cheers
 
Ryon Groff
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess I'm just not ready to tackle a program like this.

When I try to initialize the gpa double, ex. double gpa = 1

The final output of the program doesn't change the gpa at all, it stays = to 1.

And sorry about the subject line, i'll do better next time!

 
Ryon Groff
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!
Solved my problem!
 
Marshal
Posts: 79640
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic