0

The program terminates do while loop before receiving false input from user. All elements work individually when tested. Only do while doesnt work when placed with other elements. the do while statement appears to be done the same way as others have suggested, and it works while running it by itself.I have searched the forums and have looked at the similar questions, and feel like i am doing it the same way others are and arent getting the same results, so I am obviously just not seeing something correctly. It doesn't work when placed inside this code. I have placed it in various places to test it out and it still wont loop. Thanks for any help in advance.

import java.util.Scanner;
import java.text.DecimalFormat;

public class Conversion {
    public static void main(String[] args) {

        // variables declared
        int startRange = 0, endRange = 0;
        double conv = 0;
        final double INCH = 2.54;
        String answer = "y";

        //initiallizing user input device

        Scanner scan = new Scanner(System.in);

        //output
        do {

            System.out.println("Please enter begining range number: ");
            startRange = scan.nextInt();

            System.out.println("Please enter ending range number: ");
            endRange = scan.nextInt();

            if (startRange < 1 || endRange < startRange + 6 || endRange >= startRange + 37) {
                System.out.print("Please enter a valid number range: ");
            } else {
                System.out.println();
                System.out.println("       Conversion Chart");
                System.out.println();
                System.out.println("    Inches      Centimeters");
                System.out.println("   ********    *************");
                System.out.println();

                for (conv = startRange; endRange >= startRange; startRange += 6) {
                    DecimalFormat fmt = new DecimalFormat("0.##");
                    conv = conv * INCH;
                    System.out.println("         " + startRange + "            " + fmt.format(conv) + "\n");
                }
            }

            System.out.println("Would you like to do another conversion (y/n)? ");
            answer = scan.nextLine();
        }while (answer.equalsIgnoreCase("y"));
    }
}
  • all elements of program work individually. it's just the do while loop wont loop and terminates. i looked at the link posted by @Scary Wombat and it didnt really help explain anything better. – Fapperdanman Mar 12 '19 at 03:13
  • before doing `answer = scan.nextLine();` do `scan.nextLine();` to consume the already pressed `` – Scary Wombat Mar 12 '19 at 03:47
  • thank you @Scary Wombat. i knew it had to be something silly i was missing. You rock! – Fapperdanman Mar 12 '19 at 03:52

0 Answers0