Question

write a java app that ensures the user enters a valid speed, from 40-65 (use try...catch...

write a java app that ensures the user enters a valid speed, from 40-65 (use try...catch to ensure int values)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class ValidSpeed {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int speed;
        while (true) {
            try {
                System.out.print("Enter a speed(40-65): ");
                speed = Integer.parseInt(in.nextLine());
                if (speed >= 40 && speed <= 65)
                    break;

                System.out.println("Speed is not in valid range. Try again!");
            } catch (Exception e) {
                System.out.println("That was not an integer. Try again!");
            }
        }
        System.out.println("Valid speed entered was " + speed);

        in.close();
    }
}

Add a comment
Know the answer?
Add Answer to:
write a java app that ensures the user enters a valid speed, from 40-65 (use try...catch...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For practice using exceptions, this exercise will use a try/catch block to validate integer input from...

    For practice using exceptions, this exercise will use a try/catch block to validate integer input from a user. Write a brief program to test the user input. Use a try/catch block, request user entry of an integer, use Scanner to read the input, throw an InputMismatchException if the input is not valid, and display "This is an invalid entry, please enter an integer" when an exception is thrown. InputMismatchException is one of the existing Java exceptions thrown by the Scanner...

  • I am required to use the try - catch block to validate the input as the...

    I am required to use the try - catch block to validate the input as the test data uses a word "Thirty" and not numbers. The program needs to validate that input, throw an exception and then display the error message. If I don't use the try - catch method I end up with program crashing. My issue is that I can't get the try - catch portion to work. Can you please help? I have attached what I have...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • This is a Java beginner class. Write the following exercise to check the user inputs to...

    This is a Java beginner class. Write the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and...

  • (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive...

    (Java) Rewrite the following exercise to check the user inputs to be: Valid input and positive with using try-catch (and/or throw Exception ) ************************************************************************** Write a program to prompt the user for hours and rate per hour to compute gross pay. Calculate your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Your code should have at least the following methods to calculate the pay. (VOID and NON-STATIC) getInputs calculatePay printPay Create...

  • use java program, create a class with conditions below. 1. asking user to type int number...

    use java program, create a class with conditions below. 1. asking user to type int number 2.you have to receive it use next String (not next int) 3.use try catch , if user input is not int, ask them to retype int number. (have to print out message that said only type int number)

  • Java Write code for the problem: The user enters exactly 5 scores. Use a for loop...

    Java Write code for the problem: The user enters exactly 5 scores. Use a for loop to input the scores and count the number of scores that are greater or equal to 80. Print the result after all 5 scores have been entered.

  • Use JGRASP if possible and try to keep it at a beginner level. Write a Java...

    Use JGRASP if possible and try to keep it at a beginner level. Write a Java proof-of-concept program that reads the value of today's dollar, from the user, in Euros. Your program then reads U.S. dollar values and converts each to Euros. Use 0 as a sentinel to terminate the program. Use a do while loop in this program.

  • How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting...

    How would I try/catch recovery from a format mismatch error? The scanner needs to continue prompting until the user enters a valid number. I know that I would need to try n = kbd.nextInt(); and catch a InputMismatchException and if n<1 or n>100 then I need to print Out of Range Exception. Must be in 1..100. However, I am unsure how to loop all of that. import java.io.*; import java.util.*; public class Lab5 { public static void main( String args[]...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT