从java语句中获取每个单词的第一个字母,并从java文件中打印给用户

从java语句中获取每个单词的第一个字母,并从java文件中打印给用户,java,word,sentence,Java,Word,Sentence,你好,我是java的初学者。我需要从文本文件中打印句子中每个单词的第一个字母,并将其打印到文本文件中。但是,我一直收到这个错误消息 输入 输入包含许多测试用例: 第一行包含一个整数N,表示测试用例的数量。每个测试用例由一行组成,包含1到50个字符,可以是小写字母('a'-'z')或空格('')。注意前导空格和尾随空格 输出 对于每个测试用例,打印隐藏在输入中的文本。 这是我的错误 "Exception in thread "main" java.util.InputMismatchExce

你好,我是java的初学者。我需要从文本文件中打印句子中每个单词的第一个字母,并将其打印到文本文件中。但是,我一直收到这个错误消息 输入

输入包含许多测试用例: 第一行包含一个整数N,表示测试用例的数量。每个测试用例由一行组成,包含1到50个字符,可以是小写字母('a'-'z')或空格('')。注意前导空格和尾随空格

输出

对于每个测试用例,打印隐藏在输入中的文本。

这是我的错误

   "Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at main.main(main.java:23)"
这是我的密码

public class main {     

        public static void main (String[] args) throws Exception {
            //reading from to the stream of lines 
            System.setIn(new BufferedInputStream(new FileInputStream ("txt")));
            System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("out"))));

            //Triming the space at the start of the sentence
            Scanner file = new Scanner(System.in);

            // read the lines of text 
            while ( file.hasNextLine() ){
            // Read the number of cases 
            //  if ( file.hasNextInt() ){
                    // number of cases 
                    int t = file.nextInt();
                    //looping through to number 4 
                    while (t-- > 0){
                        //for (int i=0; i>t; i++){

                        String input =file.next();

                        String b= input.trim().replaceAll(" +", " ");

                        String[] sentence =b.split(" ");

                        for (int i1=0; i1<sentence.length;i1++ ){

                            String s = sentence[i1]; 
                            System.out.print(s.charAt(0));

                        } 

                    }   
            }
            System.out.close();
        }

    }
公共类主{
公共静态void main(字符串[]args)引发异常{
//从头到尾阅读
setIn(新的BufferedInputStream(新的FileInputStream(“txt”));
系统设置(新打印流(新缓冲输出流(新文件输出流(“输出”))));
//修剪句子开头的空格
扫描仪文件=新扫描仪(System.in);
//读几行文字
while(file.hasNextLine()){
//阅读案例数量
//if(文件.hasNextInt()){
//个案数目
int t=file.nextInt();
//循环到4号
而(t-->0){
//对于(int i=0;i>t;i++){
字符串输入=file.next();
字符串b=input.trim().replaceAll(“+”,”);
字符串[]语句=b.split(“”);

对于(int i1=0;i1don)不要使用nextInt来读取输入,也就是说当您只需要数字时。我建议使用nextLine()然后解析返回的行我还建议不要使用System更改输入和输出。读入和写入文件时不需要这样做。不要使用nextLine读取输入,这是在只需要数字的情况下。我建议使用nextLine()然后解析您得到的行,我还建议不要使用系统更改输入和输出。您不需要这样做来读取和写入文件。