0

Good evening. I am trying to read doubles from a file and put them into a double array. The way I have approached this is to use a Scanner.

This is my code

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
    }

I am getting the error "InputMismatchException" at the line

int en = in.nextInt();

This is the first time I've run into this particular error and documentation has so far pretty much said the way to fix it is not do it in the first place.

I also tried it with FileInputStream with no success.

Input is as follows

[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]

Fix that ended up working

I took the input file content and used regex to essentially filter out the square brackets and commas. Then did the same to go to new line when encountering a space.

This is the code I used to do it.

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);
}

Once I did this I was able to read each double line by line and put them into a double array.

1 Answers1

0

Fix that ended up working

I took the input file content and used regex to essentially filter out the square brackets and commas. Then did the same to go to new line when encountering a space.

This is the code I used to do it.

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);
}

Once I did this I was able to read each double line by line and put them into a double array.