1

So for a class, I need to make a program that scores a word based off of scrabble values for a letter. So aa should be 2 since 1 a is worth 1 point. But for some reason, my program only shows aa as 1 point.

import java.util.Scanner;

public class Scrabble {   
       public static void main(String[] args) {
            String[] 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"};

            int[] values = {1, 3, 3, 2, 1, 4,  
               2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10,
               1, 1, 1, 1, 4, 4, 8, 4, 10};

            Scanner kboard = new Scanner(System.in);
            System.out.println("Input your word.");

            String choice = kboard.nextLine();

            int score = 0;
            for(int i = 0; i < choice.length(); i++) {
                for(int n = 0; n < 26; n++)   {
                    if(choice.substring(i).equals(alphabet[n]))
                    {
                       score += values[n];
                    }
                }
             }

             System.out.println("The score for your word is " + score);
       }

}
Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Isaiah Mitchell
  • 75
  • 3
  • 11

4 Answers4

3

The problem is that someStr.substring(i) doesn't return only a single letter. For example, the output of System.out.println("aaa".substring(1)); is "aa", not a.

Here's a modified version:

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'}; 
int[] values = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
Scanner kboard = new Scanner(System.in); System.out.println("Input your word.");
String choice = kboard.nextLine();
int score = 0;
for(int i = 0; i < choice.length(); i++) {
  for(int n = 0; n < 26; n++) {
    // Use charAt here instead of substring
    if(choice.charAt(i) == alphabet[n])
    {
      score += values[n];
    }
  }
}
System.out.println("The score for your word is " + score);

}

Even better, you could just use a Hash Table for the alphabet and avoid the inner loop.

0

Because "aa".substring(0) == "aa" which != "a". So basically this is an expensive 'score last char'.

Use choice.charAt(index) instead. (also, you could just us a char to map lookup to skip iterating over every char)

Tezra
  • 8,463
  • 3
  • 31
  • 68
0

You're using the wrong version of the substring, I'd assume you wanted to use substring(int beginIndex, int endIndex)

This version of substring should enable you to select only one character substring to compare against your alphabets array. However, when using substring(int beginIndex) the substring begins with the character at the specified index and extends to the end of this string, meaning you're selecting more than one character substring (except when you're at alphabet.length-1 ).

Alternatively to solve the problem at hand, simply change your if condition to this:

if(Character.toString(choice.charAt(i)).equals(alphabet[n]))
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Try this solution:

import java.util.Scanner;

public class Scrabble {
    public static void main(String[] args) {
        String[] 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"};
        int[] values = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

        Scanner kboard = new Scanner(System.in);
        System.out.println("Input your word.");
        String choice = kboard.nextLine();

        int score = 0;
        for(int i = 0; i < choice.length(); i++) {
            for(int n = 0; n < 26; n++) {
                if(choice.substring(i, i + 1).equals(alphabet[n])) {
                    score += values[n];
                }
            }
        }

        System.out.println("The score for your word is " + score);
    }
}

Basically, you are not using substring() correctly. You only entered a begin index, which means that it reads from that point until the end of the string. I changed it to read to the end index, which in your case is i+1.

Basically in you first iteration you were looking for "aa" in alphabet and only in your second iteration were you looking for only "a" in alphabet, which is why you didn't get your first '1 point' in the first iteration.

Tmr
  • 268
  • 1
  • 7