Connecting Tech Pros Worldwide Help | Site Map

Getting an input out of a "while"

Newbie
 
Join Date: Oct 2009
Posts: 2
#1: 3 Weeks Ago
I have placed an input JOptionPane and get input and then qualify the input but then when I try to use the input to qualify when it is entered again it says the variable was not initialized. How do I get the input info out of the While into the rest of the program?

import javax.swing.*;
public class Password
{
public static void main (String[] args)
{
String len= "f";//Length correct
String alph = "f";//Letter Present
String num = "f";//Number Present
String f = "f";
String t = "t";
String password;
String password2;

while (len.equals(f) || alph.equals(f) || num.equals(f))
{
len = "f";
alph = "f";
num = "f";
int x = 0;
char oneChar = 'f';

password = JOptionPane.showInputDialog("Please enter a password that is: " +
"\n6 to 10 characters long and contain at least " +
"\none number and one letter.");

// Start checking the entry
x = password.length();
if (x<=10 && x >= 6)
len = "t";
else len = "f";
for (int k = 0; k < x ; k++)
{
oneChar = password.charAt(k);
if (alph.equals(f))
{
if (Character.isLetter(oneChar))
{alph="t";
}}
if (num.equals(f)){
if (Character.isDigit(oneChar))
num="t";
else num = "f";
}}

}

do
password2 = JOptionPane.showInputDialog("Please re-enter the password to verify ");
while (!password.equals(password2));


JOptionPane.showMessageDialog(null, "Thank You for entering your password!");

}

}
best answer - posted by pbrockway2
You must initialise variables before you use them.

You have written something like:

Expand|Select|Wrap|Line Numbers
  1. String foo;
  2. while(condition)
  3. {
  4.     foo = "some value";
  5. }
  6. doSomethingWith(foo);
  7. foo.whatever();
  8. String bar = foo;
  9.  
None of the last three statements are going to be valid. That's because the compiler (which can't/won't try very hard) can't see that foo is ever given a value. And if it's not given a value then that (nonexistent) value can't be passed as a mehod argument, dereferenced or assigned to anything else.

A different matter: your logic in that method is getting rather tangled. Consider breaking it up into different methods (obtainInput() validateInput(), verify() etc). That way you can avoid the nested structures. Also I would advocate using braces. Always.
Newbie
 
Join Date: Nov 2007
Posts: 27
#2: 3 Weeks Ago

re: Getting an input out of a "while"


You must initialise variables before you use them.

You have written something like:

Expand|Select|Wrap|Line Numbers
  1. String foo;
  2. while(condition)
  3. {
  4.     foo = "some value";
  5. }
  6. doSomethingWith(foo);
  7. foo.whatever();
  8. String bar = foo;
  9.  
None of the last three statements are going to be valid. That's because the compiler (which can't/won't try very hard) can't see that foo is ever given a value. And if it's not given a value then that (nonexistent) value can't be passed as a mehod argument, dereferenced or assigned to anything else.

A different matter: your logic in that method is getting rather tangled. Consider breaking it up into different methods (obtainInput() validateInput(), verify() etc). That way you can avoid the nested structures. Also I would advocate using braces. Always.
Newbie
 
Join Date: Nov 2007
Posts: 27
#3: 3 Weeks Ago

re: Getting an input out of a "while"


Oh! And I've just read the code! What's this "t" and "f" business?

Try something along the lines of:

Expand|Select|Wrap|Line Numbers
  1. boolean lenOK = false;
  2. boolean alphaOK = false;
  3. boolean numberOK = false;
  4. //...
  5. while (!lenOK || !alphaOK || !numberOK)
  6. {
  7.     //...
  8.  
The boolean type is custom made for conditions that can be true or false. See, for instance Primitive Data Types in Sun's Tutorial.
Newbie
 
Join Date: Oct 2009
Posts: 2
#4: 3 Weeks Ago

re: Getting an input out of a "while"


Thanks for the reply I did get to clean up my code with the boolean, What I needed to do was to place the variable outside the the while loop. Now it works!
Reply