0

I am trying to find element that is duplicated in an array. Program is intended to take user input.

Here is the code:

   package sortingattempt;

import java.util.Scanner;

public class ArraySimilar {

    public static void main(String args[]){
        int[] a = new int[100];
        int[] b = new int[5];
        int Duplicate = 0;

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter size of array");
        int size = sc.nextInt();

        //Scanner elem = new Scanner(System.in);
        System.out.println("Input elements in array" +size);
        for(int j = 0;j<size;j++){
            a[j] = sc.nextInt();
        }
        System.out.println(a.length);
        System.out.println("a[]" + a.toString());

    for (int i = 0; i < a.length;i++){
        b[0] = a[i];
        if (b[0] == a[i+1]){
            Duplicate = b[0];
            System.out.println(Duplicate);
        }
        i++;
        }
        System.out.println("No common variable");

    }

}

When I try to run it, its running properly till line where I ask user to give input. After entering input nothing is happening. Please indicate errors in program. Thank you.

2
  • Try inserting sc.nextLine(); before a[j] = sc.nextInt(); Commented Dec 7, 2013 at 1:18
  • No it did not work. I debugged program and found out that elements are not going in array. Commented Dec 7, 2013 at 1:22

2 Answers 2

1

I think what's happening is that you are not being prompted to enter a number with a System.out.println("Enter a number: "); So you think the program is not working, when really it is just waiting for you to keep entering numbers. You may want to prompt with System.out.println()s on each iteration:

System.out.println("Input elements in array" +size);
for(int j = 0;j<size;j++){
    System.out.println("Enter a number: ");
    a[j] = sc.nextInt();
}

Also, your initializng your arrays before hand. Shouldn't you wait to get the user input for size, then initialize them?

int[] a; 
int[] b;

Scanner sc = new Scanner(System.in);
System.out.println("Enter size of array");
int size = sc.nextInt();

a = new int[size];
b = new int[size];

Also, trying to print a.toString() will not give you your desired output. You have to iterate through it or do something like this

System.out.println("a[] " + Arrays.asList(a));

Edit: To Compare

You should just make the values equals in the first loop

for(int j = 0;j<size;j++){
    System.out.println("Enter a number: ");
    a[j] = sc.nextInt();
    b[j] = a[i];
}

Then in the bottom loop, compare in a nested loop

int dupCount = 0;
for (int i = 0; i < size; i++){
    for (int j = 0; j < size; j++){
        if (b[i] == a[j]){
            Duplicate = a[j];
            System.out.println(Duplicate);
            dupCount = 0;
        }
    }
}
if (dupCount == 0)
    System.out.println("No common variable");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. It is now taking elements of input as Array. but I am getting an error now: Exception in thread "main" a[][I@6bb1a986 java.lang.ArrayIndexOutOfBoundsException: 5 at sortingattempt.ArraySimilar.main(ArraySimilar.java:29)
Is this if (b[0] == a[i+1]) line 29?
Yes. I am comparing elements there.
Im assuming you entered 5 for the size? You can see the problem when you try access a[i + 1]. The max i in the loop is 4, so when you try and add 1 you're trying to access a[5] which doesnt exist in an array of size 5. max index would be 4. You need to fix that +1. I'm not exactly sure what you're trying to do, so I can't really offer a solution.
I am trying to put each element in a[] at b[0] position and compare it with other elements in a[].
0

Another error is that you need to delete the i++ from this block of code

   Duplicate = b[0];
        System.out.println(Duplicate);
    }
    i++;  //delete that
    }

Also change that

  System.out.println("Input elements in array" +size);
for(int j = 0;j<size;j++){
    a[j] = sc.nextInt();
}
System.out.println(a.length);
System.out.println("a[]" + a.toString());//change that to a[j] and put it inside the loop. 

I assume here that you want to print the elements of your array,so the a.toString won't do that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.