2014-10-28 47 views
2

我試圖找到一個特定的點,從位置0開始計數。繼承人是我到目前爲止有:如何創建一個變量來保存文本文件中的所有數據而不使用has.next方法?

import java.util.Scanner; 
import java.io.*; 
public class SearchFile { 
    public static void main (String [] args) throws IOException { 
     int count = 0; 


     File text = new File ("TEXT.txt"); //Makes new object of File Class called text and creates  a text document called whatever they want 
     Scanner reader = new Scanner (text); //makes object of Scanner class called reader to read from the text file 
     Scanner finder = new Scanner (System.in); //makes object of Scanner class called finder to store input 

     System.out.print ("Please enter a file name: "); 
     String name = finder.nextLine(); 
     System.out.print ("Please enter a word you want me to search for: "); 
     String check = finder.nextLine(); 

     while (reader.hasNextLine()){ 
      String word = reader.next() + " "; 
      if (word.equalsIgnoreCase (check)){ 
       System.out.println ("Searching in file name: " + name + "...\nThe word " + check + " occurs " + count + " time in the text file."); 
       count++; 
      } 
     } 
     if (count > 0) { 
      int occurs = word.indexOf(check); 
      System.out.println ("The word " + check + " occurs first at index " + occurs + ".");  
     } 
     if (count == 0){ 
      System.out.println ("Sorry! Unable to find word"); 
      return; 
     } 
    } 
} 

我遇到的問題是,我的「word」只有同時在循環的值。因此使我無法在循環之外使用它。誰能幫我?也許給我一些新的東西,我還沒有嘗試過呢?

+0

在循環之前聲明'word'? – Tetramputechture 2014-10-28 04:20:05

+0

是的,但作爲什麼?如果我只聲明沒有任何東西,它將保持不變,並且在while循環期間仍然只有一個值 – 2014-10-28 04:20:41

+0

正確的,就像你在循環外面聲明count一樣,把'word'放在循環後面的行上。這就是所謂的「範圍」,順帶一提。 – markspace 2014-10-28 04:20:49

回答

0
String word = reader.next() + " "; 

您已在您的while代碼塊中聲明「word」。所以,系統只有在循環內部知道這個變量,並且一旦它在循環之外,它就會忘記它。如果你想在循環外使用變量,你應該在外面聲明它。我明白你曾嘗試過一次。現在,即使在這之後,爲什麼單詞在循環之外具有相同的值? 「word」取自文件中的值,即讀者通過reader.next()給出的值。所以,一旦你在循環之外,它仍然會有最後一個值reader.next()提供。如果你想查看單詞的值,只需在賦值後立即給出一個打印語句。

String word=""; 
    while (reader.hasNextLine()){ 
    word = reader.next() + " "; 
    System.out.println("Word:"+word); 

      | 
      | 
      | 

    } 

注意:如果你想word變量來保存文件的全部的話,那麼這樣做。

word=word+reader.next();

+0

如果我在循環之外聲明詞,它會有什麼價值?在循環內部它具有文本文件的值,但在其外部沒有任何內容。那麼,如何在循環內保留與循環內部相同的值的同時,如何在循環外部獲得單詞? – 2014-10-28 04:49:49

+0

當你說聲明 - 這意味着告訴這個 - 「字符串字」;但是,如果在聲明過程中爲它指定一個值 - 「String word =」「;」,那麼它會告訴編譯器word是一個指向null值的String對象。在循環的每一次迭代中,都可以看到分配給單詞變量的不同值。一旦你退出循環,最後分配的值將保持不變。如果你想要整個文件,請做這個 - word = word + reader.next(); – user1930402 2014-10-28 05:04:10

+0

我明白你在說什麼,但如果我不給它賦值,我會得到一個錯誤,如果我確實聲明它並使用while循環來獲取整個文件,那麼在我的while語句期間循環,我不會得到一個空白,因爲它會指向我分配「字符串單詞」時。我試過了,我似乎無法查明原因,它只是跳過while循環中的if語句,並推斷word.equalsIgnoreCase(check)不再正常工作,因爲「word」現在具有不同的值? – 2014-10-28 05:27:41

0

嘗試讀取使用Files類的文件。此代碼會發現第一次出現在每一行

Path path = Paths.get("path"); 
List<String> lines = Files.readAllLines(path); 
int found = -1; 
for (String line : lines) 
    found = line.indexOf("word"); 

不要忘了替換路徑,檢索詞。

如果你想找到在整個文件的第一次出現,使用StringBuilder以連接線:

StringBuilder sb = new StringBuilder(); 
for (String line : lines) 
    sb.append(line); 
found = sb.toString().indexOf("word"); 

或者與之前針對所有行找到每行首次出現它:

int loc = 0; 
for (String line : lines) { 
    if (line.indexOf("word") != -1); 
     found = loc + line.indexOf("word"); 
    loc += line.length(); 
} 

你也可以使用一個Pattern - Matcher組合爲這一點,它會更容易,如果你想找到全部這個詞的出現。

相關問題