1

Not sure where i'm going wrong, please help. Trying to check if the user has only entered an integer. Error showing strInpit cannot be resolved to a variable.

import java.util.Scanner;

public class Salary{ 
  public static void main(String[] args){
    int num = 0;
    String strIntput;
    boolean valid = false;
    

    // setup scanner
    Scanner in = new Scanner(System.in);
    // loop to check for valid input
    while(valid == false){
      //prompt user to enter
      System.out.println("Enter your salary per year");
      // grab input from keyboard
      strInput = in.nextLine();
      // try to convert String to int
      try{
        num = Integer.parseInt(strInput);
        valid = true;
      }
      catch(NumberFormatException e){
        System.out.println("Error - enter an integer value.");
      }
    }
    System.out.println("your salary is " + num);
  }
}

2
  • 3
    You have a typo strIntput vs. strInput Commented Apr 15, 2022 at 11:32
  • 1
    I am voting to close this question because it was caused by a typo, and as such it is unlikely to be of help to future visitors to this site. Commented Apr 16, 2022 at 17:33

2 Answers 2

1

you have named the variable as String strIntput;

while declaring it..but you are using strInput everywhere else

so change String strIntput; TO String strInput;

Sign up to request clarification or add additional context in comments.

1 Comment

0

There is another method which you can use: #Scaner.nextInt(); It will get Integer value from console or, in another case, will throw an exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.