r/learnjava • • Nov 16 '15

help with switching from while to for and adding array

Ok, so this is my original code that takes the input and uses it to make shipping labels. I used 'while' loops to do it and now I'm supposed to switch to a 'for' and use an array for all the user input instead of individual Strings.

public static void main(String[] args) { // declare variables String title; String firstName; String lastName; String streetAddress; String city; String state; String zip; String numBoxesInput; String enterAnother = "Y"; int numBoxes; int count;

    // priming read of mail order input from the user
    title = JOptionPane.showInputDialog("Enter title (Mr., Ms., Dr., etc.): ");
    firstName = JOptionPane.showInputDialog("Enter first name: ");
    lastName = JOptionPane.showInputDialog("Enter lastname: ");
    streetAddress = JOptionPane.showInputDialog("Enter street address: ");
    city = JOptionPane.showInputDialog("Enter city: ");
    state = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
    zip = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");

    numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order:");
    numBoxes = Integer.parseInt(numBoxesInput);

    //while

     while(enterAnother.compareToIgnoreCase("N") != 0)
     {
     	count = 1;
     	while(count <= numBoxes)
     	{
     					System.out.println(title + " " + firstName + " " + lastName);
		                System.out.println(streetAddress);
		                System.out.println(city + ", " + state + " " + zip);
		                System.out.println("Box " + count++ + " of " + numBoxes);
		                count = count++;
		                System.out.println();
		}

		// ask the user if finished entering mail orders
        enterAnother = JOptionPane.showInputDialog("Do you want to produce more labels? Y or N");
		//When prompted, if the user inputs Y or y, this if will complete the steps to make more labels.
			if(enterAnother.equalsIgnoreCase("Y"))
			{

			title = JOptionPane.showInputDialog("Enter title (Mr., Ms., Dr., etc.): ");
			firstName = JOptionPane.showInputDialog("Enter first name: ");
			lastName = JOptionPane.showInputDialog("Enter lastname: ");
			streetAddress = JOptionPane.showInputDialog("Enter street address: ");
			city = JOptionPane.showInputDialog("Enter city: ");
			state = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
    		zip = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");
			numBoxesInput = JOptionPane.showInputDialog("Enter number of boxes in the order:");
			numBoxes = Integer.parseInt(numBoxesInput);
			count = 1;

			}


	}

	System.exit(0);
}

}

and this is where I am in my new code String[] labelInfo = new String[8]; String enterAnother = "Y"; int numBoxes; int count;

    // priming read of mail order input from the user
    labelInfo[0] = JOptionPane.showInputDialog("Enter title (Mr, Ms, Dr, etc.): ");
    labelInfo[1] = JOptionPane.showInputDialog("Enter first name: ");
    labelInfo[2] = JOptionPane.showInputDialog("Enter last name: ");
    labelInfo[3] = JOptionPane.showInputDialog("Enter street address: ");
    labelInfo[4] = JOptionPane.showInputDialog("Enter city: ");
    labelInfo[5] = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
    labelInfo[6] = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");

    labelInfo[7] = JOptionPane.showInputDialog("Enter number of boxes in the order:");
    numBoxes = Integer.parseInt(labelInfo[7]);

    // Outer while

     while(enterAnother.compareToIgnoreCase("N") != 0)
     {
     	count = 1;
     	//inner while that outputs user input on display while loop is running
     		while(count<= numBoxes)
     		{
     			System.out.println(labelInfo[0] + " " + labelInfo[1] + " " + labelInfo[2]);
		  		System.out.println(labelInfo[3]);
		    	System.out.println(labelInfo[4] + ", " + labelInfo[5] + " " + labelInfo[6]);
		    	System.out.println("Box " + count++ + " of " + numBoxes);
		    	count = count++;
		    	System.out.println();
			}

		//Asks the user if finished entering mail orders
         enterAnother = JOptionPane.showInputDialog("Would you like to make more labels? Y or N");
		//When prompted, if the user inputs Y or y, this if statement will complete the steps to make more labels. If not, the control loop will exit the program.
			if(enterAnother.equalsIgnoreCase("Y"))
			{

				labelInfo[0] = JOptionPane.showInputDialog("Enter title (Mr, Ms, Dr, etc.): ");
				labelInfo[1] = JOptionPane.showInputDialog("Enter first name: ");
				labelInfo[2] = JOptionPane.showInputDialog("Enter last name: ");
				labelInfo[3] = JOptionPane.showInputDialog("Enter street address: ");
				labelInfo[4] = JOptionPane.showInputDialog("Enter city: ");
				labelInfo[5] = JOptionPane.showInputDialog("Enter state (IL, MO, etc.): ");
    			labelInfo[6] = JOptionPane.showInputDialog("Enter zip (e.g., 62025): ");
				labelInfo[7] = JOptionPane.showInputDialog("Enter number of boxes in the order:");
				numBoxes = Integer.parseInt(labelInfo[7]);

			}


	}

	System.exit(0);
}

}

1 Upvotes

1 comment sorted by

1

u/shadowfactsdev Nov 16 '15

And your problem is...