Reputation: 59
I have looked up several other problems that are similar to mine in Stack Overflow, but none of them help with my issue. So basically, my issue is that the program won't execute because it says "int cannot be dereferenced
" for the if statement. How do I solve this issue?
Well I tried to move around the curly brackets, and I tried different methods of .length(), but nothing worked.
private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {
int username, password, random;
username = Integer.parseInt(usernameTextField.getText());
password = Integer.parseInt(passwordTextField.getText());
if (password.length() != 7) {
}
}
I am using the GUI in NetBeans. The variable username and password are for text fields, and as you can see in the code I converted any inputted data for "username" and "password" to numeric values. So for the issue, I expect the if statement to display the length of the variable "password", but it instead shows an error stating, "int cannot be dereferenced
"?
Upvotes: 0
Views: 317
Reputation: 59988
You have a problem here :
if (password.length() != 7){
int
doesn't have length()
method, not like String, if you want to check the length of the password field then you can use :
if (passwordTextField.getText().length() != 7) {
Or if you are using the password as an int, and you need to check the length of the int then use :
if (String.valueOf(password).length() != 7) {
Upvotes: 1
Reputation: 6017
The primitive datatype int does not have a method length() nor does its wrapper class have such a method.
You can check the length before parsing it into an integer like:
int username, password, random;
String userName = usernameTextField.getText();
String pass = passwordTextField.getText();
if (userName.length() != 7) {
// do something
}
if (pass.length() != 7) {
// do something
}
username = Integer.parseInt(userName);
password = Integer.parseInt(pass);
Hope this helps. Good luck.
Upvotes: 1
Reputation: 1673
The compiler error is exactly correct
You have declared password as int
and int does not have length method
I think what you want to do is
private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {
String username,password,random;
username = usernameTextField.getText();
password = passwordTextField.getText();
if (password.length() != 7){
}
}
Upvotes: 1
Reputation:
The function length() is for Strings, if you want to get the digits of a number you should do like below:
int length = String.valueOf(password).length();
Upvotes: 1