简体   繁体   中英

"invalid character constant" error

//takes out random letters if taking out all of the vowles does not get it to the right number of charachters
    while(tweet.length()> TWEET_LENGTH && vowles == 0){
        int i = ranNum.nextInt(tweet.lenght());
        char c = tweet.charAt(i);
        if ((c !=''))
        {
            tweet = tweet.substring(0,i) + tweet.substring(i+1);

this is the code i have and i keep getting an error at this point

 if ((c !=''))      

where the two single quotes are saying "invalid character constant" I'm not sure what i am doing wrong. any tips?

There is no empty character. Based on the comment, you should be using 'a', 'e', 'i' ...

There is no such thing as a blank character. It is possible to have an empty String - that would be a String with zero characters in it. But a character always has a value.

Even if your code somehow compiled, it would be checking for an impossibility - tweet.charAt(i) is going to return the character at location i . How could there not be a character at that location? (If it was off the end of the String, it would throw an exception.)

If you wanted to check for a space, you could use ' ' instead of '' . A space is a valid character that the compiler is happy to let you use.

The following code compiles and runs (but doesn't extract the vowels; you didn't show us that code and it's outside the scope of this question anyway.) It's almost identical to what you have, except I changed that '' to ' ' and added the obvious stuff to make it compile.

import java.util.Random;

public class Answers
{
    public static void main(String[] args)
    {
        Random ranNum = new Random();
        String tweet = " \"So the combination is... one, two, three, four, five? That's the stupidest combination I've ever heard in my life! That's the kind of thing an idiot would have on his luggage!\"";
        final int TWEET_LENGTH = 140;

        while(tweet.length() > TWEET_LENGTH){
            int i = ranNum.nextInt(tweet.length());
            char c = tweet.charAt(i);
            if ((c !=' '))
            {
                tweet = tweet.substring(0,i) + tweet.substring(i+1);
            }
        }

        System.out.println(tweet);
        System.out.println(tweet.length());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM