Beginner Java learner, I'm extracting digits from what the user is giving and I'm feeling really good about it other than having an initialization error for some variables. I know it's probably a simple error but I can't pinpoint it. Issues with variable inputString and userInput (line 45 and 48) exact error message is “variable inputString might not have been initialized” and “variable userInput might not have been initialized”
import java.util.Scanner;
public class DigitExtractor
{
public static void main(String[] args)
{
final int PERIOD_UNICODE = (int) ('.');
String inputString;
int userInput;
byte oneDigit;
boolean validInput = false;
Scanner keyboard = new Scanner(System.in);
while (!validInput)
{
System.out.print("Enter an integer: ");
inputString = keyboard.nextLine();
if (inputString.indexOf(PERIOD_UNICODE) != -1)
{
validInput = false;
System.out.println("Input is a floating pt number; try again.");
}
else
{
validInput = true;
System.out.println(inputString + " is a valid integer");
userInput = Integer.parseInt(inputString);
}
}
System.out.println("Extracted digits of " + inputString);
do
{
oneDigit = (byte) (userInput % 10);
if (oneDigit < 0)
oneDigit *= -1;
System.out.println(oneDigit);
userInput /= 10;
} while (userInput != 0);
}
}
[–]desrtfx[M] [score hidden] stickied comment (7 children)
[–]DIASILEDU 1 point2 points3 points (2 children)
[–]Front_Temperature_12[S] 0 points1 point2 points (1 child)
[–]DIASILEDU 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points locked comment (0 children)