0

I need help with this part of code i am fairly new at coding so i am not that good below is the question Ask user to input a double(number), if the value entered is not a double it continues asking them until they enter a double. Once they enter a double it checks if the double entered is in the range (between 1-25). It will continue prompting until age1 is in that range.

Scanner input = new Scanner(System.in);

while(!input.hasNextDouble()) 
{
    input.nextLine();
    System.out.print("Input is not a number ReEnter:");
 }
 double age1 = input.nextDouble();
 while (age1 < 1 || age1 > 25)
 {
 System.out.print( "Enter the Age between 1-25 Only!");
       age1 = input.nextDouble();
         }
1

2 Answers 2

2

Your code runs just fine. Though it won't check if a valid double is entered after it reaches the second while loop, so it's best you combine the validation in a single loop:

Scanner input = new Scanner(System.in);

        while(true) 
        {
            if(!input.hasNextDouble()) {
                input.nextLine();
                System.out.print("Input is not a number ReEnter:");
            }else {         
                double age1 = input.nextDouble();
                if (age1 < 1 || age1 > 25){
                    System.out.print( "Enter the Age between 1-25 Only!");
                    input.nextLine();
                }else {
                    break;
                }
            }
         }
Sign up to request clarification or add additional context in comments.

1 Comment

I'd replace System.exit(0) with break
1

When you need to check range use && cause we need it in between those range
Additionally your condition weak toowhile (age1 < 1 || age1 > 25) it should be
while (age1 >= 1 && age1 <= 25)

Using wrapper class to convert String to double.

            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a double number:");
            String d = sc.next();
            while(true) {
                boolean dot = false;
                for(int i = 0 ; i < d.length() ; i++) {
                    char ch = d.charAt(i);
                    // checking for a decimal 'dot'
                    if(ch == '.') {
                        dot = true;
                        break;
                    }
                }
                if(dot == false) {
                    System.out.print("Input is not a double number ReEnter:");
                    d = sc.next();
                }else {
                    // wrapper class
                    double age = Double.parseDouble(d);
                    if(age >= 1 && age <= 25) {
                        System.out.println("Just prefect");
                        break;
                    }else {
                        System.out.print( "Enter the Age between 1-25 Only!");
                        d = sc.next();
                    }
                }// else block  
            }//while block

Output:

Enter a double number:1
Input is not a double number ReEnter:20
Input is not a double number ReEnter:26.00
Enter the Age between 1-25 Only!15
Input is not a double number ReEnter:15.0
Just prefect

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.