0

I'm trying to make it for that the program will continue to request input of a 5 digit number until the user gives a 5 digit number. When I run the following:

//import scanner to read keyboard input
import java.util.Scanner;
class NumberInverter {
    public static void main(String[] args) {
    //create a new Scanner object in the memory that reads from the input System.in
    Scanner keyboard = new Scanner(System.in);
        
        //display message propmt and input for number
        //conditional statement loop to check if the length is any number other than 5
        do {
        System.out.print("Enter any 5 digit whole number you wish to invert!");
        int num = keyboard.nextInt();
        int numSize = String.valueOf(num).length();
        } while(!isValid(numSize));
}
    private static boolean isValid(int numSize){
        
        if(numSize != 5){
            System.out.println("Oops! Looks like you gave a number that isn't exactly a 5 digit whole number. Try again!");
            return false;
        } else return true; 
}
}

I get the following error:

NumberInverter.java:20: error: cannot find symbol
                } while(!isValid(numSize));              ^
  symbol:   variable numSize
  location: class NumberInverter
1 error

I've tried a bunch of different things, can anyone fluent in java help me out I'm very new? Thanks ahead of time!

  • 1
    I think my answer there will help - https://stackoverflow.com/questions/67741830/repeatedly-ask-user-for-input-if-input-is-not-an-integer/67742010#67742010 – Most Needed Rabbit May 28 '21 at 19:44

3 Answers3

1

Num size is declared inside the loop scope, you're trying to access it outside that scope. For this to work, declare numSize just before the do statement and assign it within the loop. This way, the variable is visible in the whole statement and also inside the loop (for assignment).

    int numSize;
    do {
      // The rest of your code, all variables declared here are
      // gone outside the brackets, however you can access the ones
      // in outer scopes.
      numSize = String.valueOf(num).length();
    } while(!isValid(numSize));

See this for more information about different types of scope: https://www.geeksforgeeks.org/variable-scope-in-java/

bones.felipe
  • 484
  • 5
  • 16
1

The variable numSize is not in the same scope as the while condition check.

In Java, any variable declared inside a block (a region surrounded by {}) cannot be accessed outside of that block. In this case, since you are declaring numSize right inside the loop, the loop's condition (which is outside that block) cannot access it. Each block creates something called a "block scope", and variables created in there cannot be accessed outside it.

The fix for this is very simple: Declare the variable in the same scope as the while loop. This can be done by putting it right above the do. Notice that you only need to int numSize;, outside, once. You don't put int when you are assigning to it inside the loop, you just do numSize = .... since you are assigning to a previously-declared variable.

You can still assign to the variable from inside the loop, but since it was originally declared outside the loop, stuff outside the loop can access it.

 int numSize;
 do {
        System.out.print("Enter any 5 digit whole number you wish to invert!");
        int num = keyboard.nextInt();
        numSize = String.valueOf(num).length();
 } while(!isValid(numSize));

Some more information about scopes can be found at What is 'scope' in Java?.

Luke Borowy
  • 1,381
  • 15
  • 24
0

Create a local variable numSize. For exemple :

//import scanner to read keyboard input
import java.util.Scanner;
class NumberInverter {
    public static void main(String[] args) {
    //create a new Scanner object in the memory that reads from the input System.in
    Scanner keyboard = new Scanner(System.in);
   int numSize;
        //   int numSize;
        //display message propmt and input for number
        //conditional statement loop to check if the length is any number other than 5
        do {
        System.out.print("Enter any 5 digit whole number you wish to invert!");
        int num = keyboard.nextInt();
         numSize = String.valueOf(num).length();
             
         } while(!isValid(numSize));
}
    private static boolean isValid(int numSize){
        
        if(numSize != 5){
            System.out.println("Oops! Looks like you gave a number that isn't exactly a 5 digit whole number. Try again!");
            return false;
        } else return true; 
}

}

Enjy
  • 235
  • 1
  • 6