Java 在从文件中使用扫描仪时输入不匹配
晚上好。我试图从一个文件中读取双精度,并将它们放入双精度数组中。我采用的方法是使用扫描仪 这是我的密码Java 在从文件中使用扫描仪时输入不匹配,java,java.util.scanner,inputmismatchexception,Java,Java.util.scanner,Inputmismatchexception,晚上好。我试图从一个文件中读取双精度,并将它们放入双精度数组中。我采用的方法是使用扫描仪 这是我的密码 public static void decryption(int n, int d) throws IOException { String encFile = getFile(); String decFileName = getNewFile(); //create save file for encrytped
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
}
这是我第一次遇到这个特殊的错误,到目前为止,文档几乎都说解决它的方法不是一开始就做
我还尝试了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);
}
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);
}
一旦我这样做了,我就能够逐行读取每个双行,并将它们放入双数组中。最终有效的修复方法 我获取输入文件的内容,并使用正则表达式过滤掉方括号和逗号。然后,当遇到一个空间时,同样的方法也适用于新线 这是我用来做这件事的代码
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);
}
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”作为分配指南的名称。通常我会让它们更具描述性。有没有办法将给定的输入读入双数组?