簡體   English   中英

在while循環內初始化變量

[英]Initializing variable inside a 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個字,因此程序的其余部分無法正常工作。

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

您的變量不斷重新分配,您將獲得最后一個。

從這里開始

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

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

變量ord在循環內和循環外僅包含一個值。 在循環內部,其值會不斷重新分配和打印。 但是在循環之外,它將在循環中設置最后一個值。

您可以改為使用StringBuffer

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

您每次迭代都要重新分配變量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);

為什么variabel“ ord”在循環的內部和外部不包含相同的內容?

因為,String是不可變的。 while循環的每一步都會生成新的引用,而ord的先前引用會丟失。

我該如何解決?

如果使用StringBuilder而不是String並將內容附加到while循環內,則將在while循環外看到整個文件輸出。

如您所見,您正在逐字讀取文件,並且每次將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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM