简体   繁体   中英

How do i add the or use While loop in my code

I want to use the while loop so it can catch any invalid input like letters or random %%$@@...etc

Im new to java...THANKS alot for ur guys help :) here is what im working on:

import java.util.Scanner; 

public class AreaCircle {

    public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // read the keyboard
System.out.println("This program will calculate the area of a circle");

System.out.println("Enter radius:");//Print to screen

double r = sc.nextDouble(); // Read in the double from the keyboard


double area = (3.14 *r * r); 
String output = "Radius: " + r + "\n";
output = output + "Area: " + area + "\n";
System.out.println("The area of the circle  is " + area);

    }
}

put a try-catch block around nextDouble ... the doc describes what the scanner throws: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextDouble%28%29

while(True) {
  try {
    do_parsing()
    break;
  } catch (EvilException e) {
    continue;
  }
}

If the input isn't valid, nextDouble will throw InputMismatchException . You could surround your code in a do/while loop where you catch the exception and break the loop upon receiving valid input.

boolean error = false;
do {
    try {
        System.out.println("Enter val: ");
        Scanner kbd = new Scanner(System.in);
        double r = kbd.nextDouble();
        error = false;
    } catch (InputMismatchException e) {
        error = true;
    }
}while(error);

It is really simple to handle the user input...

I recently have a program that detects invalid user input...

Here is what I did using the loop:

static String[] letters = {"a","b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y"};

//declare an array of letters or number for your case
//and make a method to check if input key is present at the array..

 public int getIndexOf(char key){

        int charindex = -1;
        for(int index=0; index<letters.length; index++){            
            if(symbols[index] == key){
               charindex = index;
            }
        }

        return charindex;
    }

Then for your input: //so you could iterate though your input

boolean sentinel = false;
char[] numbervalue= input.getText().toString().toCharArray();

 for (int z = 0; z < numbervalue.length; z++) {
                    int m = bal.getIndexOf(plaintext[z]);
                  if(m == -1){
                   sentinel = false;
                  break;
}
                }

Then you can do the checking...

if(sentinel){
//prompt the user that the input contains invalid characters
}else{
//continue with the processing....
}

Personally, I would not use a while loop, I would use a try/catch exception handler. You could put a while loop around it, but I'm not sure why you would do that. There's a built in exception called NumberFormatException and it's made for errors like the one you said. All you do is

try {
    double r = sc.nextDouble();
}
catch( NumberFormatException e ) {
    //put a message or anything you want to tell the user that their input was weird.
}

Literally, all it's doing is, if whatever entered is not a number, then go into the catch block and print your message. If you'd like, put everything there in a while loop for the sake of having a while loop. Hope this works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM