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.

5
  • You’re trying to read an integer from the file. But there’s no integer in your file — there’s "[", followed by a floating point number, followed by ",", etc. Commented Oct 26, 2020 at 9:28
  • Obviously your file contents do not match the layout that your code wants. Commented Oct 26, 2020 at 9:30
  • And unrelated: use meaningful names, always. "n" and "d" are pretty nothingtelling names ... Commented Oct 26, 2020 at 9:39
  • I am using "n" and "d" as names for the purpose of assignment guidelines. Normally I would have them be more descriptive. Commented Oct 26, 2020 at 9:43
  • Is there a way to read the given input into a double array? Commented Oct 26, 2020 at 9:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.