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);
user1823986
  • 87
  • 3
  • 11

3 Answers3

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 = "";
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
1
String info1,info2,info3,info4,info5,info6,info7;

Above line should be:

String info1="",info2="",info3="",info4="",info5="",info6="",info7="";
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
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 !

Harsha
  • 505
  • 5
  • 11