0

beginner here. As mentioned before my question is: how do you output an error when the input is a string instead of an int? I'm trying to created a program that outputs a workers raise in salary, and I wanted to implement a function that displays an error when the user entered something other than a number for their salary. here's my code:

package calculating.salary;
import java.util.Scanner;

public class CalculatingSalary {



public static void main(String[] args) {

    Scanner in = new Scanner(System.in);

    System.out.println("Please input yearly salary:");

    int salary = in.nextInt();
    //this is where I would implement the error message

    System.out.println("Please input how many years worked:");

    boolean running = true;
    while (running){

    String years = in.nextLine();

    if (years.equals ("1")|| years.equals("one")){
        System.out.println("Your salary is now $"+(salary + salary*0.02));
        break;
    }

    if (years.equals("2")||years.equals("two")){
         System.out.println("Your salary is now $"+(salary + salary*0.03));
        break;
    }
    if (years.equals("3")||years.equals("three")){
         System.out.println("Your salary is now $"+(salary + salary*0.04));
        break;
    }
    if (years.equals("4")||years.equals("four")){
         System.out.println("Your salary is now $"+(salary + salary*0.05));
        break;
    }
    if (years.equals("5")||years.equals("five")){
         System.out.println("Your salary is now $"+(salary + salary*0.06));
        break;
    }       
        else {
        System.out.println("Please type in a number from 1 through 5");
    }
}
    }
 }
1
  • I would suggest validating the data entry rather than trying to generate an error on the entered data. I would call a function on the keyup (or other event) that occurs when the value is entered - to prevent the user entering any value other than what you want. Alternatively a function that is called on blur of the data entery field to validate the entered data. You don't really want the user to proceed through if their entered data is incorrect. IMO Commented Oct 29, 2016 at 4:10

1 Answer 1

1
String salary = in.nextLine();
int salaryValue;
try {
    salaryValue = Integer.parseInt(salary);
} catch (NumberFormatException e) {
    System.out.println("You didn't enter a valid integer.");
}

This will store the user's input in a String instead of an int, then manage the conversion separately. If the conversion fails, an exception is thrown, which causes the message to be printed.

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

2 Comments

I'm sorry, but this solution didn't work. After i converted the variable to a string, the if statements later in the code were unable to calculate the value of the salary variable
OK, in my snippet above, the variable used to store the salary is called salaryValue. Is that what you used in your calculations? If you want to keep it called salary then you could do something like String salaryAsAString = in.nextLine(); followed by salary = Integer.parse(salaryAsAString)' if that's clearer for you.

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.