JOptionPane do-while loop error trap not working... help!
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.
Re: JOptionPane do-while loop error trap not working... help!
Quote:
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.
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.