Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problems checking user-grabbed values in while loops (only else block works)

I have a problem validating the error messages of the two while, when I fill in the right values the loop ends, when I enter incorrect values or leave blank nothing happens. Only what works is the else in each loop.

         while (true){
            try {
                System.out.print("Enter the year of manufacture of the motorcycle: ");
                m1.setYear(keyboard.nextInt());
                if (m1.getYear() <= 0){
                    System.out.println("Year field cannot be less than or equal to zero, redo the operation.");
                } else {
                    break;
                }
            } catch (Exception e) {
                System.out.println("Field must contain only integers, redo the operation.");
            }
        }
        while (true){
            try {
                System.out.print("Enter the value of the motorcycle: ");
                m1.setValue(keyboard.nextDouble());
                if (m1.getValue() <= 0){
                    System.out.println("Value field cannot be less than or equal to zero, redo the operation.");
                } else {
                    break;
                }
            } catch (Exception e) {
                System.out.println("Field must contain only decimal numbers, redo the operation.");
            }
        }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try using nextLine instead of nextInt/nextDouble:

    while (true){
        try {
            System.out.print("Enter the year of manufacture of the motorcycle: ");
            m1.setYear(Integer.parseInt(keyboard.nextLine()));
            if (m1.getYear() <= 0){
                System.out.println("Year field cannot be less than or equal to zero, redo the operation.");
            } else {
                break;
            }
        } catch (Exception e) {
            System.out.println("Field must contain only integers, redo the operation.");
        }
    }
    while (true){
        try {
            System.out.print("Enter the value of the motorcycle: ");
            m1.setValue(Double.parseDouble(keyboard.nextLine()));
            if (m1.getValue() <= 0){
                System.out.println("Value field cannot be less than or equal to zero, redo the operation.");
            } else {
                break;
            }
        } catch (Exception e) {
            System.out.println("Field must contain only decimal numbers, redo the operation.");
        }
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading