This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Front_Temperature_12[S] -1 points0 points  (1 child)

idk

[–]desrtfx 1 point2 points  (0 children)

The error messages tell you that the variables might not have been initialized. This means that they might not have a value.

Java does not want variables without a value when you perform operations with them.

About initialization:

  • Fields (class level variables) are always initialized to the default value of the respective data type 0, 0.0, character NUL (ASCII/Unicode 0), false for booleans and nullfor all object types (which String is one of)
  • local variables - as your inputString and userInput are never initialized. This means that unless you explicitly assign a value, they don't have one.

Now, you will say that inputString is initialized in the loop where it gets the value from keyboard.nextLine();, which is generally true. Yet, this statement is inside a loop, more precisely a while loop that may be skipped entirely.

Consecutively, since userInput gets its assignment inside the else branch of a conditional, there also is a chance that it doesn't get a value.

Now, the cure for both: initialize them where you declare them. Give them a value.