user1823986
user1823986

Reputation: 87

accessing while loop variable

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);

Upvotes: 1

Views: 97

Answers (3)

Harsha
Harsha

Reputation: 503

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 !

Upvotes: 0

Achintya Jha
Achintya Jha

Reputation: 12843

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

Above line should be:

String info1="",info2="",info3="",info4="",info5="",info6="",info7="";

Upvotes: 1

Petr Mensik
Petr Mensik

Reputation: 27516

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 = "";

Upvotes: 1

Related Questions