Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to figure out how to check if a user inputs a negative number or an

ID: 3851937 • Letter: I

Question

I am trying to figure out how to check if a user inputs a negative number or an alphabetic character and ask the user to re-enter the number as long as it is in the incorrect format.

Basically conditions are

1. Cannot equal a negative number
2. Cannot equal a alphabetic character
3. User must be able to re-enter into to go in to my if and else statements below the code that I have posted.

Java 7
IDE: Eclipse Neon

package RollerCoaster;

import java.util.Scanner;

public class RollerCoasterRestriction {
   public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
      
       //prompt user to enter age
       System.out.println("Enter age: ");
       double age = input.nextDouble();
      
       //prompt user to enter weight
       System.out.println("Enter weight in pounds: ");
       double weight = input.nextDouble();

Explanation / Answer

package RollerCoaster;

import java.util.Scanner;

public class RollerCoasterRestriction {
   public static void main(String[] args) {
       double weight = 0;
       Scanner input = new Scanner(System.in);

       // prompt user to enter age
       System.out.print("Enter age: ");
       double age = input.nextDouble();
       try {
           // prompt user to enter weight
           System.out.print("Enter weight in pounds(Must be positive): ");
           weight = input.nextDouble();
           if (weight < 0) {
               while (weight <= 0) {
                   System.out.print("Enter weight in pounds(Must be positive):");
                   while (!input.hasNextDouble()) {
                       input.next();
                       System.out.print("** Error Enter Numeric Value **");
                       System.out.print(" Enter weight in pounds(Must be positive): :");
                   }
                   weight = input.nextDouble();
               }
           }
       } catch (Exception e) {
           while (!input.hasNextDouble()) {
               input.next();
               System.out.print("** Error Enter Numeric Value **");
               System.out.print(" Enter weight in pounds(Must be positive): :");
               weight = input.nextDouble();
               while (weight <= 0) {
                   System.out.print("Enter weight in pounds(Must be positive):");

                   weight = input.nextDouble();
               }
           }

           weight = input.nextDouble();
       }
   }

}

______________

Output:

Enter age: 21
Enter weight in pounds(Must be positive): -44
Enter weight in pounds(Must be positive):a
** Error Enter Numeric Value **
Enter weight in pounds(Must be positive): :-56
Enter weight in pounds(Must be positive):56

___________

Output2:

Enter age: 21
Enter weight in pounds(Must be positive): 56

__________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote