1

I am trying to read 2 input files containing integers(even duplicates are considered) and trying to find common integers and write them to the output file.

input1.txt

01
21
14
27
31
20
31

input2.txt

14
21
27
08
09
14

Following is the code I tried:

public static void main(String[] args) throws NumberFormatException {
        try {
            BufferedReader inputFile1 = new BufferedReader(new FileReader(new File("src/input1.txt")));
            BufferedReader inputFile2 = new BufferedReader(new FileReader(new File("src/input2.txt")));
            FileWriter fileCommon = new FileWriter("src/common.txt");
            String lineInput1;
            String lineInput2;
            int inputArray1[] = new int[10];
            int inputArray2[] = new int[10];
            int index = 0;
            while ((lineInput1 = inputFile1.readLine()) != null) {
                inputArray1[index] = Integer.parseInt(lineInput1);
                index++;
            }
            index = 0;
            while((lineInput2 = inputFile2.readLine()) != null) {
                inputArray2[index] = Integer.parseInt(lineInput2);
                index++;
            }

            for (int a = 0; a < inputArray1.length; a++) {
                for (int b = 0;b < inputArray2.length; b++) {
                    if(inputArray1[a] == inputArray2[b]) {
                        fileCommon.write(inputArray1[a]);
                    }
                }
            }
            inputFile1.close();
            inputFile2.close();
            fileCommon.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

I don't understand where I am making mistake. I am not getting any errors and the output file that is generated is empty.

output expected are common integers in both files

14
21
27
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
  • 2
    Have you tried going through it with a debugger? If not, do so. –  Jul 21 '20 at 06:03
  • try creating a new list with common elements and then write the result to file – deadshot Jul 21 '20 at 06:07
  • Please learn to debug (even with no debugger), print both array after reading both first, then print both value before the `if` – azro Jul 21 '20 at 06:08
  • @azro, Yeah array is being printed – Akshay Shinde Jul 21 '20 at 06:16
  • Share both print , also do System.out.println(arr1[a] +"-" + arr2[b] + "-" + arr1[a]==arr2[b]) – azro Jul 21 '20 at 06:18
  • If you're problem is solved you should put a check mark next to the answer that suits you best. It seems most of them have come to the same conclusion, but you haven't said if that solves your issue. – matt Jul 22 '20 at 06:43

5 Answers5

2

Remember, that FileWriter's write(int c) accepts an integer representing a character code from either a specified charset or the platform's default charset, which is mostly extensions of ASCII (for example, in Windows, default charset is Windows-1252 which is an extension of ASCII).

which means, that you actually don't have any (semantical or syntactical) problem per se, and you're writing into file successfully, but! you're writing some special characters which you can't see afterwards.

If you'll invoke write(..) with some integer representing Latin character (or symbol) in the ASCII table, you'll see that it'll write actual English letter (or symbol) into your file.

For instance:

fileCommon.write(37); //will write `%` into your file.
fileCommon.write(66); //will write `B` into your file.

In your code, you're only writing 21, 14 and 27 into your file, and as you can see from the ASCII table:

  • Decimal 21 represents Negative Acknowledgment
  • Decimal 14 represents Shift-out
  • Decimal 27 represents Escape
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
1

FileWriter.write(int) will write a single character, in your case 14, 21, and 27 are all control characters that would not be visible in a text file.

common.write("" + arr1[a]);

Should write the string representation. You'll find some other problems though, such as missing line endings and repeated values, but this should get you started.

matt
  • 10,892
  • 3
  • 22
  • 34
  • `fileCommon.write(inputArray1[a] + "\r\n");` plus that the bytes/chars 14, 21 are (invisible) control characters. Also remarks like `int[] inputArray1 = ...;` being more conventional than `int inputArray1[] = ...;` – Joop Eggen Jul 21 '20 at 07:29
1

Here's the thing.

  1. The write(int c) method of FileWriter is not actually write an int value, but write an ASCII code of a single character.For example, write(53) will write a "5" to a file.

    In your code, you are acctually writting some symbols.You can use write(String str) method of FileWriter or just use BufferedWriter class to achieve you goal.

  2. The result of the write value is acctually "21141427" by your code, so you have to remove the repeat value when write it and line feed after write each value.

Sorry for the poor English.

yuexing
  • 36
  • 3
  • The write method is literally writing an int, or more precisely a character. It is not limited to the ascii range. The rest of what you've written seems pretty sound. – matt Jul 21 '20 at 06:46
-1

You can read Strings from the original input files, instead of ints, and use the String.equals(Object):boolean function to compare Strings.

Then you won't need to parse from String to int, and convert an int to string back when writing to the file.

Also note that writing an int will write the unicode char value to the file, not the number as a string.

nurnachman
  • 4,468
  • 2
  • 37
  • 40
-2

The problem is the common.write line. It should be as follows.

                   common.write(String.valueOf(arr1[a])+"\n");

Additionally, This would perform much better if you put all of the data from the first file into a Map vs an array then when reading the second file just check the map for the key and if it exists write to common.

If you are dead set on using an array you can sort the first array and use a binary search. This would also perform much better than looping through everything over and over.

Navid Mitchell
  • 1,276
  • 11
  • 10
  • 3
    "Try calling common.flush() after each common.write() statement." - Why? Streams are automatically flashed on close(). –  Jul 21 '20 at 06:06
  • I do not think this is the case with the FileWriter. Give it a shot if it doesn't work let me know. I don't see anything else that looks like bug in the logic. – Navid Mitchell Jul 21 '20 at 06:13
  • have you checked that there is no problem reading from the file? –  Jul 21 '20 at 06:13
  • @Navid Mitchell, I tried but even flushing doesn't work. – Akshay Shinde Jul 21 '20 at 06:14
  • It is a requirement of all streams in Java - it is definitely the case with `FileWriter`. https://docs.oracle.com/javase/7/docs/api/java/io/OutputStreamWriter.html#close() – Boris the Spider Jul 21 '20 at 06:21
  • As a note - if you haven’t tested the code and found the issue _don’t post an answer_. Feel free to post a comment on the question saying “perhaps this might work”. But guessing at the problem is of help to no one. – Boris the Spider Jul 21 '20 at 06:23
  • 1
    It looks like the bug is actually the write line. Change to common.write(String.valueOf(arr1[a])+"\n"); – Navid Mitchell Jul 21 '20 at 06:25
  • @BoristheSpider That's a good point. I will do that in the future. – Navid Mitchell Jul 21 '20 at 06:28