简体   繁体   English

访问while循环变量

[英]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. 当我尝试从while循环外部访问我的info1时,它说变量info1可能尚未初始化,但是我已经在while循环外部对其进行了初始化。 how can I access it from outside the while loop with my code? 我如何从while循环之外使用我的代码访问它?

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

You have to initialize it before the while loop like 您必须在while循环之前将其初始化,例如

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

You can also use shortcut like 您也可以使用快捷方式,例如

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

上面的行应该是:

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

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. 根据您的代码,您有两个while循环,可能您已在第一个while循环内对其进行了初始化,但是您要在两个循环之外进行访问。 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 ! 还放一个断点,看看它要去哪里!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM