FullCombatBeard
FullCombatBeard

Reputation: 313

Starting with Java, syntax error on tokens

For some reason its highlighting boolean and string as errors, I copied this code straight out of the textbook why wont it work? package practice;

public class practice{

public boolean isUniqueChars(string str){
if (str.length() > 256)
            return false;

        boolean[] char_set = new boolean[256];
        for (int i = 0; i< str.length(); i++){
            int val = str.charAt(i);
            if (char_set[val]) {
                return false;

            char_set[val] = true;
        }
        return true;
    }
}
Errors: Multiple markers at this line
- string cannot be resolved to a type
- Syntax error on token "boolean", @ 
 expected
- Syntax error on token ")", -> expected
- Syntax error on token(s), misplaced 
 construct(s)

Upvotes: 0

Views: 1004

Answers (3)

user4380042
user4380042

Reputation:

Use "S" for String declaration in Java.

isUniqueChars(String str)

Upvotes: 0

Manoj Sharma
Manoj Sharma

Reputation: 616

Well Try this:

package practice;

/**
 *
 * @author manoj.sharma
 */

public class Test{
public static void main(String [] a){
System.out.println(new Test().isUniqueChars("Hello world"));
}
public boolean isUniqueChars(String str){
    if (str.length() > 256)
        return false;

    boolean[] char_set = new boolean[256];
    for (int i = 0; i< str.length(); i++){
        int val = str.charAt(i);
        if (char_set[val]) {
            return false;
        }
        char_set[val] = true;
    }
    return true;
}
}

Upvotes: 1

Jayaram
Jayaram

Reputation: 1725

public boolean isUniqueChars(string str){

seems a typo, string should be String

Upvotes: 0

Related Questions