【问题标题】:Reading int value from text file, and using value to alter file contents to separate text file.(java)从文本文件中读取 int 值,并使用值将文件内容更改为单独的文本文件。(java)
【发布时间】:2019-02-27 01:09:31
【问题描述】:

所以我试图从文本文件中读取输入,将其存储到变量中,然后使用文件中的变量将该文本的更改版本输出到不同的文件中。我正在使用 Filereader、Scanner 和 Printwriter 来执行此操作。 我必须存储此文本文档的最后一行(这是一个数字)并使用该数字将文本正文乘以不同的文件而不包括数字。

所以文本是: Original file text

输出应该是: desired output

我能够检索数字并将其存储到我的乘数变量中,并将文本检索到我的字符串中,但是如果我在控制台内部检查,它会存储为一行: how the text is stored seen through console

所以它在新文件上输出如下: undesired output

我是 Java 新手,如果有任何我无法回答的问题可以帮助解决我的代码中的任何问题,请原谅我。

我尝试将+"\n" 添加到文件输出行,但没有骰子。我还尝试将它添加到 words += keys.nextLine() +"\n",不幸的是,它分隔了 CONSOLE 中的行而不是文件本身。我至少在正确的轨道上吗?

这是我的代码:

public class fileRead {
public static void main(String[] args) throws IOException{
    String words = "" ; //Stores the text from file
    int multiplier = 1; // Stores the number

    FileReader fr = new FileReader("hw3q3.txt");
    Scanner keys = new Scanner(fr);

    //while loop returns true if there is another line of text will repeat and store lines of text until the last line which is an int
    while(keys.hasNextLine())
        if (keys.hasNextInt()) { //validation that will read lines until it see's an integer and stores that number
            multiplier = keys.nextInt(); //Stores the number from text
        } else {
            words += keys.nextLine();//Stores the lines of text
            keys.nextLine();
        }
    keys.close();
    PrintWriter outputFile =  new PrintWriter("hw3q3x3.txt");
    for(int i = 1; i <= multiplier; i++) {
        outputFile.println(words);

    }
    outputFile.close();
    System.out.println("Data Written");
    System.out.println(multiplier);//just to see if it read the number
    System.out.println(words); //too see what was stored in 'words'
}

}

【问题讨论】:

    标签: java file loops text


    【解决方案1】:

    请参阅下面的 if 语句:

    words += keys.nextLine(); //Stores the lines of text
    if(words.endsWith(words.substring(words.lastIndexOf(" ")+1))) { //detect last word in sentence 
            words += '\n'; //after last word, append newline
    }
    
    ...
    
    for(int i = 1; i <= multiplier; i++) {
            outputFile.print(words); //change this to print instead of println
    }
    

    基本上,在文件中句子的最后一个单词之后,我们希望附加一个换行符,以便从换行开始编写下一个句子。

    上述 if 语句通过确定words 字符串中的最后一个单词,然后将换行符附加到words 字符串来检测句子的结尾。这将产生您期望的结果。

    为你分解表达式:

    words.substring(words.lastIndexOf(" ")+1)
    

    返回位于字符串中最后一个空格的索引处的字符串部分 (substring) 加 1 (lastIndexOf(" ") + 1) - 即我们在最后一个空格之后得到单词,所以最后一个词。

    整个while循环:

    while(keys.hasNextLine()) {
        if (keys.hasNextInt()) { //validation that will read lines until it see's an integer and stores that number
            multiplier = keys.nextInt(); //Stores the number from text
        } else {
            words += keys.nextLine();//Stores the lines of text
            if(words.endsWith(words.substring(words.lastIndexOf(" ")+1))) {
                words += '\n';
            }
        }
    }
    

    【讨论】:

    • 抱歉,我无法理解。那么将 if 语句嵌套在 else 语句中会查看行/字符串的最后一个单词吗?或文件的文本?另外,我没有意识到“keys.nextLine();”声明在那里。那是偶然放在那里的,希望不会影响您的答案。编辑:好的没关系,我明白你在解释什么,谢谢!
    • 它将查看单词 String 的最后一个单词,它基本上是文件中的一行。例如。对于“一只棕色的青蛙跳过懒惰的狗”,它将返回最后一个单词“狗”,下一句“尾巴长而棕色”将返回“棕色”。不,它不会影响我的回答,你明白了 :) 我已经编辑了答案和你的问题以删除那个错字。
    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多