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 5 of 5

Thread: Returning to a loop after showConfirmDialog.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Returning to a loop after showConfirmDialog.

    SOLVED. thanks snowguy13!!

    Hello everyone. I'm currently working on an assignment that requires me to have a loop counting votes until q is entered to activate a dialog box asking if I really want to quit. If I hit yes, the program ends and votes are tallied. If I hit any other key, it goes back to the first loop where it counts votes. I'm having trouble with this because when I set up the showConfirmDialog box, I have to use a while loop. When I do this, I have already left the scope of the first while loop and do not know how to re-enter it.

    Can someone help me out with this? I just need advice, I don't want the code written for me or anything. Thank you.

    public class VoteCount {
     
    public static void main(String[] args) {
     
    int yesVotes = 0, noVotes = 0, totalVotes = 0;
    char vote;
    {
        do {
     String voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
     vote = voteString.charAt(0);
     if (vote == 'Y' || vote == 'y')
         yesVotes++;
     if (vote == 'N' || vote == 'n')
         noVotes++;
        }
        while (vote != 'Q' && vote != 'q');
    }
    totalVotes = yesVotes + noVotes;
    if (vote == 'Q' || vote == 'q')
    {
        int reply = 1;
      while (reply != JOptionPane.YES_OPTION)
        reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION)
            JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes);
     
     
    }
    }
    }



    package javaapplication16;
     
     import javax.swing.JOptionPane;
     
     public class VoteCount {
     
     public static void main(String[] args) {
     
     int yesVotes = 0, noVotes = 0, totalVotes = 0;
     totalVotes = yesVotes + noVotes;
     char vote;
     int reply = 1;
     {
         do {
        String voteString;
       voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting", "Vote Now!", JOptionPane.OK_CANCEL_OPTION);
       vote = voteString.charAt(0);
         if (voteString == null || voteString.length() == 0)
                vote = 'q';
       if (voteString.toLowerCase().equals("y"))
             yesVotes++;
      if (voteString.toLowerCase().equals("n"))
             noVotes++;
     
            { 
        }
     
     if (voteString.toLowerCase().equals("q"))
     {
          reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION);
         if (reply == JOptionPane.YES_OPTION)
             JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes);
         else if (reply != JOptionPane.YES_OPTION);
     continue;
             }
          } while (reply != JOptionPane.YES_OPTION);
     
         }
         }
     }
    Last edited by qrts; December 4th, 2011 at 10:57 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Returning to a loop after showConfirmDialog.

    Here's the simplest thing I can think of: put the confirmDialog inside your first while statement. Then, if the user clicks "No", you can tell the program to just ccontinue to the next while iteration. If the user clicks "Yes", you can use a break statement, then write some code after the while loop that you want the program to execute before ending.

    Note: using a

    continue;

    statement makes the program skip all the code in the loop after it and start at the beginning of the loop's code.

    break;

    makes the program exit the loop completely and execute whatever code is after the loop.

    So to reiterate, I think you only need one while loop.

    Hopefully this helps!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Returning to a loop after showConfirmDialog.

    package javaapplication16;

    import javax.swing.JOptionPane;
     
    public class VoteCount {
     
    public static void main(String[] args) {
     
    int yesVotes = 0, noVotes = 0, totalVotes = 0;
    totalVotes = yesVotes + noVotes;
    char vote;
    {
        do {
     String voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
     vote = voteString.charAt(0);
     if (vote == 'Y' || vote == 'y')
         yesVotes++;
     if (vote == 'N' || vote == 'n')
         noVotes++;
            {
        }
     
    if (vote == 'Q' || vote == 'q')
    {
        int reply = 1;
        reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION);
        if (reply == JOptionPane.YES_OPTION)
            JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes);
        else if (reply != JOptionPane.YES_OPTION);
        continue;
            }
        } while (vote != 'Q' && vote != 'q');
     
        }
        }
    }
     
    Is this sort of what you're telling me to do? I got rid of the second while and combined them, then I put the continue after the else if statement should they answer anything but yes. When I run this, however, the program just ends after I hit "no".
     
    Thank you! It does help, quite a bit in fact. I feel like I've almost got this one.
    Last edited by qrts; December 3rd, 2011 at 08:18 PM.

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Returning to a loop after showConfirmDialog.

    One last problem, and that is fixing errors on my showInputDialog. can anyone help me? when i run the program and click ok, i get a variable voteString might not have been initialized, if i hit cancel or x then it is a null pointer exception. Thanks..

    package javaapplication16;
     
     import javax.swing.JOptionPane;
     
     public class VoteCount {
     
     public static void main(String[] args) {
     
     int yesVotes = 0, noVotes = 0, totalVotes = 0;
     totalVotes = yesVotes + noVotes;
     char vote;
     int reply = 1;
     {
         do {
        String voteString;
       voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting", "Vote Now!", JOptionPane.OK_CANCEL_OPTION);
       vote = voteString.charAt(0);
         if (voteString == null || voteString.length() == 0)
                vote = 'q';
       if (voteString.toLowerCase().equals("y"))
             yesVotes++;
      if (voteString.toLowerCase().equals("n"))
             noVotes++;
     
            { 
        }
     
     if (voteString.toLowerCase().equals("q"))
     {
          reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION);
         if (reply == JOptionPane.YES_OPTION)
             JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes);
         else if (reply != JOptionPane.YES_OPTION);
     continue;
             }
          } while (reply != JOptionPane.YES_OPTION);
     
         }
         }
     }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Returning to a loop after showConfirmDialog.

    variable voteString might not have been initialized
    The compiler wants you to give the variable a value. Give it a value of null when you define it.
    if i hit cancel or x then it is a null pointer exception.
    What variable is null? The error message gives you the line number. Find the variable that is null and make sure it has a valid value.

Similar Threads

  1. Returning location instead of value?
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2011, 04:39 PM
  2. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  3. Returning Null
    By Woody619 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: July 22nd, 2010, 12:53 PM
  4. returning a 2D array
    By straw in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2010, 04:30 AM
  5. throwing and returning
    By chronoz13 in forum Exceptions
    Replies: 6
    Last Post: October 25th, 2009, 12:00 AM