0

I was writing a program in which user is supposed to enter double value but I want to prompt the user again if he/she enters a string value.

This is my code so far:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in); 
    double purchaseAmount = 0; 
    if (input.hasNext("mp")) { 
        do { 
            System.out.println("Enter your loan amount: ");
            purchaseAmount = input.next(); 
        } while(!input.hasNextDouble()); 
    }
}
    
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
pattt
  • 1
  • 2
  • 1
    It depends on how your code is reading the data from the user. Unless you show us the code, your question is Too Broad. – Stephen C Jun 20 '20 at 05:35
  • @StephenC so its kind of a mortgage calculator and user has been ask to enter the loan amount and if the answer in string(eg xmbcrwf) I want to prompt them again to enter the loan amount message – pattt Jun 20 '20 at 05:48
  • 1
    That's beside the point. You need to show us your actual code if you want us to give you a meaningful answer. How are you reading the value from the user? – Stephen C Jun 20 '20 at 05:52
  • ok I'm just a beginner and I have been trying this code. public static void main(String[] args) { Scanner input = new Scanner(System.in); double purchaseAmount = 0; if(input.hasNext("mp")) { do { System.out.println("Enter your loan amount: "); purchaseAmount = input.next(); } while(!input.hasNextDouble()); – pattt Jun 20 '20 at 10:06
  • im sorry I donk know how to write the code in comment section. Im kinda new here – pattt Jun 20 '20 at 10:09
  • This is a good time to explore design pattern @DipPatel – silentsudo Jun 20 '20 at 14:42

3 Answers3

0

When you prompt a user in Java (assuming you're using System.in or a JTextField) what you're reading from user input is text.

If you want to convert the text to a double there are several approaches, but you could use:

do {
   String promptValue = prompt();
   NumberFormat nf = NumberFormat.getInstance();
   nf.setMaximumFractionDigits(16); // maximum for double
   Number num = nf.parse(promptValue):
} while (null == num);

Notice also that you could ask character by character and parse progressively so you don't need to prompt again but rather discard anything that doesn't match. This would work well in Swing or you could use jline3 if you really want to get sophisticated.

To learn how to parse progressively look at: https://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html

AminM
  • 822
  • 4
  • 11
0

If the input given by the user doesn't match the expected format then an exception will be thrown, you can catch this exception to again request for the long value.

    public static Long getLongInputFromUser(){
        Scanner sc= new Scanner(System.in);
        try {
            System.out.println("Print the long value: ");
            Long inputInteger = sc.nextLong();
            return inputInteger;
        }catch(InputMismatchException inputMismatchException){
            System.out.println("The input is not a valid long, please enter again");
            return getLongInputFromUser();
        }
    }

    public static void main(String[] args) {
        Long input = getLongInputFromUser();
        System.out.println("The final value is " + input);
    }

The output of this program is :

Print the long value: 
abc 
The input is not a valid long, please enter again 
Print the long value: 
pqr 
The input is not a valid long, please enter again
Print the long value: 
6 
The final value is 6 
Deepak Patankar
  • 3,076
  • 3
  • 16
  • 35
0

You can use Java exception mechanism

Scanner input=new Scanner(System.in);
double purchaseAmount;
while(true){
    try{
        System.out.println("please input a double value");
        purchaseAmount=input.nextDoble();
    }catch(Exception e){
        System.out.println("You input a string,not a double value");
    }
}