Array Help....

im trying to enter a word take the word apart letter by letter and score each letter with a number value. then add all of the values for each letter up for one language and do this for 10 different languages. I have two problems here.

The first is... its not printing out 10 values nor is it even printing the correct value of the first value in the array.

heres my method.

public String scoreWord()

{

int [] amount= new int[10];

String scoreValue="";

for (int k=0; k<10; k++)

{

int score=0;

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

{

int j=1;

String letter=word.substring(i,j);

j++;

if (letter=="A" || letter=="a")

score+=(int) values[k][0];

else if (letter=="B" || letter=="b")

score+=(int) values[k][1];

else if (letter=="C" || letter=="c")

score+=(int) values[k][2];

else if (letter=="D" || letter=="d")

score+=(int) values[k][3];

else if (letter=="E" || letter=="e")

score+=(int) values[k][4];

else if (letter=="F" || letter=="f")

score+=(int) values[k][5];

else if (letter=="G" || letter=="g")

score+=(int) values[k][6];

else if (letter=="H" || letter=="h")

score+=(int) values[k][7];

else if (letter=="I" || letter=="i")

score+=(int) values[k][8];

else if (letter=="J" || letter=="j")

score+=(int) values[k][9];

else if (letter=="K" || letter=="k")

score+=(int) values[k][10];

else if (letter=="L" || letter=="l")

score+=(int) values[k][11];

else if (letter=="M" || letter=="m")

score+=(int) values[k][12];

else if (letter=="N" || letter=="n")

score+=(int) values[k][13];

else if (letter=="O" || letter=="o")

score+=(int) values[k][14];

else if (letter=="P" || letter=="p")

score+=(int) values[k][15];

else if (letter=="Q" || letter=="q")

score+=(int) values[k][16];

else if (letter=="R" || letter=="r")

score+=(int) values[k][17];

else if (letter=="S" || letter=="s")

score+=(int) values[k][18];

else if (letter=="T" || letter=="t")

score+=(int) values[k][19];

else if (letter=="U" || letter=="u")

score+=(int) values[k][20];

else if (letter=="V" || letter=="v")

score+=(int) values[k][21];

else if (letter=="W" || letter=="w")

score+=(int) values[k][22];

else if (letter=="X" || letter=="x")

score+=(int) values[k][23];

else if (letter=="Y" || letter=="y")

score+=(int) values[k][24];

else if (letter=="Z" || letter=="z")

score+=(int) values[k][25];

else

score+=0;

amount[k]= score;

}

}

for (int t=0; t><10; t++)

{

scoreValue="Word Score: " + amount[t];

}

return scoreValue;

}

[2862 byte] By [zito358a] at [2007-11-26 18:37:11]
# 1
> if (letter=="A" || letter=="a")One of the top newbie mistakes is thinking they can compare String values like that. There is an equals method. Use it.
warnerjaa at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 2
do you know that chars have are really just unsigned ints? also, do you know that you can use the charAt function with a for loop instead of your current approach. google for charAt.
kikemellya at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 3
> > if (letter=="A" || letter=="a")> One of the top newbie mistakes is thinking they can> compare String values like that. There is an> equals method. Use it.if you stick with equals as above then check out equalsIgnoreCase.
kikemellya at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 4

thanks man, that does help. I guess I wasnt thinking, im just trying to get it done as quickly as possible. umm, am I using the substring wrong? because now it wont even give me a zero...?!?! and the error says im using the substring wrong.

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

at java.lang.String.substring(String.java:1768)

at scoring.scoreWord(scoring.java:67)

at prog2client.main(prog2client.java:24

zito358a at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Plus that mess of if statements could be simplified to:

char letter = word.toUpperCase().charAt(i);

if ((letter >= 'A') && (letter <= 'Z'))

score += (int) values[k][letter - 'A'];

Edit: fixed goofs

Message was edited by:

warnerja

warnerjaa at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 6
a word of advice, whenever you feel you have to write than many "if-esle-if" statements then something is wrong. stop and think, usually it can be much easier and a lot less code. as the previous post.
kikemellya at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 7
you are stepping outside of the length of the string.for (int n = 0; n < stringname.length; n++){stuff here}
grilleda at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 8
true that... Haha. how long have you guys been doing java? I have been in school doing it for about 5 months. Im a noob at it. How can I get it to print 10 values from that method, instead of just the 1?
zito358a at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...
# 9

use a for loop and iterate through and array where you would have stored the values.

//something like this

char [] myValues = {'a','b','c'};

for (int i=0; i <myValues.length; i++)

{

System.out.println(myValues[i]);

}

>

kikemellya at 2007-7-9 6:11:16 > top of Java-index,Java Essentials,Java Programming...