2014-10-28 50 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组合为这一点,它会更容易,如果你想找到全部这个词的出现。

相关问题