Hi there,
I have tried for ages to figure out why this won't work and I think it must be something very
simple, but I'm unable to get it working. After the menu loop the user enters in a valid option, namely A, B, C or X. The only option that works is option A. I'm just trying to get the program functioning at the moment and I'm a beginner programmer so any assistance would be very much appreciated.
//This program allows the user to select from three menu options //The menu loops back if a valid option isn't chosen and the //program is exited if the user selects x //===================================== import java.util.Scanner; public class Assignment1{ public static void main (String [] args){ Scanner input = new Scanner(System.in); char selectedOption = 0; char primeNumberChecker = 'A'; char stampDutyCalculator = 'B'; char vowelCounter = 'C'; char exit = 'X'; //Start the menu loop while (selectedOption != primeNumberChecker && selectedOption != stampDutyCalculator && selectedOption != vowelCounter && selectedOption != exit) { //Display application menu to user System.out.println("*************** Assignment 1 Menu ****************\n"); System.out.println(primeNumberChecker + " Prime number checker"); System.out.println(stampDutyCalculator + " Stamp duty calculator."); System.out.println(vowelCounter + " Vowel counter."); System.out.println(exit + " exit option."); System.out.println("\n**************************************************"); System.out.println("Please select one of the above options: "); //Capture selected option selectedOption = input.next(".").charAt(0); if (selectedOption == primeNumberChecker) { int num; System.out.print("Enter an integer value: "); num = input.nextInt(); int i; for (i=2; i < num ;i++ ) { if (num % i == 0) { System.out.println(num + " is not a prime number!"); break; } } if(i == num) { System.out.println(num + " is a prime number!"); }else if (selectedOption == stampDutyCalculator) { final double THRESHOLD_ONE = 20000; final double THRESHOLD_TWO = 115000; final double THRESHOLD_THREE = 870000; final double THRESHOLD_FOUR = 870001; double purchasePrice; double stampDutyPayable = 0; double difference; System.out.print("Enter a purchase value: "); purchasePrice = input.nextInt(); if (purchasePrice <= 20000) { stampDutyPayable = (purchasePrice * 1.4) / 100; }else if (purchasePrice > THRESHOLD_ONE && purchasePrice <= THRESHOLD_TWO) { difference = purchasePrice - THRESHOLD_ONE; stampDutyPayable = (THRESHOLD_ONE*1.4) / 100 + (difference*2.4) / 100; }else if (purchasePrice > THRESHOLD_TWO && purchasePrice <= THRESHOLD_THREE) { difference = purchasePrice - THRESHOLD_TWO; stampDutyPayable = (THRESHOLD_TWO * 2.4) / 100 + (difference * 6) / 100; }else if (purchasePrice >= THRESHOLD_FOUR) { stampDutyPayable = (purchasePrice * 5.5) / 100; } System.out.println("Your stamp duty payable: $" + stampDutyPayable); }else if (selectedOption == vowelCounter) { char vCounter[] = new char[5]; vCounter[0] = 'a'; vCounter[1] = 'e'; vCounter[2] = 'i'; vCounter[3] = 'o'; vCounter[4] = 'u'; //Prompt user to input a string int counter = 0; System.out.println("Enter a string of text: "); //Hold the input in the String object aString String aString; aString = input.nextLine(); if (aString.isEmpty()) { aString = input.nextLine(); } for (int k = 0; k < aString.length(); k++) { char currentChar = aString.charAt(k); for (int j = 0; j < vCounter.length; j++ ) { if (currentChar == vCounter[j]) { counter++; } } } System.out.println("Number of vowels found in line is " + counter); }else if (selectedOption == exit) { System.out.println("Exiting the program - goodbye..."); } } } } }