aspose file tools*
The moose likes Beginning Java and the fly likes String not initialised question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String not initialised question" Watch "String not initialised question" New topic
Author

String not initialised question

Kevin M Brown
Greenhorn

Joined: Nov 06, 2012
Posts: 3
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

Joined: May 26, 2003
Posts: 30925
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kevin M Brown
Greenhorn

Joined: Nov 06, 2012
Posts: 3
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 39828
    
  28
. . . 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)?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String not initialised question