-1

if I'm entering a string like hello the program will work and say there is an error and ask user to input a radius again, but if I enter something like Hello World 123 it will give me the error and the calculation. How can I check the input and make it stay in the loop if there is string characters entered?

import java.util.Scanner;

public class ComputeArea {
    public static void main(String[] args) {
        double radius; //define radius variable
        double area; //define area variable
        double pie = 3.14159; //define pie as 3.14159
        boolean inputIsAString = false;

        //Import Scanner library to allow user to input value
        System.out.print("Please enter radius to calculate area: ");
        Scanner input = new Scanner(System.in);

        //Is the input a number? If not print error and redirect to input statement
        while (!input.hasNextDouble() && !inputIsAString) {
            System.out.println("");
            System.out.println("*********************************");
            System.out.println("Error Please enter a valid number");
            System.out.println("*********************************");
            System.out.println("");
            System.out.print("Please input radius: ");
            input.next();
            if (input.next().matches("[a-zA-Z]")) {
                System.out.println("user enter string");
                inputIsAString = true;
            } else {
                inputIsAString = false;
            }
        }

        //Calculate the area with the given radius
        radius = input.nextDouble();
        area = radius * radius * pie;

        //Print out the area
        System.out.println("The area of a circle with a radius of " + radius + " is equal to: " + area);
    }
}
Acer79
  • 145
  • 10
  • 2
    Does this answer your question? [Validating input using java.util.Scanner](https://stackoverflow.com/questions/3059333/validating-input-using-java-util-scanner) – Tom Jan 18 '21 at 14:56
  • you can use nextInt() method – Joe Taras Jan 18 '21 at 14:56
  • Aside from your question, you should use Math.PI instead of defining it yourself. – Jems Jan 18 '21 at 14:58

2 Answers2

0

The best way would be to put your while loop operation inside a separate function (or method), rather than keeping it in main. That way, you could recursively call the function until the correct input is entered.

String yourMethod() {
   if(INPUT_IS_ACCEPTABLE) {
   //do stuff
   return YOUR_STRING;
   }
yourMethod(); //recursive call
return null;
}

This way, your method will keep calling itself until it has acceptable input.

on-the-cut
  • 21
  • 2
0

Use a do-while loop like below. do take input while use has not entered a number. Here is the modified code below-

import java.util.Scanner;

public class CircleRadius {
    public static void main(String[] args) {
        double radius; //define radius variable
        double area; //define area variable
        double pie = 3.14159; //define pie as 3.14159
        boolean inputIsAString = true;
        do{
            System.out.print("Please enter radius to calculate area: ");
            //Import Scanner library to allow user to input value
            Scanner input = new Scanner(System.in);
            //Is the input a number? If not print error and redirect to input statement
            if(input.hasNextDouble()){
                //Calculate the area with the given radius
                radius = input.nextDouble();
                area = radius * radius * pie;
                //Print out the area
                System.out.println("The area of a circle with a radius of " + radius + " is equal to: " + area);
                inputIsAString= false;
            }else{
                System.out.println("");
                System.out.println("*********************************");
                System.out.println("Error Please enter a valid number");
                System.out.println("*********************************");
                System.out.println("");
                inputIsAString= true;
            }
        }while (inputIsAString);
    }
}
Vipul Kumar
  • 415
  • 2
  • 10