Results 1 to 3 of 3
  1. #1
    adonaldson is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Question JOptionPane do-while loop error trap not working... help!

    Java Code:
    package errorTrap;
    
    import java.awt.Graphics;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import java.util.Scanner;
    
    
    public class ErrorTrap extends JFrame{
    
        private static final int FRAME_SIZE = 500;
        private static final int MAX_NUM = 40;
        private static final int MIN_NUM = 10;
    
        private int numberOfLines;
        
    
        public static void main(String[] args) {
            ErrorTrap guiWindow = new ErrorTrap();
            guiWindow.setSize(FRAME_SIZE, FRAME_SIZE);
            guiWindow.setDefaultCloseOperation(EXIT_ON_CLOSE);
            Scanner keyboard = new Scanner(System.in);
            String valueString;
            
            
            //Error check loop
            
            do{
             valueString = JOptionPane.showInputDialog
                            ("Enter the number of lines in the grid (10-40): ");
             guiWindow.numberOfLines = Integer.parseInt(valueString);
            
            }
            while( numberOfLines< MIN_NUM||  numberOfLines> MAX_NUM );
                   
            guiWindow.setVisible(true);
    
        }
    }
    From what I understand this should prompt the user with a input window and if they answer a number that's not withing the appropriate range,
    then it will pop the window up again, and keep asking them till they enter a correct number.

    Yet, in the while part of the while loop, it says that I can not use numberOfLines because it is not static. I do not know what this means or how
    to fix it.

    Please help if you can.
    Thank you.
    Mandi.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: JOptionPane do-while loop error trap not working... help!

    says that I can not use numberOfLines because it is not static.
    The main method can NOT use any variables from the class that only exist when there is an instance of the class. The numberOfLines variable only exists when an instance of the class is created. If you make it static, then the main method will be able to see and use it.

  3. #3
    adonaldson is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: JOptionPane do-while loop error trap not working... help!

    Oh my thank you. I feel foolish, but it seems that the small syntax errors are the hardest. :(
    Anyways thank you so much, it works perfectly.

Similar Threads

  1. How to implement do..while loop with JOptionPane
    By gabrielpr12 in forum New To Java
    Replies: 4
    Last Post: 01-20-2012, 07:29 PM
  2. Loop not working
    By swilliams236 in forum New To Java
    Replies: 2
    Last Post: 11-07-2011, 11:36 PM
  3. Why isn't this while loop code working
    By GreenTea in forum New To Java
    Replies: 16
    Last Post: 11-10-2010, 04:14 AM
  4. while loop not working
    By RBNSN83 in forum New To Java
    Replies: 6
    Last Post: 06-21-2010, 08:29 AM
  5. How to trap java process termination events?
    By thalupularavi in forum SWT / JFace
    Replies: 0
    Last Post: 03-10-2010, 04:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •