Sergeant ROFLcopter
Sergeant ROFLcopter

Reputation: 9

Why am I getting this error with my for loop?

public boolean isNumber(String t) {
    for (int i = 0, i<= 9, i++) {
        if t.equals(i) {
                return true;
        }
    }
    return false;
}

Copypastad the wrong method originally -_- I have this inside a class compiling with this error:

data_structures/ExpressionEvaluator.java:40: illegal start of type for (int i = 0, i< 10, i++) {

Upvotes: 0

Views: 95

Answers (5)

Daniel Pereira
Daniel Pereira

Reputation: 2785

Your method only verifies if the String that you passed is a digit, not a number (a number can have more than one digit). You could verifiy it only using a char and calling the ,Character.isDigit

char c = '1';
boolean isDigit = Character.isDigit(c);

If you really want to create your own method, passing a String param, I suggest you to modify like this:

public boolean isDigit(String t) {
    return t.length() == 1 && Character.isDigit(t.charAt(0));
}

Upvotes: 0

kosa
kosa

Reputation: 66637

You should use semi-colon and your if should be surrounded with brackets.

public boolean isNumber(String t) {
    for (int i = 0; i <= 9; i++) {
        if (t.equals(i)) {
                return true;
        }
    }
    return false;
}

I would suggest reading Language Basics

Upvotes: 2

Uncle
Uncle

Reputation: 1

public boolean isNumber(String t) {
    for (int i = 0; i<= 9; i++) {
        if( t.equals(i) ){
                return true;
        }
    }
    return false;
}

1 . use ";" replace of ","

2 .

if(boolean) {
        //do stuff
     }

Upvotes: 0

FThompson
FThompson

Reputation: 28687

Semicolons separate the qualities of a for loop. Also, the condition of your if block must be surrounded by parentheses.

public boolean isNumber(String t) {
    for (int i = 0; i <= 9; i++) {
        if (t.equals(i)) {
            return true;
        }
    }
    return false;
}

Upvotes: 0

James McCracken
James McCracken

Reputation: 15766

Use semi-colons instead of commas.

for(int i = 0; i < 10; i++) {
    //do stuff
}

Upvotes: 3

Related Questions