1

I am using a scanner to retrieve from a textfile. When I try to access my info1 from outside of the while loop, it says variable info1 might not have been initialized but I have already initialised it outside of the while loop. how can I access it from outside the while loop with my code?

String info1,info2,info3,info4,info5,info6,info7;
boolean infoTrue = true;
do{ 
while(custInfo.hasNext())
  {

  info1 = custInfo.next();
  info2 = custInfo.next();
  info3 = custInfo.next();
  info4 = custInfo.next();
  info5 = custInfo.next();
  info6 = custInfo.next();
  info7 = custInfo.next(); 

  if(info2== loginID && info3==password)
  {
    infoTrue=false;
  }
  }
 }while(infoTrue!=false);
  System.out.println(info1);
4
  • Show us where you have initialized. Commented Apr 27, 2013 at 15:25
  • Sorry. Updated already. Commented Apr 27, 2013 at 15:27
  • Also, read this: How do I compare strings in Java? !!! Commented Apr 27, 2013 at 15:40
  • Thanks alot. sorry for my bad programming habits. Commented Apr 27, 2013 at 15:43

3 Answers 3

1
String info1,info2,info3,info4,info5,info6,info7;

Above line should be:

String info1="",info2="",info3="",info4="",info5="",info6="",info7="";
Sign up to request clarification or add additional context in comments.

Comments

1

You have to initialize it before the while loop like

String info1 = "", info2 = "";//rest is the same
//your loop

You can also use shortcut like

String info1 = info2 = info3 = "";

Comments

0

According to your code, you have two while loops, probably you might have initialized it inside first while loop, but you are accessing it outside both loops. Hence make sure you have initialized it outside both loops.

String info="";
do{
    while{
    }
while();
Sysout(info);

Also put a break point and see where it is going !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.