Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/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处理来自文件的输入_Java_Oop - Fatal编程技术网

Java处理来自文件的输入

Java处理来自文件的输入,java,oop,Java,Oop,所以我在做这个期末考试的样本,题目要求从文件中读取输入,然后将它们处理成单词句子的结尾由任何以三个字符之一结尾的单词来标记 我能够为此编写代码,但我只能使用scanner类和use.Delimiter将它们拆分为句子。我想把它们处理成单词,看看一个单词是否以上面的句子分隔符结尾,然后我将停止在句子类中添加单词。 任何帮助将不胜感激,因为我正在自学,这就是我想到的。我的密码在这里 File file = new File("finalq4.txt"); Scanner

所以我在做这个期末考试的样本,题目要求从文件中读取输入,然后将它们处理成单词句子的结尾由任何以三个字符之一结尾的单词来标记

我能够为此编写代码,但我只能使用scanner类和use.Delimiter将它们拆分为句子。我想把它们处理成单词,看看一个单词是否以上面的句子分隔符结尾,然后我将停止在句子类中添加单词。 任何帮助将不胜感激,因为我正在自学,这就是我想到的。我的密码在这里

File file = new File("finalq4.txt");
    Scanner scanner = new Scanner(file);
    scanner.useDelimiter("[.?!]");
    while(scanner.hasNext()){
        sentCount++;
        line = scanner.next();
        line = line.replaceAll("\\r?\\n", " ");
        line = line.trim();
        StringTokenizer tokenizer = new StringTokenizer(line, " ");
        wordsCount += tokenizer.countTokens();
        sentences.add(new Sentence(line,wordsCount));
        for(int i = 0; i < line.replaceAll(",|\\s+|'|-","").length(); i++){
            currentChar = line.charAt(i);
            if (Character.isDigit(currentChar)) {
            }else{
                lettersCount++;
            }
        }
    }
File File=new文件(“finalq4.txt”);
扫描仪=新扫描仪(文件);
scanner.useDelimiter(“[.?!]”);
while(scanner.hasNext()){
sentCount++;
line=scanner.next();
line=line.replaceAll(\\r?\\n“,”);
line=line.trim();
StringTokenizer tokenizer=新的StringTokenizer(行“”);
WordScont+=tokenizer.countTokens();
添加(新的句子(行,单词));
对于(int i=0;i
我在这段代码中所做的是使用分隔符方法将输入拆分成句子,然后计算整个文件中的单词、字母,并将句子存储在一个句子类中

如果我想将其拆分为单词,如何在不使用scanner类的情况下进行拆分。

我必须处理的文件中的一些输入如下:

下面的文字是基于维基百科的加密页面

密码学是隐藏信息的实践和研究。在现代,, 密码学被认为是数学和计算机的一个分支 科学,并与信息理论、计算机安全和 工程学。密码学在技术上应用于现有的应用中 先进社会;例子包括ATM卡的安全性、计算机 密码和电子商务都依赖于加密技术

如果这个问题需要解释,我可以进一步阐述


我想做的是不断地向句子类添加单词,如果单词以上述句子分隔符之一结尾,就停止添加。然后读另一个单词并不断添加这些单词,直到我碰到另一个分隔符。

下面的代码片段将起作用

public static void main(String[] args) throws FileNotFoundException {
    File file = new File("final.txt");
    Scanner scanner = new Scanner(file);
    scanner.useDelimiter("[.?!]");
    int sentCount;
    List<Sentence> sentences = new ArrayList<Sentence>();
    while (scanner.hasNext()) {
        String line = scanner.next();
        if (!line.equals("")) { /// for the ... in the end
            int wordsCount = 0;
            String[] wordsOfLine = line.split(" ");
            for (int i = 0; i < wordsOfLine.length; i++) {
                wordsCount++;
            }
            Sentence sentence = new Sentence(line, wordsCount);
            sentences.add(sentence);
        }
    }
}



public class Sentence {
    String line = "";
    int wordsCount = 0;
    public Sentence(String line, int wordsCount) {
        this.line = line;
        this.wordsCount=wordsCount;
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
文件=新文件(“final.txt”);
扫描仪=新扫描仪(文件);
scanner.useDelimiter(“[.?!]”);
int sentCount;
列出句子=新建ArrayList();
while(scanner.hasNext()){
字符串行=scanner.next();
如果(!line.equals(“”){///用于结尾的
int-wordscont=0;
字符串[]wordsOfLine=line.split(“”);
for(int i=0;i
您可以使用缓冲读取器读取文件的每一行。然后使用拆分方法将每一行拆分为一个句子,最后使用相同的方法将句子拆分,以获得单词。最后的结果如下:

BufferedReader br;
try{
    br = new BufferedReader(new File(fileName));
}catch (IOException e) {e.printStackTrace();}
StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine()) != null){
    sb.append(line);
}
String[] sentences = sb.toString().split("\\.");
for(String sentence:sentences){
    String word = sentence.split(" ");
    //Add word to sentence...
}
try{
    br.close();
}catch(IOException e){
    e.printStackTrace();
}

好的,我已经通过几种技术解决了这个问题,其中一种方法是上面提到的。然而,我也能够通过另一种方法解决这个问题,这种方法不需要使用
Scanner
类。这种方法更准确,它给了我精确的输出,而在上面的方法中,我只需要几个单词和字母。

try {
        input = new BufferedReader(new FileReader("file.txt"));
        strLine = input.readLine();
        while(strLine!= null){

            String[] tokens = strLine.split("\\s+");
            for (int i = 0; i < tokens.length; i++) {
                if(strLine.isEmpty()){
                    continue;
                }
                String s = tokens[i];
                wordsJoin += tokens[i] + " ";

                wordCount += i;
                int len = s.length();
                String charString = s.replaceAll("[^a-zA-Z ]", "");
                for(int k =0; k<charString.length(); k++){
                    currentChar = charString.charAt(k);
                    if(Character.isLetter(currentChar)){ 
                        lettersCount++;
                    }  
                }
                if (s.charAt(len - 1) == '.' || s.charAt(len - 1) == '?' || s.charAt(len - 1) == '!') {
                    sentences.add(new Sentence(wordsJoin, wordCount));
                    sentCount++;
                    numOfWords += countWords(wordsJoin);
                    wordsJoin = "";
                    wordCount = 0;
                } 
            }
            strLine = input.readLine();
        }
试试看{
输入=新的BufferedReader(新的文件读取器(“file.txt”);
strLine=input.readLine();
while(strLine!=null){
字符串[]标记=strLine.split(\\s+);
for(int i=0;i对于(int k=0;kScanner非常好。)您也可以逐行读取文件。使用
String.split如何?
?是的,扫描器很好,也很容易,不需要太多编码。@ΦXocę웃Пepeúpaツ是的,我知道拆分方法,但如何拆分它们?我不知道用于拆分的正则表达式。@scarywombat你的意思是我可以使用scanner类拆分单词,然后将它们添加到句子类?我只能拆分以,或!或?结尾的行。我可以使用scanner类吗?@ΦXocę웃Пepeúpaツ如果我正在跟随你的程序,那么这一行会得到一个句子,因此它基本上与我的程序相同。我想将它拆分为单词,然后将每个单词添加到句子类中,当它以?结尾时停止!我认为你的程序不会这样做。