2013-10-04 205 views
2

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

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”在循环内部和外部不包含相同的内容,我该如何解决这个问题?我当然需要在我的程序中的其他地方使用这个变量,但由于只存储了一个单词,我的程序的其余部分不能按预期工作。

+0

尝试concate,而不是覆盖它 – upog

+0

您是刚开始的最后一个字外循环读取。在内部循环中,单词被打印在不同的迭代上。您应该将它们添加到列表中,或通过连接它们来创建一个'StringBuilder'。 –

回答

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

你变量继续重新分配,你会得到最后一个。

从这里开始,

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()); 
+0

谢谢,这工作得很好:)只有一个问题,现在所有的单词都写在一个块中,而在他们写出每行之前。有什么办法可以用StringBuilder做到这一点? – user2795095

+0

@ user2795095是的。您可以在每次追加后附加一个新行。我将更新我的帖子。请看我的更新 –

+0

太棒了,现在我一字不变。谢谢:) – user2795095

0

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

0

您可以使用StringBuffer的代替

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

'StringBuilder'通常是首选的,除非需要线程安全(以及引起的开销)。 –

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

为什么variabel“ORD”不包含相同的内部和 外循环?

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

我该如何解决这个问题?

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

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内变量,你可以做这样的事情:同时loop.So在while循环,ORD包含目前在leser.next的最后一个字()最后一次迭代

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 
1

ORD重新初始化内。

做一件事

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