-1

Professor requires us to write a program that will give the user prompt to enter two float (or double) values. If the values inputted are correct then display the inputted two values. If user enters characters instead of numbers or if they enter invalid numbers then the program will display the error message and ask the user to re-enter the correct values again. It only exits when the correct input is received and displayed.

However, I wrote a program that will only work if the user input the two right doubles. Can someone helps me to change the line about catching errors? Thanks.

import java.util.Scanner;

public class FiveSecond {

    static void printMenu() {
        System.out.println("Welcome to get two doubles program:");
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        boolean valid = false;
        double first = 0;
        double second = 0;

        printMenu();

        while(!valid) {
            System.out.print("Enter two doubles, seperate by space ");

            try {
                first = Double.parseDouble(scan.next());
                second = Double.parseDouble(scan.next());
            } catch (NumberFormatException e) {
                System.out.println("Try again");
            }

            valid = true;   
        }

        System.out.println("You entered valid choice: " + first + " " +second);
        System.out.println("Thank you for giving your choice.");

        scan.close();
    }

}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Z. Zhang
  • 19
  • 3
  • 2
    Try setting `valid` to `true` inside the `try` block. As it is, it'll be `true` regardless of whether an exception was thrown. – jsheeran Nov 14 '18 at 13:45
  • @jsheeran I have tried your solution and it really helps me out. This drives me crazy all night, thank you very much. – Z. Zhang Nov 14 '18 at 13:52
  • You need to learn how to debug your code. In fact, universities should teach how to debug code before teaching how to write code. – Max Vollmer Nov 14 '18 at 13:52
  • @MaxVollmer Hi, max. Do you know somewhere to learn how to debug my code? I'm not computer science major during the college time. However, I want to get a master degree of the data science. Most programs suggest the applicants has background in Java, that's why I'm start learning write code with the UCSC extension. – Z. Zhang Nov 14 '18 at 13:59
  • Here is a good start: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Max Vollmer Nov 14 '18 at 14:00

2 Answers2

0

In addition to the previous comments, you have to be careful, because the scanner will 'remember' a previously correct double if you don't reset it :

EDITED: Thanks to @Stultuske comment

while (!valid) {
    System.out.print("Enter two doubles, seperate by space ");
    try {
        first = Double.parseDouble(scan.next());
        second = Double.parseDouble(scan.next());
        valid = true;
    }
    catch (NumberFormatException e) {
        System.out.println("Try again");
        scan.nextLine(); // <------- Important line
    }
}
Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
  • seriously? you really should NOT do that. you would actually create a new instance for each iteration? There are better ways to deal with such issues – Stultuske Nov 14 '18 at 14:08
  • @Stultuske : It will create a new instance each time the user enters a incorrect input, not at each iteration. If you know a better way to reset the content of a scanner, I will be glad to learn it. – Romain Warnan Nov 14 '18 at 14:21
  • it will iterate as long as the user inputs invalid data, so yes, it will create a new instance each iteration, which you really shouldn't. just replace that new instantiation with scan.nextLine(); that's all you need to do – Stultuske Nov 14 '18 at 14:28
  • You are right, thank you for explaining. I will edit what I wrote. – Romain Warnan Nov 14 '18 at 14:32
  • @DogEata Hi, thanks for your suggestion, but I didn't find the scanner will remember the previous correct double. e.g. I typed the 3.0 sss in the first time, and 4.0 5.0 in the second time, the scanner will only show 4.0 and 5.0. Somehow, the scanner didn't remember the input even without the line. – Z. Zhang Nov 15 '18 at 01:18
0

Try this:

catch (NumberFormatException e) {
           System.out.println("Try again");
           continue;
       }
Nick
  • 744
  • 6
  • 11