Why is it after I compile the program sucessfully, the options will not work? Only one out of my four case statements actually execute.
Finalmadness 0 Newbie Poster
Dani AI
Generated
A quick, practical diagnosis for readers who find a compiled menu program only ever running one option: the usual culprits are switching on the wrong variable or reading the menu choice only once before a loop. noted moving the input read into the while loop fixed the symptom — that change forces the program to fetch a fresh choice each iteration so the switch can act on the newest value.
Two implementation mistakes to watch for. First, make sure the value used in the switch is actually the variable you read from the user; if you switch on a different variable that never changes, none of the intended cases will match. Second, reading the menu choice once before entering a loop and never updating it means the loop keeps working with the same value. Removing break statements causes fall-through (multiple cases executing) and is rarely the correct fix; breaks are there to isolate each menu action.
Practical fixes and hardening tips: always print the menu and read the user choice inside the loop (or use a do/while that prompts first), and ensure the switch expression references that freshly read variable. Add a default case to catch invalid selections and re-prompt. Validate input (hasNextInt or try/catch around nextInt/nextDouble) to avoid InputMismatchException or stuck input. Use one Scanner for System.in and reuse it rather than creating several. Choose clear variable names (for example, choice) or constants/enums for menu values so the intent is obvious.
If you’re summing money repeatedly, prefer BigDecimal for exact cents and format output for readability. Thanks to and for their suggestions — the root issue here was an input/variable mismatch and loop placement, not the switch syntax itself.
Recommended Answers
Jump to Post— sam1 0hi
may be you have used your statements in that way to only print the last statement e.g. the first three are false and last becomes true.
All 4 Replies
sam1 0 Posting Whiz
hi
may be you have used your statements in that way to only print the last statement e.g. the first three are false and last becomes true.
server_crash 64 Postaholic
Take out the break statements if you want all them to execute, but that probably means your logic is flawed.
Finalmadness 0 Newbie Poster
I tried to correct the program and am still having the same problem. Can you please take a look to see if anything jumps out at you. Thanks. Here is the program:
import java.util.*;
public class GraduationMoney
{
public static void main (String[] args) // start main method
{
// class method to call gift class
Gift mygifts = new Gift ();
// set up input stream
Scanner stdin1 = new Scanner (System. in);
// define varaibles
int UnitOfCurrency = 0;
double EnteredAmount = 0;
double answer = 0;
int currency = -1;
// Description of Program
System.out.print ("This program will convert dollars, euros, and yen");
System.out.println (" to dollars.");
System.out.println ();
// Users options for selection
System.out.println ("Press 1 to convert USDollars to dollars.");
System.out.println ("Press 2 to convert Euros to dollars.");
System.out.println ("Press 3 to convert Yen to dollars.");
System.out.println ("Press 4 to exit the program.");
System.out.println ("Please make a selection");
System.out.println ( );
UnitOfCurrency = stdin1.nextInt();
while (UnitOfCurrency != 4) // set up while statement
{
switch (currency) //use a switch statement to switch currency
{
case 1:
mygifts.USDollarsEntered();
break;
case 2:
mygifts.EurosEntered();
break;
case 3:
mygifts.YenEntered();
break;
case 4: // total collected
mygifts.TotalSavings();
break;
}
}
}
}
Gift class
import java.util.*;
public class Gift
{
Scanner stdin = new Scanner (System.in);
// define variables
double EnteredDollars, EnteredEuros, EnteredYen, TotalSaving = 0;
double ConvertedDollars, ConvertedEuros, ConvertedYen = 0;
double total = 0;
// define constants
final double EURO_CONVERSION_RATE = 1.24 ;
final double YEN_CONVERSION_RATE = 0.0092 ;
final double DOLLARS_CONVERSION_RATE = 1.0 ;
//method to convert Dollars to Dollars
public void USDollarsEntered ()
{
System.out.println ("Please enter the number of Dollars.") ;
EnteredDollars = stdin.nextDouble() ; // read in the user input
ConvertedDollars = EnteredDollars * DOLLARS_CONVERSION_RATE ; // Dollar to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredDollars + " dollars equals " + ConvertedDollars + " dollars.") ;
total += ConvertedDollars;
}
//method to convert Euros to Dollars
public void EurosEntered ()
{
System.out.println ("Please enter the amount of Euros.") ;
EnteredEuros = stdin.nextDouble() ; // read in the user input
ConvertedEuros = EnteredEuros * EURO_CONVERSION_RATE ; // Euro to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredEuros + " euros equals " + ConvertedEuros + " dollars.") ;
total += ConvertedEuros;
}
//method to convert Yen to Dollars
public void YenEntered ()
{
System.out.println ("Please enter the number of Yen.") ;
EnteredYen = stdin.nextDouble() ; // read in the user input
ConvertedYen = EnteredYen * YEN_CONVERSION_RATE ; // Yen to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredYen + " yen equals " + ConvertedYen + " dollars.") ;
total += ConvertedYen;
}
//method to calculate Total Savings
public void TotalSavings()
{
TotalSaving = total;
System.out.println ( ) ;
System.out.println ("You have choosen to quit the program.") ;
System.out.println ("Your total amount of savings is " + TotalSaving) ;
}
}
Edited by happygeek because: fixed formatting
Finalmadness 0 Newbie Poster
I actually figured it out. I needed the stdin in the while loop. Simple. Thanks everyone for your help. I really appreciate it more than you know.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.