【问题标题】:InputMismatch when using Scanner in from file从文件中使用扫描仪时输入不匹配
【发布时间】:2020-10-26 09:23:26
【问题描述】:

晚上好。我正在尝试从文件中读取双精度并将它们放入双精度数组中。我解决这个问题的方法是使用扫描仪。

这是我的代码

public static void decryption(int n, int d) throws IOException
    {
        String encFile = getFile();
        String decFileName = getNewFile();
        
        //create save file for encrytped message
        File encFileSave = new File(decFileName);
        if(encFileSave.createNewFile())
        {
            System.out.println("Decrypted file created");
        }
        else
        {
            System.out.println("File already exists");
        }
                
        FileInputStream inS = new FileInputStream(encFile);
        //FileOutputStream outS = new FileOutputStream(decFileName);
        FileWriter outW = new FileWriter(decFileName);
        
        Scanner in = new Scanner(new File(encFile));
        int en = in.nextInt();
        double[] pmMess = new double[en];
        for(int i = 0; i < pmMess.length; i++)
        {
            pmMess[i] = in.nextDouble();
        }
        String dMess = decrypt(pmMess);
        
        outW.write(dMess);
    
        inS.close();
        outW.close();
        //end deccrypt
    }

我在该行收到错误“InputMismatchException”

int en = in.nextInt();

这是我第一次遇到这个特定的错误,到目前为止,文档几乎都说解决它的方法不是一开始就这样做。

我也用 FileInputStream 尝试过,但没有成功。

输入如下

[373248.0, 1030301.0, 1259712.0, 1259712.0, 1367631.0, 32768.0, 658503.0, 1367631.0, 1481544.0, 1259712.0, 1000000.0, 97336.0, 2197.0, 1000.0, 474552.0, 1157625.0, 970299.0, 1030301.0, 32768.0, 314432.0, 912673.0, 1771561.0, 35937.0, 2197.0, 1000.0, 274625.0, 1331000.0, 32768.0, 274625.0, 32768.0, 1061208.0, 1367631.0, 1481544.0, 32768.0, 1560896.0, 1124864.0, 1030301.0, 32768.0, 970299.0, 1259712.0, 912673.0, 1520875.0, 1520875.0, 32768.0, 1685159.0, 1367631.0, 1601613.0, 1259712.0, 1000000.0, 32768.0, 941192.0, 1030301.0, 32768.0, 1331000.0, 1157625.0, 970299.0, 1030301.0, 97336.0, 32768.0, 32768.0, 32768.0, 195112.0, 68921.0, 91125.0]

最终成功的修复

我获取了输入文件内容并使用正则表达式从本质上过滤掉了方括号和逗号。然后在遇到空格时做同样的事情去换行。

这是我用来做的代码。

String bracOut;
while(bIn.ready())
{
    bracOut = bIn.readLine();
    String noBrac = bracOut.replaceAll("[,\\[\\]]", "");
    noBrac = noBrac.replaceAll(" ", "\n");
    byte[] noBracByte = noBrac.getBytes();
            
    outS.write(noBracByte);
    System.out.print(noBrac);
}

一旦我这样做了,我就可以逐行读取每个双精度并将它们放入双精度数组中。

【问题讨论】:

  • 你正试图从文件中读取一个整数。但是你的文件中没有整数——有"[",后跟一个浮点数,然后是",",等等。
  • 显然您的文件内容与您的代码想要的布局不匹配。
  • 不相关:总是使用有意义的名称。 "n" 和 "d" 是很不起眼的名字……
  • 我使用“n”和“d”作为分配指南的名称。通常我会让它们更具描述性。
  • 有没有办法将给定的输入读入双精度数组?

标签: java java.util.scanner inputmismatchexception


【解决方案1】:

最终成功的修复

我获取了输入文件内容并使用正则表达式从本质上过滤掉了方括号和逗号。然后在遇到空格时做同样的事情去换行。

这是我用来做的代码。

String bracOut;
while(bIn.ready())
{
    bracOut = bIn.readLine();
    String noBrac = bracOut.replaceAll("[,\\[\\]]", "");
    noBrac = noBrac.replaceAll(" ", "\n");
    byte[] noBracByte = noBrac.getBytes();
            
    outS.write(noBracByte);
    System.out.print(noBrac);
}

一旦我这样做了,我就可以逐行读取每个双精度并将它们放入双精度数组中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多