Question: Can someone please help me with this java homework? The problem I'm having is with the method getBase(Scanner in). I'm trying to get it to
Can someone please help me with this java homework? The problem I'm having is with the method getBase(Scanner in). I'm trying to get it to where it will keep looping until the user enters 2, 8, or 16. As of right now, the while loop in the getBase method just keeps on looping no matter what number I enter.
public class Hw { private static final int EXIT = -1;
public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Welcome to the Decimal Converter program. Enter a positive integer and either base 2, 8, or 16 and the program will" + " display the equivalent number in the new base. To quit, enter q at the first prompt."); System.out.print("Enter a positive integer, or " + EXIT + " to quit: "); int x = getNum(console); System.out.print("Enter the new base (2, 8, or 16): " ); int y = getBase(console); while (x != EXIT) //Keeps asking the user for input until they enter -1 { System.out.println(" " + x + " (base-10) is equivalent to " + Conversion.baseCon(x, y) + " (base-" + y+ ")"); System.out.print(" " + "Enter a positive integer, or " + EXIT + " to quit: "); x = console.nextInt(); if (x != EXIT) { System.out.print("Enter the new base (2, 8, or 16): "); y = console.nextInt(); } } System.out.println("Good-bye"); } public static int getNum(Scanner in) { int userInput = getInt(in); while (userInput <= 0 && userInput != EXIT) { System.out.print("Please enter an integer bigger than 0 or " + EXIT + " to quit: "); userInput = getInt(in); } return userInput; } public static int getBase(Scanner in) { int userInput = getInt(in); while (userInput != 2 || userInput != 8 || userInput != 16) { System.out.println("Sorry, that is an invalid base. Please enter 2, 8, or 16 only."); System.out.print("Enter the new base (2, 8, or 16): "); userInput = getInt(in); } return userInput; } public static int getInt(Scanner in) { while (!in.hasNextInt()) { in.next(); System.out.print("Error. Please enter a integer: "); } return in.nextInt(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
