Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何创建一个变量来保存文本文件中的所有数据,而不使用has.next方法?_Java - Fatal编程技术网

Java 如何创建一个变量来保存文本文件中的所有数据,而不使用has.next方法?

Java 如何创建一个变量来保存文本文件中的所有数据,而不使用has.next方法?,java,Java,我试图找到一个从单词出现的位置0开始计数的特殊点。以下是我到目前为止的情况: 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" ); /

我试图找到一个从单词出现的位置0开始计数的特殊点。以下是我到目前为止的情况:

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
”在循环中只有一个值。因此,我不可能在循环之外使用它。有人能帮我吗?也许给我一些我还没试过的新东西

String word = reader.next() + " ";
您已经在while块中声明了“
word
”。因此,系统只有在这个变量在循环内时才知道它,一旦它在循环外,它就会忘记它。如果要在循环外部使用变量,则应在外部声明它。我知道你试过一次。现在,为什么word在循环之外有相同的值,即使在这之后?“
word
”从文件中获取读卡器通过
reader.next()
给出的值。因此,一旦您在循环之外,它仍然会有
reader.next()
提供的最后一个值。如果您想查看word的值,只需在为其赋值后立即给出一个print语句

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

          |
          |
          |

     }
注意:如果希望
word
变量保存文件的整个单词,请执行此操作


word=word+reader.next()

尝试使用
文件
类读取文件。此代码将在每行中找到第一个匹配项:

或查找每行中相对于其前面所有行的第一个匹配项:

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

您也可以使用
模式
-
匹配器
组合来进行此操作,如果您想找到该单词的所有匹配项,则会更容易。

在循环之前声明
单词
?是ofc,但作为什么?如果我只是声明为nothing,它将保持为nothing,并且在while循环期间仍然只有一个值,就像您在循环外部声明
count
一样,将
word
放在它后面的行上。顺便说一句,这称为“scope”,查找这个单词。所以我尝试了这个方法,但我不得不在while循环中去掉单词前面的字符串,以便能够在计数后声明它。即便如此,在运行一个测试来检查word现在是否与循环内的值相同后,我得到了空白。你能解释一下你想要实现什么吗?如果我在循环外声明word,它会有什么值?在循环内部有文本文件的值,但在循环外部没有任何值。那么,如何在循环外部获取word,同时保留与循环内部相同的值呢?当您说declare时,它的意思是告诉这个--“String word;”,但是如果您在声明期间像这样给它赋值--“String word=”“;”,那么它会告诉编译器word是一个字符串对象,它指向空值。在循环的每次迭代中,您都可以看到分配给word变量的不同值。一旦退出循环,最后分配的值将保留。如果需要整个文件,请执行以下操作-word=word+reader.next();我理解你的意思,但是如果我不给它赋值,我会得到一个错误,如果我声明它并使用while循环来获取整个文件,那么在while循环中的if语句中,我不会得到一个空白,因为它会在我赋值“String word”时引用。我已经试过了,但似乎无法找出原因,它只是跳过while循环中的if语句,并推断出word.equalsIgnoreCase(检查)不再正确工作,因为“word”现在有一个不同的值?您应该声明它并分配一个值,比如null(
)。在您发出
if
语句之前,有另一个赋值给
word
,对吗?因此,它不能为空,除非您的文件具有空值。请使用打印语句进行调试,可能是调试器,如果有更多问题,请返回。
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();
}