Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 数字I';I’’我想打印出来,但下一行的数字却不断增加_Java_File_Input_Output - Fatal编程技术网

Java 数字I';I’’我想打印出来,但下一行的数字却不断增加

Java 数字I';I’’我想打印出来,但下一行的数字却不断增加,java,file,input,output,Java,File,Input,Output,我想从一个文本文档文件中读取几行,然后在另一个文本文档中写入每行的元音数 “vlez.txt”是输入文件,“destinacija.txt”是输出文件 输入->输出示例: 你好 世界 再见 在输出文件中,结果将是: 2(来自Hello的2个元音) 3(1个来自World+previous的元音) 7(你明白了) 我的代码: package prvaZad; import java.io.BufferedReader; import java.io.FileReader; import java.

我想从一个文本文档文件中读取几行,然后在另一个文本文档中写入每行的元音数

“vlez.txt”是输入文件,“destinacija.txt”是输出文件

输入->输出示例:

你好
世界
再见

在输出文件中,结果将是:

2(来自Hello的2个元音)
3(1个来自World+previous的元音)
7(你明白了)

我的代码:

package prvaZad;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class prvaZadaca {

    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader(new FileReader("vlez.txt"));
        PrintWriter out = new PrintWriter(new FileWriter("destinacija.txt"));
        StringBuilder sb = new StringBuilder();

        String line;
        int number = 0;
        while ((line = in.readLine()) != null) {

            sb.append(line);
            sb.append('\n');

            String lc = sb.toString().toLowerCase();
            for (int i=0; i<lc.length(); i++) {
                char ch = lc.charAt(i);

                if ("aeiouy".indexOf(ch) > -1) {
                    number++;
                }
            }
            out.print(number);
            out.print('\n');
            number = 0;
        }       

        if (in != null) 
            in.close();

        if (out != null)
            out.close();

        System.out.println(number);
    }
}
package-prvaZad;
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.PrintWriter;
普瓦扎达卡公务舱{
公共静态void main(字符串[]args)引发IOException{
BufferedReader in=新的BufferedReader(新文件读取器(“vlez.txt”);
PrintWriter out=新的PrintWriter(新的文件编写器(“destinacija.txt”);
StringBuilder sb=新的StringBuilder();
弦线;
整数=0;
而((line=in.readLine())!=null){
某人附加(行);
sb.追加('\n');
字符串lc=sb.toString().toLowerCase();
对于(int i=0;i-1){
数字++;
}
}
打印(编号);
out.print('\n');
数字=0;
}       
if(in!=null)
in.close();
if(out!=null)
out.close();
系统输出打印项次(编号);
}
}

问题是您一直在向
StringBuilder
添加行,换句话说,它将从0开始重新计数

您可以通过在下一次迭代之前“清除”StringBuilder来解决此问题:

out.print(number);
out.print("\n")
number = 0;
sb = new StringBuilder(); // a new empty StringBuilder

此代码可以帮助您

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication11;

import java.util.Timer;

/**
 *
 * @author Sem-6-INGENIERIAINDU
 */
public class JavaApplication11 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String word = "Hello World Goodbye";
        String[] container = word.split(" ");
        for (String container1 : container) {
            System.err.println("The word:" + container1 + " has " + (container1.replaceAll("[^aeiou]", "")).length());
        }
    }

}
跑步:单词:你好有2个单词:世界有1个单词:再见有3个 生成成功(总时间:0秒)


请用文本编码,不要用图片:)你能不能展示一个输入->输出的例子?太好了。你能告诉我们你目前得到的输出吗?