Results 1 to 6 of 6
Thread: easy question about while loop
- 06-26-2011, 01:23 AM #1
easy question about while loop
I'm writing my first program eve. I'm using a while loop with three more while within.
In the first while statement the following code. The code asks for a name, address & email; however, I don't how to make it stop& return to the main loop when I enter the data.
thanks in advance
Java Code:while (true) { while (option1 ==1) { cname = JOptionPane.showInputDialog(null, "What is the customer's name?"); PrintWriter outputFile = new PrintWriter("customerInfo.txt"); outputFile.println(cname); caddress = JOptionPane.showInputDialog(null, "What is the custormer's address?"); outputFile.println(caddress); cemail = JOptionPane.showInputDialog(null, "What is the customer's e-mail address?"); outputFile.println(cemail); outputFile.close(); break; }
- 06-26-2011, 01:46 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Use break to break out of the inner loop.
Also, it seems like an if or switch would work better than a loop if you only plan on collecting information once. You should also wrap a file output stream with the print writer.Last edited by sunde887; 06-26-2011 at 01:48 AM.
- 06-26-2011, 01:53 AM #3
I'm don't know how to do the break that you're talking about.
I did the if statement and it does stop after I enter the data but it does not return to the main while loop.Last edited by popeyito18; 06-26-2011 at 01:57 AM.
- 06-26-2011, 02:09 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If you have two loops, an outer and an inner, break inside the inner will move to the outer loop.
Here is a working snippet so you can kind of see how it should work.Java Code:public class X{ public static void main(String[] args){ while(true){ System.out.println("Hello"); while(true){ System.out.println("In inner loop"); break; } System.out.println("Outer loop"); } } }
- 06-26-2011, 02:17 AM #5
I have several loops.. I'm debating where to put the break
Java Code:import java.util.Scanner; //Needed for Scanner class import java.io.*; import javax.swing.JOptionPane; //Needed for JOptionPane public class Proyecto // { public static void main(String[] args) throws IOException //exception thrown: Neccessary { String cname, caddress, cemail, name2; //variables that will only contain textual values int option1, menu2; //all variables that will contain numerical values Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to THE STORE"); System.out.println("Please Select One of Following"); System.out.println(" 1. Enter customer information \n 2. Price Lookup \n 3. Display Total Bill \n 4. Quit"); option1 = keyboard.nextInt(); { while (true) { while (option1 ==1) //using the if statement to pick one of the choices. In this case, option 1 {// getting all the customer information cname = JOptionPane.showInputDialog(null, "What is the customer's name?"); //asking for customer's name PrintWriter outputFile = new PrintWriter("customerInfo.txt"); //creating new text file outputFile.println(cname); //writing name to the files caddress = JOptionPane.showInputDialog(null, "What is the custormer's address?"); //asking for custormer's address outputFile.println(caddress); //exporting address to the text file cemail = JOptionPane.showInputDialog(null, "What is the customer's e-mail address?"); outputFile.println(cemail); //exporting email to file outputFile.close(); //closing the text file while (option1 ==2) {Scanner keyboard2 = new Scanner(System.in); //user input System.out.println("Choose one of the following products"); System.out.println(" 1. Shoes \n 2. T-shirts \n 3. Shorts \n 4. Caps \n 5. Jackets"); //offering customer the available options menu2 = keyboard.nextInt(); //obtaining customer's option if (menu2 == 1) System.out.println("The price for Shoes is $50.00"); //displaying results for option 1 if (menu2 == 2) System.out.println("The price for T-Shirts is $30.00"); //displaying results for option 2 if (menu2 == 3) System.out.println("The price for Shorts is $75.00"); //displaying results for option 3 if (menu2 == 4) System.out.println("The price for Caps is $15.00"); //displaying results for option 4 if (menu2 == 5) System.out.println("The price for Jackets is $100.00"); //displaying results for option 5 while (option1 ==3) {Scanner keyboard3 = new Scanner(System.in); //creating scanner for input System.out.println("Enter the customer's name"); //asking for a name name2 = keyboard.nextLine(); //creating variable name2 with the new name while (option1==4) {System.exit(0);} } } break; } } } } }
- 06-26-2011, 02:28 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Your code isn't really formatted well, you may want to look into that. However, there seems to be a bit too many loops. If the option doesn't do something you want to repeat you should not be using a loop. You generally use while loops when you want to do something x times, where x is unknown. For loops are similar, except for x is (generally) known.
You should get into the habit of wrapping ifs in {} so you don't mess up and indenting everything the same.
Notice how my indenting makes everything easier to read? Also, you want to be over cautious with the ifs or else you may end up getting errors by doingJava Code:while(true){ if(condition){ do stuff } if(condition){ do stuff } while(true){ do inner loop } }
instead ofJava Code:if(condition) statement 1 statement 2
Also, don't doJava Code:if(condition){ statement 1 statement 2 }
It's just sloppy looking.Java Code:while(true) {statement}
You want to put the loop in the loop you want to break out of. For option one I don't believe you should be using a loop since you are only performing the task once, however;
Java Code:while(true){ while(option 1){ do stuff break } }
Similar Threads
-
Easy solvable question.
By xneonx in forum New To JavaReplies: 5Last Post: 10-22-2010, 01:35 PM -
New to Java and have an easy question
By JBOY08 in forum New To JavaReplies: 1Last Post: 11-19-2008, 07:40 PM -
quick easy question
By jmHoekst in forum New To JavaReplies: 1Last Post: 06-19-2008, 03:28 PM -
Noob question- easy
By mattonitto in forum New To JavaReplies: 7Last Post: 06-13-2008, 12:26 AM -
Easy question
By JavaNoob in forum New To JavaReplies: 10Last Post: 08-03-2007, 10:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks