Reputation: 1
I have to create a java code that creates labels for packages. When the user inputs "y" or "Y" in response to "Would you like to create more" it will go through the loop again. If the user enters anything other than "y" or "Y", it will exit the loop. The problem I'm having is that after a user inputs all the information and is asked the question, if the user says anything other than y or Y it does exit the program, but it doesn't output any of the label information. If I enter y or Y, it outputs the label information correctly and runs through the loop again perfectly. Just wondering if anyone can help me see what I'm doing wrong so that I can get it to output the label info after I enter anything other than y or Y.
import javax.swing.JOptionPane;
public class MailOrderAFG //Define my public class
{
public static void main(String [] args)
{
String title, firstName, lastName, streetAddress, city, state, zip, numBoxesInput, enterAnother;
String a = "y", b = "Y";
title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
firstName = JOptionPane.showInputDialog("Enter first name: ");
lastName = JOptionPane.showInputDialog("Enter last name: ");
streetAddress = JOptionPane.showInputDialog("Enter street address: ");
city = JOptionPane.showInputDialog("Enter city: ");
state = JOptionPane.showInputDialog("Enter state: ");
zip = JOptionPane.showInputDialog("Enter zip code: ");
numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");
int numBoxes = 0;
int i;
while(enterAnother.equals(a) || enterAnother.equals(b)) //priming read
{
numBoxes = Integer.parseInt(numBoxesInput); //changing string to int so we can do arithmatic with it
{
for (i = 1; i <= numBoxes; i++) { //adding one until i is equal to numBoxes, then it will exit the loop
System.out.println(title + " " + firstName + " " + lastName);
System.out.println(streetAddress);
System.out.println(city + ", " + state + " " + zip);
System.out.println("Box " + i + " of " + numBoxes);
System.out.println();}
title = JOptionPane.showInputDialog("Enter title (Mr., Mrs., Ms., etc.): ");
firstName = JOptionPane.showInputDialog("Enter first name: ");
lastName = JOptionPane.showInputDialog("Enter last name: ");
streetAddress = JOptionPane.showInputDialog("Enter street address: ");
city = JOptionPane.showInputDialog("Enter city: ");
state = JOptionPane.showInputDialog("Enter state: ");
zip = JOptionPane.showInputDialog("Enter zip code: ");
numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order: ");
enterAnother = JOptionPane.showInputDialog ("Do you want to produce more labels? Y or N ");
}
}
System.exit(0);
}
}
Upvotes: 0
Views: 89
Reputation: 6439
Your prints are inside of your loop, which doesn't run again after the exit condition is met.
I suggest you make the following changes:
while-do
to a do-while
.Or, if you need to know if the user has stopped input elsewhere:
boolean isRunning = true
variable. This will be your new loop-exit condition.y
, set the exit condition to false
.while
loops condition to simply be while(isRunning)
Unrelated to the problem, but also important:
a
and b
, are not needed.
main
class, meaning the whole duration of the program.a
and b
. Except for a few specific cases like in for-loop
indexes (usually called i
, for index), your variables should have meaningful names. In this case, they aren't needed, but they would be named something like exitCharLowCase
and exitCharHighCase
.
JOptionPane
) twice, you could have it in a function, and call that function twice instead, simplifying your code, and reducing maintenance work (only has to find and change one code instead of two; good practice for future).Objects
in Java.
title
, firstName
, lastName
, //etc...
variables can be turned into an object, allowing you to deal with all those variables as if they were just one variable with "child" variables.Upvotes: 1
Reputation: 14121
I can see you created the loop by trying to copy and paste the original code, but the resulting code's workflow isn't correct.
See, your code is currently like this:
FETCH INFO FROM USER
ASK IF USER WANTS MORE
while (WANTSMORE)
PRINT INFO
FETCH INFO FROM USER
ASK IF USER WANTS MORE
end while
end program
See, it should actually be simply this:
do
FETCH INFO FROM USER
ASK IF USER WANTS MORE
PRINT INFO
while (WANTSMORE)
end program
See if you can reorder your code in order for the workflow to be corrected. Check this link if you need info on do-while
.
Upvotes: 2