0

Q. what is the right condition for DISPLAYING ONLY equal elements in my array.

heres the code.

    int num[]=new int[5];
    int numm[]=new int[5];

    for(int x=0;x<num.length;x++){
        num[x]=Integer.parseInt(JOptionPane.showInputDialog("Enter Values for num["+(x+1)+"]"));
    }

    for(int x1=0;x1<num.length;x1++){
        num[x1]=Integer.parseInt(JOptionPane.showInputDialog("Enter Values for num1["+(x1+1)+"]"));
    }

    String tosho="";
        for(int a=0;a<num.length;a++){
            for(int a1=0;a1<numm.length;a1++){
            if(num[0]==numm[0]){
              num[0]=numm[0];}


            else if(num[1]==numm[1]){
                num[1]=numm[1];}

            else if(num[2]==numm[2]){
                num[2]=numm[2];}

            else if(num[3]==numm[3]){
                num[3]=numm[3];}

            else if(num[4]==numm[4]){
                num[4]=numm[4];}       
        }

    }tosho+=(num[0])+""+(num[1])+""+(num[2])+""+(num[3])+""+(num[4]);

    JOptionPane.showMessageDialog(null, "both "+tosho+" found in array.");

when I input 1,2,3,4,5 in first aary

then 1,22,3,4,5 in second array

still it display the 2 or 22, even though they are not equal,,,

1
  • I would use ArrayList.contains() to find the intersection, and display that ... Commented Nov 20, 2013 at 13:55

5 Answers 5

3

Why so long code

    for(int a=0;a<num.length;a++){
        for(int a1=0;a1<numm.length;a1++){
        if(num[0]==numm[0]){
          num[0]=numm[0];}


        else if(num[1]==numm[1]){
            num[1]=numm[1];}

        else if(num[2]==numm[2]){
            num[2]=numm[2];}

        else if(num[3]==numm[3]){
            num[3]=numm[3];}

        else if(num[4]==numm[4]){
            num[4]=numm[4];}       
    }
   }

do like this

    List <Integer> matchedElements = new ArrayList<Integer>();
    for(int a=0;a<num.length;a++){
        for(int a1=0;a1<numm.length;a1++){
            if(num[a]==numm[a1]){
                matchedElements.add(num[a]);
           }
        }
    }
    Integer[] mached = matchedElements.toArray(new Integer[matchedElements.size()]);
    System.out.println(Arrays.toString(mached));
Sign up to request clarification or add additional context in comments.

1 Comment

Yes it can be shortened but is the the logic not still wrong? for the problem described.
2
num[x1]=Integer.parseInt(JOptionPane.showInputDialog("Enter Values for num1["+(x1+1)+"]"));

maybe numm[x1]?

You can use this code to fill tosho:

String tosho="";
for(int a=0;a<num.length;a++) {
    if(num[a]==numm[a]) {
        tosho+=num[a]+",";
    }
}

1 Comment

big mistake for numm[], hahaha and thank you for this code.
2

you start your if squence with

if(num[0]==numm[0]){
          num[0]=numm[0];}

and then you are using ELSE which means that it will be evaluated ONLY if the first if is not executing. It means that after you change the first element of the array, no other will be changed as there will be no ELSE since num[0] will equal numm[0]. Remove all "else" words and it should work.

2 Comments

but still it does not display the ONLY equal elements.
that's because you don't even need those two loops if you check every element of the array manually (all those ifs)
2

These two nested loops

for(int a=0;a<num.length;a++){
    for(int a1=0;a1<numm.length;a1++){
    ...

are useless.

You're not using in any way the variables a and a1.

Instead, you could simply iterate form 0 to array length and check the elements with same index for equality.

Comments

2

The assignment is wrong. you never assign values to numm variable.

The nested loops are no effect in the code. there no usage of a and a1 loop variable.

I added an additional array to store same values called sameValues and assign same values.

int num[]=new int[5]; int numm[]=new int[5]; int sameValues[] = new int[5];

for(int x=0;x<num.length;x++){
        num[x]=Integer.parseInt(JOptionPane.showInputDialog("Enter Values for num["+(x+1)+"]"));
        }

for(int x1=0;x1<num.length;x1++){
        numm[x1]=Integer.parseInt(JOptionPane.showInputDialog("Enter Values for num1["+(x1+1)+"]"));
        }

int counter = 0;
String tosho="";
for(int a=0;a<num.length;a++){
        for(int a1=0;a1<numm.length;a1++){
                if(num[a] == numm[a1]){
                        sameValues[counter] = num[a];
                        counter++;
                 }
        }
}

for(int x=0;x<counter ;x++){
        tosho+=(num[x]) + " ";
}

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.