1

I am trying to create a char array, but am getting an error everytime i run my code. This is my code:

public static int namevalue(String name){
    char[] charval={'', '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'};
    char let;
    int val=0;
    int letter;
    int total=0;
    int character;
    for (letter=0;letter<name.length();letter++){
        let=name.charAt(letter);
        if (charval[letter]==let){
            total+=letter;
        }
        total+=val;
    }
    return total;
}

And this is the error I am getting (main.java:10 is the line with the char array):

main.java:10: error: empty character literal char[] charval={'', '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'};

There is an arrow pointing towards the "'S', " part of my code in the debugger, and I am unsure why. If I remove that part, making it "'R', 'T', " the same error shows but pointed at the "'T'". Can someone please help me and tell me why? As far as I can tell, it is the same format as the other letters, which do not have an issue.

Thanks a lot

Sal
  • 1,471
  • 2
  • 15
  • 36
  • 4
    There is no empty character literal (`''`). Remove that, and your code is legal. `char[] charval={/*'', */'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'};` – Elliott Frisch Mar 09 '16 at 01:26
  • How come their is no error until the 's' then? – Sal Mar 09 '16 at 01:27
  • Either remove the first value `''` or insert a space between the single-quotes, depending on if you want the first value in `charval` to be a space or not. – Sci Prog Mar 09 '16 at 01:29
  • 1
    What is this code intended to do? And when do you imagine that `if` condition might be `false`? – Elliott Frisch Mar 09 '16 at 01:29
  • This is to answer project euler 22. I guess i should have added an else{ break;} line in there. Thanks for your help guys. – Sal Mar 09 '16 at 01:33

3 Answers3

1

Much like what was commented earlier, there is no literal ''. But, you can store a NULL value in there if you need to leave that spot open for an empty string.

char[] charval={'\u0000', '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'};

The value for a "NULL" char in java is '\u0000'. This is the default value of a char. This was pulled from what's the default value of char? .

Community
  • 1
  • 1
DarkJade
  • 270
  • 1
  • 8
0

I got this to run by adding a space to your first empty value:

 char[] charval = {' ', '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'};

Hope this helps.

aus
  • 69
  • 1
  • 11
-1

another, less elegant but I like its explicitness is

char[] charval = new char[] {' ', 'A', 'B', 'C'...};

though it is a little more verbose...

Scott Sosna
  • 1,443
  • 1
  • 8
  • 8