Array pairwise matching in java give error also store data between two similar element

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Array pairwise matching in java give error also store data between two similar element



I have two array. They are represent x-coordinates and y-coordinates. The code is like that if both coordinates are same it print a statement "Both co ordinates are same" else they print They are not same.



My code is


public static void main(String args){
doublexcoordinate=2.3,1.2,3.3,5.5,2.3,1.3,7.9,1.2,3.3,3.3,5.2;
doubleycordinate=5.4,2.2,4.4,6.6,5.4,1.9,5.2,2.2,3.5,4.4,4.2;
int i=0,k=1;
while(i<xcoordinate.length)
//if(xcoordinate[i]&&ycordinate[i]==xcoordinate[k]&&ycordinate[k])
if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k])
System.out.println("Both co ordinates are same");
i++;
if(k<xcoordinate.length)
k = i + 1;



else
System.out.println("They are not same");

k++;




If I analysis the array I can see that 2.3 and 5.4 are pair,1.2 and 2.2 are pair and so on. As you can see 2.3 and 5.4 is repeat at 5th pair. So in this time they print They are same.



But the code block not run for that if statement.



Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11



How to eliminate this error and also how to store the They are not same types of data?



Like want to store 1.2,3.3,5.5and2.2,4.4,6.6 and 3.3,5.5,2.3,1.3,7.9and4.4,6.6,5.4,1.9,5.2 ..... separately.





Shouldn't it be if(xcoordinate[i] == ycordinate[i] && xcoordinate[k] == ycordinate[k])?
– ernest_k
Aug 10 at 13:07


if(xcoordinate[i] == ycordinate[i] && xcoordinate[k] == ycordinate[k])





Your problem has nothing to do with your algorithm (pairwise matching). You have a simple syntactical error. Note that left and right operands of && must evaluate to a boolean expression.
– Florian Albrecht
Aug 10 at 13:08






You should use a for-loop instead of while. You will loop infinitely if not all your coordinates are the same.
– T A
Aug 10 at 13:15





@FlorianAlbrecht I change my code block and new problem arise. Now it is related to my pairwise algorithm problem.
– Saswati
Aug 10 at 13:42





Have edited my code to match your new requirements! hope it helps
– Shubham Chopra
Aug 10 at 13:52




4 Answers
4



Apart from the answer from Simion who is solving your evaluation problem, will you end up having an enless loop, cause you only increment i (i++) when your numbers are the same.





that is why i have written a well working code below
– Shubham Chopra
Aug 10 at 13:28





Had the editor open for a while didn't saw that you already answered it.
– Snubs
Aug 10 at 13:30





@ShubhamChopra I change my code block and new problem arise.
– Saswati
Aug 10 at 13:41





@Saswati what is the problem?
– Shubham Chopra
Aug 10 at 13:41




I believe you are trying to keep track of repeating co-ordinates,
If yes, then this might help!


int i=0;
HashMap<Double,Double> hst = new HashMap<Double,Double>();
while(i<xcoordinate.length)

if(hst.containsKey(xcoordinate[i]))
if(hst.get(xcoordinate[i]) == ycordinate[i])
System.out.println("Occured before and now again at pos" +i);
i++;
continue;


System.out.println("Not occured before");
hst.put(xcoordinate[i],ycordinate[i]);
i++;





That is ok but how could I get the values of those co-ordinates which came not occur before. I want to create a subdata set from actual data set. Like that I mentioned earlier 1.2,3.3,5.5and2.2,4.4,6.6 is my 1st sub dataset,3.3,5.5,2.3,1.3,7.9and4.4,6.6,5.4,1.9,5.2 my 2nd sub dataset .... so on.
– Saswati
Aug 10 at 15:37



You need to change, you have a syntax error:


if((xcoordinate[i]==xcoordinate[k])&&(ycordinate[i]==ycordinate[k]))



If expressions don't evaluate as you coded.



EDIT 2: Here is the full workable code:


public static void main(String args)
doublexcoordinate=2.3,1.2,3.3,5.5,2.3,1.3,7.9,1.2,3.3,3.3,5.2;
doubleycordinate=5.4,2.2,4.4,6.6,5.4,1.9,5.2,2.2,3.5,4.4,4.2;
for(int i = 0; i < xcoordinate.length; i++)
for(int k = i+1; k < xcoordinate.length; k++)
if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k])
System.out.println("Both co ordinates are same: " + i + "," + k);

else
System.out.println("They are not same" + i + " " + k);




EDIT 3: The same functionality, using while loops, the correct way of doing:


public static void main(String args)
doublexcoordinate=2.3,1.2,3.3,5.5,2.3,1.3,7.9,1.2,3.3,3.3,5.2;
doubleycordinate=5.4,2.2,4.4,6.6,5.4,1.9,5.2,2.2,3.5,4.4,4.2;
int i = 0;
while(i < xcoordinate.length)

int k = i+1;
while(k < xcoordinate.length)

if(xcoordinate[i]==xcoordinate[k]&&ycordinate[i]==ycordinate[k])
System.out.println("Both co ordinates are same: " + i + "," + k);

else
System.out.println("They are not same" + i + " " + k);

k++;

i++;






You are missing a closing bracket.
– T A
Aug 10 at 13:14





@TA you can edit the answer :)
– Rakesha Shastri
Aug 10 at 13:15





You were right, tx.
– Simion
Aug 10 at 13:15





@Simion I change my code block and new problem arise.
– Saswati
Aug 10 at 13:41





@Shubham Chopra, you are very wrong, i just wrote the entire code in Java Fiddle, pay attention at the outputs. I didn't ewven looked into your code. Before assuming something you should be sure about that. Second: you did it differently, i do not save the values at all, i do all checks without keeping old indexes. Very rude from your side.
– Simion
Aug 10 at 13:57




!Edited to match new requirements



This code prints out where the coordinates are pairwise matched


public class Arraypairmatching

public static void main(String args)

doublexcoordinate=2.3,1.2,3.3,5.5,2.3,1.3,7.9,1.2,3.3,3.3,5.2;
doubleycoordinate=5.4,2.2,4.4,6.6,5.4,1.9,5.2,2.2,3.5,4.4,4.2;

double repeatedx = new double[xcoordinate.length];
double repeatedy = new double[ycoordinate.length];

int k=0;

for(int i=0;i<xcoordinate.length-1;i++)

double xcoordinate1 = xcoordinate[i];
double ycoordinate1=ycoordinate[i];
for(int j=i+1;j<xcoordinate.length;j++)

double xcoordinate2 = xcoordinate[j];
double ycoordinate2=ycoordinate[j];
if((xcoordinate1==xcoordinate2) && (ycoordinate1==ycoordinate2))

System.out.println("same pair repeated at index "+i+" and "+j+" index");
repeatedx[k]=xcoordinate[i];
repeatedy[k]=ycoordinate[i];
k++;





//to print the repeated x values
System.out.print("repeated x values ");
for(int l=0;l<k;l++)

System.out.print(repeatedx[l]+" ");

//to print the repeated y values
System.out.print("repeated y values ");
for(int m=0;m<k;m++)

System.out.print(repeatedy[m]+" ");





Output-


same pair repeated at index 0 and 4 index
same pair repeated at index 1 and 7 index
same pair repeated at index 2 and 9 index
repeated x values 2.3 1.2 3.3 repeated y values 5.4 2.2 4.4






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard