• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java initalizing problem in program.  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I made a program to count the amount of grades entered by a teacher and also find the total and average grade(I got help from the book Java how to program by Paul Deital) but for some weird reason even though I DID intialise my variables I declared them and yet its telling me I didn't the variables that I have a problem with are "total" and "average" they originally did work until I fixed a mistake on the program which I forgot to close the while loop after the switch statement and left it open to the end(but it did work until I changed that),now for some reason it says this error


"54: error:variable total may not have been initalized,


if someone could please help that would be great and much appreciated I'm starting to get discouraged a little =(

Thanks guys



heres the code



 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
"54: error:variable total may not have been initalized,


if someone could please help that would be great and much appreciated I'm starting to get discouraged a little =(


Well, the error message is pretty clear. In Java, local variables must be set to something before they can be used. And if the compiler detects otherwise, it will complain. Can you tell use where you initialized (first set) it? Can you tell use where you first used it? And can you confirm that the initialization came before the first usage?

Henry
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Adam Chalkley wrote:
"54: error:variable total may not have been initalized,


if someone could please help that would be great and much appreciated I'm starting to get discouraged a little =(


Well, the error message is pretty clear. In Java, local variables must be set to something before they can be used. And if the compiler detects otherwise, it will complain. Can you tell use where you initialized (first set) it? Can you tell use where you first used it? And can you confirm that the initialization came before the first usage?

Henry


Hi Henry thanks for the reply =)

I understand the concept of local variables and global variables but I'm only using one method and thats the main method for this sample program,If I declared a variable outside a while loop would that be breaking the rules?because its all in the same method,or would I have to declare AND intalize that variable in the while loop?

I declared "total" at the start of the program and intialized(or assigned a value) it in the while loop,and I declared and assigned "average after the while loop in the if statement towards the end of the program.

how do you think i could solve this

Thanks

Adam,
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your code:


To quote a song from my childhood...

One of these things is not like the others...

and the one that is different just happens to be the one the compiler is telling you is wrong...

"54: error:variable total may not have been initalized,


So...what might you do?


 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Look at your code:


To quote a song from my childhood...

One of these things is not like the others...

and the one that is different just happens to be the one the compiler is telling you is wrong...


"54: error:variable total may not have been initalized,


So...what might you do?





Thanks that fixed the compiler error,but I thought you could assign/innitalize a variable anywhere within the method aslong as you declare it?

I was just alsowondering also how could I add the total of grades togheter as the while loop "loops".


Thanks for the help,

Adam
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Thanks that fixed the compiler error,but I thought you could assign/innitalize a variable anywhere within the method aslong as you declare it?


Keep in mind that the compiler operates at ... compile time...

The issue here is that at compile time, the compiler only sees calling a method on an instance (when looking at the condition of the while loop). It doesn't know if the condition is ever true. In order words, it thinks that it is possible for the while loop to have zero iterations.

So, yes, you can initialize the variable after to declared it, but you must guarantee that it will be initialized, with every possible path prior to its first usage (including code paths that can't actually happen at runtime).

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
I was just alsowondering also how could I add the total of grades togheter as the while loop "loops".


Hint: What is a "=+" operator? What is it supposed to do?

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!