0

Ok, so my teacher asked us to have a user input a set of letters and numbers. So I got it all done, except I have one problem, my variable wont increment. Here is my code...

import java.util.Scanner;

public class Find {

    public static int LetterAdd = 0;

    public static void main(String[] args) {
        Scanner scan = new Scanner (System.in);
        int Number;
        String Words;

        System.out.print ("Enter some letters: ");
        Words = scan.next();

        System.out.print ("Enter a number: ");
        Number = scan.nextInt();

        Find (Words, Number);
    }

    public static void Find (String Words, double Number)
    {

        for (int Counter = 0; Counter < Words.length(); Counter++)
        {
            Words.toUpperCase();
            String X = Words;
            String X2;
            X2 = X.substring(Counter, Counter + 1);

            if (X2 == "A") {
                LetterAdd += 1;
            } else if (X2 == "B") {
                LetterAdd += 2;
            } else if (X2 == "C") {
                LetterAdd += 3;
            } else if (X2 == "D") {
                LetterAdd += 4;
            } else if (X2 == "E") {
                LetterAdd += 5;
            } else if (X2 == "F") {
                LetterAdd += 6;
            } else if (X2 == "G") {
                LetterAdd += 7;
            } else if (X2 == "H") {
                LetterAdd += 8;
            } else if (X2 == "I") {
                LetterAdd += 9;
            } else if (X2 == "J") {
                LetterAdd += 10;
            } else if (X2 == "K") {
                LetterAdd += 11;
            } else if (X2 == "L") {
                LetterAdd += 12;
            } else if (X2 == "M") {
                LetterAdd += 13;
            } else if (X2 == "N") {
                LetterAdd += 14;
            } else if (X2 == "O") {
                LetterAdd += 15;
            } else if (X2 == "P") {
                LetterAdd += 16;
            } else if (X2 == "Q") {
                LetterAdd += 17;
            } else if (X2 == "R") {
                LetterAdd += 18;
            } else if (X2 == "S") {
                LetterAdd += 19;
            } else if (X2 == "T") {
                LetterAdd += 20;
            } else if (X2 == "U") {
                LetterAdd += 21;
            } else if (X2 == "V") {
                LetterAdd += 22;
            } else if (X2 == "W") {
                LetterAdd += 23;
            } else if (X2 == "X") {
                LetterAdd += 24;
            } else if (X2 == "Y") {
                LetterAdd += 25;
            } else if (X2 == "Z") {
                LetterAdd += 26;
            }
        }
        System.out.println ("The sum of your letters plus your number is: " + (LetterAdd + Number));
    }
}
Reinard
  • 3,624
  • 10
  • 43
  • 61
Jonco98
  • 392
  • 2
  • 20
  • That's a _lot_ of unnecessary typing. `X2 - 65 + 1` would replace that entire if/else if chain. Just FYI – Mike G Oct 06 '15 at 15:42
  • @mikeTheLiar What do you mean? I'm new to Java so I need some explaining – Jonco98 Oct 06 '15 at 15:45
  • What variable are you talking about? Which one isn't incrementing? – Captain Man Oct 06 '15 at 15:55
  • @Jonco Well, you'd have to convert to a char first but chars are just ints under the hood. Check [this chart](http://www.asciitable.com/) out - capital A has a value of 65. So, convert the string to a char, then to an int, subtract 65 to remove the offset and add 1 to get the value you want. So you can replace that entire if/else chain with like 3 lines of code. – Mike G Oct 06 '15 at 15:55
  • @Jonco98, each character in ASCII has a value, 65 is 'A', 90 is 'Z', 97 is 'a', and 122 is 'z'. There should be some way to get these numbers from the char, so you don't have to have a case for each letter. – Captain Man Oct 06 '15 at 15:58
  • @mikeTheLiar I see what you mean, but I don't know how to do that – Jonco98 Oct 06 '15 at 16:02
  • @Jonco98 my Java's a little rusty but something like `for(char c in Words.toCharArray()) { LetterAdd += ((int) c - 65 + 1); }` – Mike G Oct 06 '15 at 16:05
  • @Jonco98 shall we take this to chat? http://chat.stackoverflow.com/rooms/91510/jonco98 – Mike G Oct 06 '15 at 16:18
  • @mikeTheLiar Ok, so I tried this and `in` causes an error, instead I looked up the `For` loop and I used `:` instead, it now works, thanks – Jonco98 Oct 06 '15 at 16:18

0 Answers0