• 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
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Java initalizing problem in program.

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
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

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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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

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
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

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
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic