• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String not initialised question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I gotta problem with ma code :O
I'm doing a project that's based on an online results showing form.
The problem that I'm having is that it's bringing up an error message saying that variables aren't initialised, when they are.

Here's the code -


The error is that the variable "name" a String, may not have been initialised.

I'm using BlueJ.

Any suggestions? It works fine while inputting code only, not showing it.

Thanks!
 
author & internet detective
Posts: 41907
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Name looks correct. YOu could try String name = sc.next() to get rid of any gap between declaration and initialization though.

However, searching your code for "webGrade" shows two references. The first is "int webGrade". Which declares it but doesn't initialize it. The other is where it gets printed out. Which won't compile because it is never set to a value.
 
Kevin M Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:Name looks correct. YOu could try String name = sc.next() to get rid of any gap between declaration and initialization though.

However, searching your code for "webGrade" shows two references. The first is "int webGrade". Which declares it but doesn't initialize it. The other is where it gets printed out. Which won't compile because it is never set to a value.



Thanks for pointing out the webGrade blunder, fixed it.

I tried the String name=sc.next() instead of creating the variable at the start, didn't work, same error "variable name might not have been initialised".

Any other suggestions?

-Kevin
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

viperkevin Yes wrote:
The problem that I'm having is that it's bringing up an error message saying that variables aren't initialised, when they are.



It's not lying. It's saying the variable might not have been initialized. That is, the compiler can't be sure that it has been initialized.

For example:



In both of the above cases, the compiler can't be sure that y will be initialized, so we'll get an error. Now, you and I can look at the code and say that all the cases are covered and y is definitely initialized, but that's because we're doing some semantic analysis that the compiler doesn't do.

Your code is far too long for me to read, but I guarantee you're doing something similar.
 
Marshal
Posts: 79406
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

. . . Your code is far too long for me to read, . . .

So are your lines. //------- comments are bad style, and their length made the code difficult to read. Multiple blank lines also make the code difficult to read. I have corrected some of those errors.

If that is one single method, it is too long. It should be divided into separate methods. It is long enough to make at least six methods. Why have you got a blank case (3)?
 
reply
    Bookmark Topic Watch Topic
  • New Topic