0

hi In my program if I enter number in the field it should display as valid value and if I enter alphabets or special characters it should be displayed as invalid value ,but when i run the following code it displays error if I enter words as input and If i enter number it is displaying correct format

  package quantity;


import java.util.Scanner;
public class Quantity {


public static void main(String[] args) {

    Scanner input =new Scanner(System.in);
    System.out.println("enter input");
    double qty = input.nextDouble();
    System.out.println(" input ="+qty);
    if(!("[_a-z_A-Z_)+").equals("qty")||
            !("[_!_~_@_#_$]").equals("qty"))
    {
        System.out.println("correct format");
    }
    // TODO code application logic here

    else
    {
        System.out.println("wrong input");
    }
}

}

following errors are displayed for words

  Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
   at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextDouble(Scanner.java:2413)
    at quantity.Quantity.main(Quantity.java:19)
D V
  • 211
  • 6
  • 13

2 Answers2

1

There are many things wrong in your solution:

  1. A string variable needs to be passed as qty, the quotes should not be there.
  2. You need to read input as a string and then decide if its a number. Hardcoding nextDouble necessitates the input of a double else it will fail
  3. You either use regular expressions or Double.parseDouble as an approach to check

Try this:

import java.util.Scanner;
public class HelloWorld{

  public static void main(String []args){
    Scanner input =new Scanner(System.in);
    System.out.println("enter input:");
    String qty = input.nextLine();
    String pattern= "^[0-9]*$";
    System.out.println(" input ="+qty);
    if(qty.matches(pattern)){

        System.out.println("correct format");
    }
    else{
        System.out.println("wrong input");
    }

  }
}

The above code is for simple numeric checks using regular expressions. This could be an implementation checking for doubles

import java.util.Scanner;
 public class HelloWorld{

 public static void main(String []args){
    Scanner input =new Scanner(System.in);
    System.out.println("enter input:");
    String qty = input.nextLine();
    System.out.println(" input ="+qty);
    try{
        Double.parseDouble(qty);
        System.out.println("correct format");
    }
    catch (NumberFormatException e){
        System.out.println("Wrong format");
    }


   }
}
Joey Pinto
  • 1,735
  • 1
  • 18
  • 34
  • 2
    This answer doesn't accept all double formats. When you type `1e10` (for example), outcome is `wrong input`, which isn't true. – zlakad May 23 '18 at 15:05
0

Or... You can catch the exception by yourself like this:

import java.util.Scanner;

public class Answer {

    private static final Scanner IN = new Scanner(System.in);

    public static void main(String[] args) {

        double qty;
        while(true){ //infinite loop
            try{
                System.out.println("Please enter the qty:");
                qty = IN.nextDouble();
                System.out.println("You entered " + qty);
                //do something with qty
            }catch(Exception e){
                System.out.println("Please input the number in double format");
                IN.next(); //consume the wrong input
            }
        }

    }

}
zlakad
  • 1,314
  • 1
  • 9
  • 16