Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Help making a loop please

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Help making a loop please

    I need help with making a loop. I want the user to be able to continue entering data by pressing the yes button and to stop entering data by pressing the no button. I have no clue how to go about this. I'm pretty damn stuck! So far this is all I have.

    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.swing.JOptionPane;
    public class users {
    public static void main(String[]args)throws IOException
    { 	String Fname; //user first name
    	String Lname; //user last name
    	String email; //user email address
     
    	//Get user first name
    	Fname=JOptionPane.showInputDialog("Enter user first name");
    	//Get user last name
    	Lname=JOptionPane.showInputDialog("Enter user last name");
     
    	//Get user email
    	email=JOptionPane.showInputDialog("Enter user email address");
     
    	FileWriter fw= new FileWriter("info.txt", true);
    	PrintWriter outputFile = new PrintWriter(fw);
    	outputFile.print(Fname);
    	outputFile.print(Lname);
    	outputFile.print(email);
    	int  response = JOptionPane.showConfirmDialog(null, 
    			"Would you like to enter anoter user?",
    			"Confirm",
    			JOptionPane.YES_NO_OPTION,
    			JOptionPane.QUESTION_MESSAGE);
     
     
    	outputFile.close();
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help making a loop please

    Take a look at this tutorial to figure out how to get user input: How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    When you have that part figured out, take a look at this tutorial to figure out how to create a loop: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)

    If you're still having trouble after that, post an SSCCE (that's a link) that demonstrates where you're stuck.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following 2 Users Say Thank You to KevinWorkman For This Useful Post:

    CheekySpoon (February 2nd, 2011), javapenguin (February 1st, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help making a loop please

    So this is what I came up with. I'm not quite sure if everything is in the right place since it doesn't work.

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.swing.JOptionPane;
    public class users2 {
    public static void main(String[]args)throws IOException
    { 	String Fname; //user first name
    	String Lname; //user last name
    	String email; //user email address
    	int  response = JOptionPane.showConfirmDialog(null, 
    			"Would you like to enter anoter user?",
    			"Confirm",
    			JOptionPane.YES_NO_OPTION,
    			JOptionPane.QUESTION_MESSAGE);
     
    	do{
    		//Get user first name
    		Fname=JOptionPane.showInputDialog("Enter user first name");
    		//Get user last name
    		Lname=JOptionPane.showInputDialog("Enter user last name");
     
    		//Get user email
    		email=JOptionPane.showInputDialog("Enter user email address");
     
    		FileWriter fw= new FileWriter("info.txt", true);
    		PrintWriter outputFile = new PrintWriter(fw);
    		outputFile.print(Fname);
    		outputFile.print(Lname);
    		outputFile.print(email);
     
    	}
     
     
     
    	while(response==JOptionPane.YES_OPTION);
     
     
     
    	if(response==JOptionPane.NO_OPTION)
    	{
    		outputFile.close();
    	}
     
     
     
    }
    }

  5. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Re: Help making a loop please

    Ok...that appears to be an infinite loop.

    If you put the int response = blah blah blah inside there it would exit when No is hit.

    As is...if Yes is hit...I think it'll go forever.

    Also, once it exists the loop, that means No is selected so that if statement after the do...while is unnecessary.

    Just tell the outfile to close after the loop is done.

  6. The Following User Says Thank You to javapenguin For This Useful Post:

    CheekySpoon (February 2nd, 2011)

  7. #5
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help making a loop please

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.swing.JOptionPane;
     
    public class users2 {
     
        public static void main(String[] args) throws IOException {
            FileWriter fw = new FileWriter("info.txt", true);
            PrintWriter outputFile = new PrintWriter(fw);
            String Fname; //user first name
            String Lname; //user last name
            String email; //user email address
            int response;
     
     
     
            do {
                //Get user first name
                Fname = JOptionPane.showInputDialog("Enter user first name");
                //Get user last name
                Lname = JOptionPane.showInputDialog("Enter user last name");
     
                //Get user email
                email = JOptionPane.showInputDialog("Enter user email address");
     
                response = moreInputRequest(); //ONLY REQUESTS "ANOTHER?" After details are provided.
     
                outputFile.print(Fname);
                outputFile.print(Lname);
                outputFile.print(email);
     
            } while (response == JOptionPane.YES_OPTION);
            outputFile.close();
     
        }
     
        /*Helper method, returns option pane with input*/
        private static int moreInputRequest() {
            return JOptionPane.showConfirmDialog(null, "Would you like to enter another?",
                    "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        }
    }

    This should get you going!
    Problems you had were:
    • outputFile.close() was out of the scope of the declaration
    • you were only asking if user would like to go again ONCE, and after that, the same input would be used over and over resulting in an infinite loop if user selected YES.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help making a loop please

    Hooray spoonfeeding
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #7
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help making a loop please

    Not like he didn't show any effort, all that needed doing was rearrangement of his code.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  10. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help making a loop please

    Quote Originally Posted by newbie View Post
    Not like he didn't show any effort, all that needed doing was rearrangement of his code.
    Fair enough, but I personally believe a better way to help him would have been to point out why his program was wrong - or better yet, to simply ask him what happened when he ran it.

    The only reason I didn't do this myself was because javapenguin had it covered (I'll give credit where credit is due). I was waiting to see if the OP still had questions after his response because javapenguin's hint might have been enough to give him the "a-ha!" moment that we programmers love. As it is, your response deprives him of that.

    I'm not looking to fight about it. It's just my opinion.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #9
    Junior Member
    Join Date
    Feb 2010
    Posts
    15
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Help making a loop please

    I went to class today and my professor explained to me how he preferred the code. Thanks for everyone pointing me in the right direction. It really did help me out. Me and loops do not get along! By the way I'm a she. Although I am partial to things like dirt, mud, and cars.

    So here's my final code.

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    import javax.swing.JOptionPane;
    public class users {
    	public static void main(String[]args)throws IOException
    	{String Fname; //user first name
    	String Lname; //user last name
    	String email; //user email address
     
     
     
    	FileWriter fw= new FileWriter("registration.csv", true);
    	PrintWriter outputFile = new PrintWriter(fw);
     
    	int  response = JOptionPane.showConfirmDialog(null, 
    			"Would you like to enter anoter user?",
    			"Confirm",
    			JOptionPane.YES_NO_OPTION,
    			JOptionPane.QUESTION_MESSAGE);
     
    	while(response==0)
    	{
    		//Get user first name
    		Fname=JOptionPane.showInputDialog("Enter user first name");
    		//Get user last name
    		Lname=JOptionPane.showInputDialog("Enter user last name");
    		//Get user age
    		String informedAge = JOptionPane.showInputDialog("Enter user age");
    		int age = Integer.valueOf(informedAge);
    		//Get user email
    		email=JOptionPane.showInputDialog("Enter user email address");
     
    		outputFile.print(Fname+", ");
    		outputFile.print(Lname + ", ");
    		outputFile.print(age + ", ");
    		outputFile.println(email);
     
     
    		response = JOptionPane.showConfirmDialog(null, 
    				"Would you like to enter anoter user?",
    				"Confirm",
    				JOptionPane.YES_NO_OPTION,
    				JOptionPane.QUESTION_MESSAGE);
    	}
     
    	outputFile.close();
     
    	}
     
    }

  12. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help making a loop please

    Thanks for sharing your solution for other people coming across this Thread.
    -Apologies for referring to you as a he, but just can never know on this Internet .
    If you're reading this, could you 'Mark as Solved' by going to Thread tools, at the top pane. Thanks.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  13. #11
    Junior Member
    Join Date
    Feb 2011
    Location
    Philadelphia
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Help making a loop please

    You should omit "another" in the first int variable named response, but when you re-use it in the loop, keep the "another".(nothing major) XD
    well written i like it; in fact, i love it. XD

    made this! lol

    import java.io.*;
    import javax.swing.*;
    public class average1 {
    public static void main(String[] args)throws IOException {
    String studentName, t1, t2, t3, t4, t5;
    double test1, test2, test3, test4, test5, average;
    char grade;

    FileWriter fw= new FileWriter("StudentRecords.txt", true);
    PrintWriter outputFile = new PrintWriter(fw);
    outputFile.println("Student, Test1, Test2, Test3, "
    + "Test4, Test5, Average, Grade");
    int response = JOptionPane.showConfirmDialog(null,
    "Would you like to enter student record?",
    "Confirm",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE);
    while(response==0){
    //Get student name
    studentName=JOptionPane.showInputDialog("Enter student name");
    //Get student's test score's
    t1 = JOptionPane.showInputDialog("Enter student first test score");
    test1 = Double.parseDouble(t1);
    t2 = JOptionPane.showInputDialog("Enter student second test score");
    test2 = Double.parseDouble(t2);
    t3 = JOptionPane.showInputDialog("Enter student third test score");
    test3 = Double.parseDouble(t3);
    t4 = JOptionPane.showInputDialog("Enter student fourth test score");
    test4 = Double.parseDouble(t4);
    t5 = JOptionPane.showInputDialog("Enter student fifth test score");
    test5 = Double.parseDouble(t5);
    average = (test1 + test2 + test3 + test4 + test5) / 5.0;
    if (average >= 90)
    grade = 'A';
    else if (average >= 80)
    grade = 'B';
    else if (average >= 70)
    grade = 'C';
    else if (average >= 60)
    grade = 'D';
    else
    grade = 'F';
    System.out.println("Average test score: " + average + " " + grade);
    // CSV form
    outputFile.println(studentName+", "+ test1 +", "+ test2 +", "+
    test3 +", "+ test4 +", "+ test5 +", "+
    average +", "+ grade);
    response = JOptionPane.showConfirmDialog(null,
    "Would you like to enter anoter student records?",
    "Confirm",
    JOptionPane.YES_NO_OPTION,
    JOptionPane.QUESTION_MESSAGE);}
    outputFile.close();
    }
    }
    Last edited by TimoElPrimo; February 5th, 2011 at 10:29 PM.

Similar Threads

  1. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  2. about making .class into .jar
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: November 13th, 2010, 01:50 PM
  3. Making a Craps game.
    By SOK in forum Object Oriented Programming
    Replies: 1
    Last Post: March 6th, 2010, 08:23 PM
  4. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM