I rewrote a project and it compiled sucessfully with no errors. I then went to test the project to make sure it gave me the output I was looking for. It then went matrix. The command prompt was filled with letters jumping on the screen just like in the movie. Can anyone tell me why this is?

Dani AI

Generated

A short expert note based on the code excerpts and the replies from and .

The visible problem is an unintended infinite loop: the program keeps printing until the console looks like "The Matrix." In the excerpt the loop condition checks one variable while the switch uses a different one, so the loop-control value never becomes the exit value. That explains why called it an infinite loop. Another contributing detail: the default case uses System.out.print, which, when executed repeatedly in a tight loop, fills the screen without line breaks.

A simple, robust pattern is to read the menu choice inside the loop, validate it, then switch on that same variable. Use a do/while (or while with an explicit break) and avoid System.exit in library-style code; call the total/cleanup routine once when exiting. Example pattern (keeps input validation and accumulation separate):

Scanner in = new Scanner(System.in);
int choice;
double total = 0.0;

do {
  System.out.println("1) Dollars  2) Euros  3) Yen  4) Exit");
  while (!in.hasNextInt()) { in.next(); System.out.print("Enter 1-4: "); }
  choice = in.nextInt();

  switch (choice) {
    case 1: total += promptAndConvert(in, "dollars", DOLLAR_RATE); break;
    case 2: total += promptAndConvert(in, "euros", EURO_RATE); break;
    case 3: total += promptAndConvert(in, "yen", YEN_RATE); break;
    case 4: System.out.printf("Total saved: %.2f\n", total); break;
    default: System.out.println("Invalid selection.");
  }
} while (choice != 4);

// helper that validates a double and returns converted amount
static double promptAndConvert(Scanner in, String label, double rate) {
  System.out.print("Amount of " + label + ": ");
  while (!in.hasNextDouble()) { in.next(); System.out.print("Enter a number: "); }
  return in.nextDouble() * rate;
}

Quick troubleshooting checklist:

  • Ensure the loop condition and the switch use the same variable (no UnitOfCurrency vs currency mismatch).
  • Add a debug print of the loop variable to confirm it changes.
  • Watch mixing nextInt/nextDouble/nextLine (scanner token issues).
  • Prefer println for prompts and results so repeated prints are readable.
  • Use constants or an enum for menu choices and call the TotalSavings method once on exit rather than repeatedly.

Recommended Answers

All 10 Replies

Loop not written properly. [Probably an infinite loop]

for (int i=10; i>=0; i++)
{
}


That would be an example.

I did a while loop for a case statement:

while (UnitOfCurrency != 4)

I cannot see why it would be the cause of the problem but I am open to any suggestions at this point.

UnitOfCurrency is never equal to 4. You may be performing some kind of arithmetic that skips four:

you may start out at 3.
Then add two, which is 5. That continues the loop because it's still not four.

Well I choose 4 because I have 3 case statements and 1 default statement. The 4 choice is to exit and to add the total amount converted throughout the entire program.

Do you mind showing me the loop?

Main method:
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;


default: // total collected
System.out.print ("Exit and add the total collected.");
break;
}


second class method:


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.") ;
EnteredConvertedYen = stdin.nextDouble() ; // read in the user input
ConvertedYen = EnteredConvertedYen * YEN_CONVERSION_RATE ; // Yen to Dollar conversion
System.out.println ( ) ;
System.out.println (EnteredConvertedYen + " 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) ;


}


}

Just as I thought; it's an infinite loop. UnitOfCurrency is never set to four. Now, from what I understand you want to loop until the user enters something other than 1,2,3, correct?

Let me know exactly why you are looping and what you expect it to do so we can fix up something that will work.

Yes, that is correct. I want the user to choose from options 1-4, 1. to convert dollars, 2. to convert euros, 3. to convert yens, or 4. to exit the program (and to do a accumlative total of all money entered by users). The while loop is to move between the different types of currency.

In that case you don't need the while loop.
All you need to do is change the default case statement to exit:

switch (currency) //use a switch statement to switch currency
{

case 1:
mygifts.USDollarsEntered();
break;

case 2:
mygifts.EurosEntered();
break;

case 3:
mygifts.YenEntered();
break;

default: // total collected
System.out.print ("Exit and add the total collected.");
System.exit(0);
break;
}

There's no reason for a while loop. The variable currency is never changed in the while loop, so even if you did create a viable exit condition it would still be an infinite loop. So, just get rid of the loop and everything should work.

Ok. I will try that. Thanks for all your help.

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.