I am having errors with static problems. I dont know if I post what i have done and what I am trying to accomplish and show the errors. Could someone help me out on how I should do my posts? Thanks in advance...
Amy
Printable View
I am having errors with static problems. I dont know if I post what i have done and what I am trying to accomplish and show the errors. Could someone help me out on how I should do my posts? Thanks in advance...
Amy
Post any code you have developed (use code tags), a brief description what the program should do and any exact error messages (cut & paste) that are displayed.
Luck,
CJSL
here it goies. It is a program that will go through and has to check 6 passwords and has to have 9 different methods, but it has to be in a cetain order. That is you rinstantiate a password object- that is what I think I did by making it mypassword.??? Then you have a for loop, then you need a method for startPasswordChecker, then callInput and several more. the class will have a default constructor(no parameter) that sets all the counder to zeros ( I am a little confused on this)I was stupid and wrote the whole program then compile, ended up with so many errors, started over and thought I would kind of do baby steps. So I will so the program that I have and then the errors. If I do this wrong I am sorry and if some one can tell me how to do it right I woudl apprecitate it. Thank you in advance.
here is the program I have:
import javax.swing.*; //to use JOptionPane
public class PasswordTest
{
private String passwordsBeingEntered;
private String testPlansAuthor;
private int numberOfPasswords;
private int validPassword;
private int badPassword;
private int index;
private boolean checkLength = false;
private boolean checkLowercase = false;
private boolean checkUppercase = false;
private boolean checkDigit = false;
public static void startPasswordChecker()
{
System.out.println("Please welcome to the Password Checker Program" +
"\n\nThis prgram asks the user how many passwords they choose to check" +
"\n and the computer will determine how many of the passwords are valid and how many are bad");
numberOfPasswords = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter how many " +
"passwords you would like to check?"));
testPlansAuthor = (JOptionPane.showInputDialog("Please enter your name?"));
System.out.println("the program just asked for the name of the participant");
// this string that will help with the output at the end of the program
String reportString = ("Results of Password Checker" +
"\n\nTest plans were made by " + testPlanAuthor +
"\nPasswords checked:" + numberOfPasswords +
"\n Good Passwords:" + validPassword + " bad Passwords" + badPassword);
}// end of startPasswordChecker method
public static void inputPassword()
{
passwordsBeingEntered(index)= (JOptionPane.showInputDialog("Please enter your password?"));
}//end of inputPasswordMethod
public static void main(String[] args)
{
Password mypassword = new Password();
for (index = 1; index <= numberOfPasswords; index++)
{ mypassword.startPasswordChecker();
mypassword.inputPassword();
}
}// end of main
}// end of class PasswordTest
Here are the errors I am receiving
PasswordTest.java:28: non-static variable numberOfPasswords cannot be referenced from a static context
numberOfPasswords = Integer.parseInt(JOptionPane.showInputDialog("Plea se enter how many " +
^
PasswordTest.java:33: non-static variable testPlansAuthor cannot be referenced from a static context
testPlansAuthor = (JOptionPane.showInputDialog("Please enter your name?"));
^
PasswordTest.java:39: cannot find symbol
symbol : variable testPlanAuthor
location: class PasswordTest
"\n\nTest plans were made by " + testPlanAuthor +
^
PasswordTest.java:40: non-static variable numberOfPasswords cannot be referenced from a static context
"\nPasswords checked:" + numberOfPasswords +
^
PasswordTest.java:41: non-static variable validPassword cannot be referenced from a static context
"\n Good Passwords:" + validPassword + " bad Passwords" + badPassword);
^
PasswordTest.java:41: non-static variable badPassword cannot be referenced from a static context
"\n Good Passwords:" + validPassword + " bad Passwords" + badPassword);
^
PasswordTest.java:47: non-static variable index cannot be referenced from a static context
passwordsBeingEntered(index)= (JOptionPane.showInputDialog("Please enter your password?"));
^
PasswordTest.java:47: cannot find symbol
symbol : method passwordsBeingEntered(int)
location: class PasswordTest
passwordsBeingEntered(index)= (JOptionPane.showInputDialog("Please enter your password?"));
^
Password.java:29: not a statement
"passwords you would like to check?"));
^
Password.java:29: ';' expected
"passwords you would like to check?"));
^
PasswordTest.java:55: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
PasswordTest.java:55: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
PasswordTest.java:55: non-static variable numberOfPasswords cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
PasswordTest.java:55: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
PasswordTest.java:57: cannot find symbol
symbol : method inputPassword()
location: class Password
mypassword.inputPassword();
^
Password.java:31: non-static variable testPlansAuthor cannot be referenced from a static context
testPlansAuthor = (JOptionPane.showInputDialog("Please enter your name?"));
^
Password.java:41: non-static variable index cannot be referenced from a static context
passwords(index) = JOptionPane.showInputDialog("Please enter all of your passwords one at a time?");
^
Password.java:41: cannot find symbol
symbol : method passwords(int)
location: class Password
passwords(index) = JOptionPane.showInputDialog("Please enter all of your passwords one at a time?");
^
Password.java:50: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
Password.java:50: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
Password.java:50: non-static variable numberOfPasswords cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
Password.java:50: non-static variable index cannot be referenced from a static context
for (index = 1; index <= numberOfPasswords; index++)
^
22 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Well as the error shows, u try to reference a non-static variable, from the static method main..
u either declare the variable as static (for example)
or u create an object of the class PasswordTest and than access the instance variables (object name, then a dot, then the variable)Code:private static int numberOfPasswords;
Check out the java trail's explanation on static and non-static variables.
And next time remember to use code tags. When you click the Post Reply button you will see the text area with buttons on the top. Click the button that has the symbol '#' to insert the code tags. Place all of your code in-between the tags.
Mr. Beans