Comparing two Char arrays
Hello, this is a method that is part of my own string class that I am making for a project. The method compares two character arrays, and returns an int based on a few criteria.
A 0 if they are equal
A (+) number if the current instance is lexicographically greater than the one in the parameter
A (-) number if the current instance is lexicographically less than the one in the parameter
This is what I have so far.
***Note - this is put into a test main file, it isn't part of my class yet. I'm just using it to test stuff quickly.
Code:
char array1 []= new char []{'A','p','p','E'};
char array2 []= new char []{'A','p','p','E'};
int value = -9999;
boolean getOut = false;
for(int i=0; i<array1.length; i++)
{
while (getOut != true)
{
if (array1[i] == array2[i])
value = 0;
else if (array1[i] > array2[i])
{
getOut = true;
value = i+1;
}
else if (array1[i] < array2[i])
{
getOut = true;
value = -1*(i+1);
}
}
}
System.out.println(value);
When I try to run it my CPU goes to 100% and stays there so I might have made something bad... But wouldn't this do what I want it to do?
***Another note - the getOut boolean is supposed to terminate the for loop when it first finds two chars that are different and return the value.
Thanks for any help!
Edit - WOOPS I did make an infinite loop. Since array1 and array2 are equal right now in the code, it never exits the while loop. hmmm.
Edit2 - Well, I tried doing it with loops and conditions, but never thought of just using "break;" in the two if statements
All is well now :D