code.hesh code.hesh - 5 months ago 19
Java Question

Combination of two methods are not working - Java

In this class I'm getting a string value from user. This has 2 functions.


  • According to the word length of the input I want to assign points/marks(Ex: If the length of user input is 3 , I want to assign 3 points).

  • Also each alphabetical letter has different point value.

    As the final output I want to show a score combining those two methods.
    Ex: (If user inputs 'APPLE' , final score should be= alphabetical points( 1+3+3+1+1) + wordlength points(9)
    Total = 9+9 = 18.



I have tried this ,but the final score is not generating. Please help me to fix this issue.

public void ScoreWord()
{
for(i=0; i < word.length();i++);
{
wordchar = word.charAt(i-1);
lettervalue=0;

switch(wordchar)
{
case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R': case 'S': case 'T': case 'U':
lettervalue= 1;
break;

case 'D': case 'G':
lettervalue=2;
break;

case 'B': case 'C': case 'M': case 'P':
lettervalue=3;
break;

case 'F': case 'H': case 'Y': case 'V': case 'W':
lettervalue=4;
break;

case 'K':
lettervalue=5;
break;

case 'J': case 'X':
lettervalue=8;
break;

case 'Q': case 'Z':
lettervalue=10;
break;
}


if(words.length == 1)
{
scores= lettervalue+wordlength_score[0];
}
if(words.length == 2)
{
scores= lettervalue+wordlength_score[1];
}
if(words.length == 3)
{
scores= lettervalue+wordlength_score[2];
}
if(words.length == 4)
{
scores= lettervalue+wordlength_score[3];
}
if(words.length == 5)
{
scores= lettervalue+wordlength_score[4];
}
if(words.length == 6)
{
scores= lettervalue+wordlength_score[5];
}
if(words.length == 7)
{
scores= lettervalue+wordlength_score[6];
}
if(words.length == 8)
{
scores= lettervalue+wordlength_score[7];
}
if(words.length == 9)
{
scores= lettervalue+wordlength_score[8];
}
if(words.length == 10)
{
scores= lettervalue+wordlength_score[9];
}
if(words.length == 11)
{
scores= lettervalue+wordlength_score[10];
}
if(words.length == 12)
{
scores= lettervalue+wordlength_score[11];
}

words[0]=word;

}

Answer

First of all you have to create a method that takes the strint(word) as a parameter and returns the score based on the latters:

public int letterScore(String word)
{
    int lettervalue = 0;
    char wordchar;
    for(int i=0; i < word.length();i++)//what was this ; here?
    {
        wordchar = word.charAt(i);

        switch(wordchar)
        {
            case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R':      case 'S': case 'T': case 'U':
            lettervalue+= 1;
            break;
            case 'D': case 'G':
            lettervalue+=2;
            break;
            case 'B': case 'C': case 'M': case 'P':
            lettervalue+=3;
            break;
            case 'F': case 'H': case 'Y': case 'V': case 'W':
            lettervalue+=4;
            break;
            case 'K':
            lettervalue+=5;
            break;
            case 'J': case 'X':
            lettervalue+=8;
            break;
            case 'Q': case 'Z':
            lettervalue+=10;
            break;        
       }



    }
    return lettervalue;
}

Now for example to can compute the score of a word like this:

String word = "APPLE";
int score = letterScore(word) + wordlength_score[word.length() - 1];//acording to you array the length must be [0,12] or else you will get an exception

EDIT:What you could also do is to create a method that computes the final score and return it, in that case you will have to do something like this:

int scorefinal(String word)
{
    int lettervalue = letterScore(word);//compute the letterscore
    return lettervalue + wordlength_score[word.length() - 1];
}
Comments