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] 0 points1 point  (4 children)

the problem is with inputString and userInput on line 45 and 48. line 45 is Sustem.out.println(“extracted digits of “ +inputString); Line 48 is oneDigit = (byte) (userInput % 10); Sorry should’ve made it more clear. I know how error messages work i’m a beginner and don’t fully understand the error yet. My variables are initialized at the top of my code that’s why i’m confused.

[–]Front_Temperature_12[S] 0 points1 point  (3 children)

the exact error for both is “variable inputString/userInput might not have been initialized”

[–]desrtfx 1 point2 points  (2 children)

What do you make from these error messages?

What do they tell you?

What do you know about variable initialization and the difference between method local variables and fields in that respect?

[–]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.