• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String not initialised question

 
Kevin M Brown
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34965
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50173
79
  • Mark post as helpful
  • send pies
  • 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)?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic