1

I've got a program that reads an input file with 4000 1's or 0's, one on each line.

The program compiles fine but when it runs I get the following error:

ˇ˛0Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at Project1.main(Project1.java:27)

For some reason when I look at the text file, the first line is a 0 and nothing else yet when the program reads the first line it gets ˇ˛0. I also tried using a buffered reader with no luck. Could anyone please offer some input. Thank you!

Here's my code:

import java.io.*;
import java.util.Random;
import java.util.Scanner;

public class Project1 {

    public static void main(String[] args) {
        int [] stream = new int[4000];
        int [] received = new int[4000];
        int corrupt = 0;
        float standardDev = 0;
        float [] averages = new float[4000];
        float averagev = 0;
        float voltage = 0;
        try {
            Scanner st = new Scanner(new File("CS 380 bit feed.txt"));
            System.out.print(st.next());
            FileWriter outFile = new FileWriter("output");
            PrintWriter out = new PrintWriter(outFile);
            int i=0;
            Random rand = new Random();

            stream[i]=st.nextInt();
            if (stream[i] == 0)
                voltage = (float) (2.49 * rand.nextFloat());
            else
                voltage = (float) ((2.5 * rand.nextFloat()) + 2.5);
            if (voltage < 2.5)
                received[i] = 0;
            else
                received[i] = 1;
            averagev = voltage;
            averages[i] = voltage;
            if (stream[i] != received[i])
                corrupt++;
            i++;

            while (i < 4000) {
                stream[i]=st.nextInt();
                if (stream[i] == 0)
                    voltage = (float) 2.49 * rand.nextFloat();
                else
                    voltage = (float) ((2.5 * rand.nextFloat()) + 2.5);
                averagev = ((averagev * i) + voltage)/(i+1);
                if (voltage <= averagev)
                    received[i] = 0;
                else
                    received[i] = 1;
                if (stream[i] != received[i])
                    corrupt++;
                i++;
            }
            averagev = 0;
            int j = 0;
            while (j < 4000) {
                for (int k = 0; k<8; k++) {
                    out.print(received[j]);
                    averagev = averages[i] + averagev;
                    j++;
                }
                out.println("    " + averages[j]);
            }

        }
        catch (FileNotFoundException e) {
              e.printStackTrace();
        } 
        catch (IOException e) {
              e.printStackTrace();
        }
        averagev = averagev / 4000;
        for (int k = 0; k<4000; k++) 
            standardDev = ((averagev - averages[k])*(averagev - averages[k])) + standardDev;
        standardDev = standardDev/4000;

        System.out.println("Errors: " + corrupt);
        System.out.println("Percentage Corrupt: " + corrupt / 4000);
        System.out.println("Average of Average Voltages: " + averagev);
        System.out.println("Standard Deviation: " + standardDev);        
    }
}
1
  • 2
    Perhaps your file is corrupt or contains BOM en.wikipedia.org/wiki/Byte_order_mark characters. I suggest creating a new file with just 0. BTW Your voltages cannot be between 2.49 and 2.50. e.g. 2.491 Is that intended? Commented Oct 9, 2012 at 9:23

1 Answer 1

2

Look at the file in a hex editor. It might be corrupt or have a Byte Order Mark at the beginning, which wouldn't necessarily show up in a text editor.

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.