dimanche 28 décembre 2014

JAVA charAt(i) in a for loop not working

I've got an assignment to write a a method that accepts two strings as an argument and returns a value of 1,0,-1 (respectively) if the first string is lexicographicaly before, equal or after the second string, using only the charAt() and length() methods from the String class.


The problem is that even though I initialized an int variable in my for loop, it won't recognize them later in the loop when using the charAt method. The compiler keeps saying "unexpected type. required: variable; found: value". (using BlueJ).



public class Word {

private String _st;

/**
* range small letters: a = 97, z = 122.
*
* range capital letters: A = 65, Z = 90.
*/
public int myCompare (String s1, String s2) {
char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};


//1st loop i;
//2nd loop j;
//3rd loop k;
for (int i = 0; i < s1.length(); i++) {
_st = new String (s1);
if (_st.charAt(i) < 97) {
_st.charAt(i) += 32;
}

for (int j = 0; alphabet[j] == _st.charAt(i); j++){
int x = alphabet[j];
_st.charAt(i) = x;
}

}

return 1; // temporaray.
/*
if (s1.charAt(0) < s2.charAt(0)){
return 1;
} else if (s1.charAt(0) > s2.charAt(0)){
return -1;
} else {
return 0;
}*/
}


}


So what exactly am I doing wrong? thanx for any help :)


Aucun commentaire:

Enregistrer un commentaire