在while循环中初始化变量

时间:2013-10-04 09:30:03

标签: java string variables while-loop

我有一个程序,我需要逐字读取文件并将其保存在String变量中。这工作正常(我使用while循环),但问题是我的字符串变量在循环外没有相同的值。如果我检查while循环中变量包含的内容,我会得到超过30000个单词,但是当我在while循环之外尝试相同的东西时,只会出现1个单词。

String ord = null;
while (fil.hasNext()) {
    ord = leser.next();
    System.out.println(ord); //If I print it here I get the whole file
}
System.out.println(ord); // If i try to print out the content here, I only get 1 word

我不明白为什么变量“ord”在循环内外不包含相同内容,我该如何解决这个问题呢?我当然需要在我的程序中的其他地方使用这个变量,但由于只存储了1个单词,我的程序的其余部分不能按预期工作。

8 个答案:

答案 0 :(得分:4)

ord的价值在不断变化。因为您选择在循环期间每次打印它,您认为它包含整个文件。

您可以改为使用StringBuilder并附加字符串。

StringBuilder builder = new StringBuilder();
while (fil.hasNext()) {
    String tmp = leser.next();
    System.out.println(tmp); // If still needed
    builder.append(tmp);
}

String completeString = builder.toString();

答案 1 :(得分:1)

你变量保持重新分配,你得到最后一个。

从这里开始,

String ord = null;

// Started executing 
    while (fil.hasNext()) {
        ord = leser.next();
        System.out.println(ord); 
    }
//Ended executing

Now in the   ord  last assigned value is there 

System.out.println(ord); 

里面的内容

 // Started executing 
        while (fil.hasNext()) {
            ord = leser.next();      //new  value  assigned
            System.out.println(ord); // new value printed
        }
    //Ended executing

您可以做的是存储以前的值。

   StringBuilder builder = new StringBuilder ();     
    // Started executing 
        while (fil.hasNext()) {

            builder.append(leser.next()); //append to previous val
            builder.append(System.getProperty("line.separator"));
        }
    //Ended executing
    System.out.println(builder.toString()); 

答案 2 :(得分:1)

ord在while循环中重新初始化。在while循环的最后一次迭代中,ord包含leser.next()中存在的最后一个单词。

做一件事

String ord = null;

StringBuffer str = new StringBuffer();

while (fil.hasNext()) {

    ord = leser.next();
    System.out.println(ord);
    str.append(ord); //To retain the value of ord for future use.

}

System.out.println(str.toString); 

答案 3 :(得分:0)

变量ord在循环内外只包含一个值。在循环内部,它的值不断被重新分配和打印。但是在循环之外,它将在循环中设置最后一个值。

答案 4 :(得分:0)

您可以使用 StringBuffer 代替

StringBuffer ord = new StringBuffer;
while (fil.hasNext()) {
   ord.append(leser.next());
}
System.out.println(ord.toString());

答案 5 :(得分:0)

每次迭代都会重新分配变量ord

如果没有while循环,最明显的是,你正在做的是:

String ord = "test";
ord = "another string";

所以ord的价值只是改变了。您需要将值存储在List

List<String> ord = new LinkedList<>();
while (fil.hasNext()) {
    ord.add(leser.next());
}
System.out.println(ord);

答案 6 :(得分:0)

  

为什么变量“ord”在内部和外部不包含相同的内容   循环?

因为,String是不可变的。 while循环的每一步都会生成新的引用,并且ord的先前引用将丢失。

  

我该如何解决这个问题?

如果使用StringBuilder而不是String并将内容追加到while循环中,则会在while循环之外看到整个文件输出。

答案 7 :(得分:0)

正如您所看到的,您正逐字逐句地阅读文件,并且每次将ord变量设置为当前单词时:

String ord = null;
while (fil.hasNext()) {
     /*if the next word exist - lets set ord to the next word, means 
     each time the ord variable changes to the next word. */
    ord = leser.next();
    System.out.println(ord);
}
System.out.println(ord); //will print the last word in the file

如果要将整个文件保存在ord变量中,可以执行以下操作:

String ord = null;
while (fil.hasNext()) {
     /*if the next word exist - lets set ord to the ord variable 
     content + the next word, means we are just adding the next word*/
    ord = ord + leser.next();
    System.out.println(ord); //print current word
}
System.out.println(ord); //will print the whole file