6

I am practicing for Java fresher interview coding examples. I am trying to write a program to find duplicate numbers between 1 to N, where N is given by the user along with the numbers themselves. Here is the code:

import java.io.DataInputStream;
import java.io.IOException;

public class DuplicateNumbers {

    public static void main(String[] args) throws IOException {
        DataInputStream in = new DataInputStream(System.in);

        System.out.println(" Enter the number of numbers ");

        int a = in.readInt();
        int[] num = new int[a];
        System.out.println(" Enter the ints one by one ");
        for (int b = 0; b < a; b++) {
            System.out.println(" Enter no "+(b+1));
            num[b]=in.readInt();
        }
        int c = 0;
        for (int d = 0; d < a; d++) {
            int f = 0;
            c = num[d];
            for (int e=0; e<a; e++) {
                if (c==num[e]) {
                    f++;
                }
            }

            if(f > 1)
                System.out.println(" Duplicate number "+c);
        }
    }

}

But I am getting following error in Eclipse Neon:

Enter the number of numbers 
5

Exception in thread "main" java.lang.OutOfMemoryError: 
Java heap space at DuplicateNumbers.main(DuplicateNumbers.java:14)

What is wrong? Why the JVM heap space error? Code compiles and runs fine.

idmean
  • 14,540
  • 9
  • 54
  • 83
  • 3
    Well, it **does not** *"run fine"*, that is why you are here asking about the exception... Does it crash on the line `int[] num=new int[a];` have you tried adding breakpoints and debugging the code? – luk2302 Feb 07 '17 at 13:58
  • 4
    Where did you learn DataInputStream was how to read user input? – OneCricketeer Feb 07 '17 at 13:59
  • No Sir, i am not trying to copy anyone's code. I just a have a list of programs to practice, where I try to code on my own, and when I run out of logic and patience, I ask for help. –  Feb 07 '17 at 14:06
  • Just for the record: you can still accept one of the answers; in case you found them helpful enough ;-) – GhostCat Feb 08 '17 at 11:53

3 Answers3

19

DataInputStream is for binary not text. When you type 4 bytes, this is turned into a 32-bit int value e.g. 5, \n, \n, \n is about 900 million which is why it complains about memory when you create the array. You can check this by stepping through the code in your debugger.

What you need is text input, try using

Scanner in = new Scanner(System.in);
System.out.println("Enter the number of numbers");
int a = in.nextInt();
in.nextLine(); // discard the rest of the line.
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

Starting here:

DataInputStream in=new DataInputStream(System.in);

You should not use a DataInputStream ... and I see that you already got explanations about that.

But beyond that:

for(int e=0; e<a; e++)

You will immediately run into num[d] and num[e] being equal. Because your second loop actually compares num[0] with num[0] for example. So: the second loop needs to only run on indexes after the outer one!

Beyond that, it is also unclear if you really want 50 times "duplicate" in case you entered 50 times the same number. I would rather go and print the number of duplicates for a number.

In other words: after fixing the input stream problem, your code will still not do the correct thing.

And beyond that: your usage of single-character names makes it almost impossible to easily understand what this code is doing.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • yeah I corrected the "flag-upon-first-match" glitch, by doing if(f>1) –  Feb 07 '17 at 14:09
  • Sure, that works; but that is a wrong approach. Don't fix "broken data" by expecting it - prevent "broken data" from coming into existence in the first place! – GhostCat Feb 07 '17 at 14:10
  • meaning planning code before typing ? could you please elaborate, Sir? Do professional programmers do "Rough Work" before coding? How does development occur in real life? Please tell me so that I can improve. –  Feb 07 '17 at 14:14
  • What I mean is: you changed the if() clause to check for > 1. That is "fixing the broken result". My solution would be to prevent that **first** comparison of num[0] with itself. In other words: instead of checking f > 1; make sure that f is only increased when you find duplicate numbers within **different** slots of your array. – GhostCat Feb 07 '17 at 14:16
  • Ok got it :) I should use better flagging techniques. –  Feb 07 '17 at 14:21
1

Try using a Scanner for your class instead.

Here's a basic example:

public static void main(String[] args) throws IOException 
    {
        System.out.println("Enter a number: ");
        Scanner sc = new Scanner(System.in);
        String item = sc.next();
        System.out.println("Your item is: " + item);
        sc.close();
    }
nmg49
  • 1,356
  • 1
  • 11
  • 28