hbk
hbk

Reputation: 11184

Return to ... in Java (like goto)

I just started to learn Java, so have a lot of questions. And now I need to return to the beginning of program if a problem occurs.

public static int getchartoint() throws IOException {
    int a;
    try {   
        BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
        String k = bReader.readLine();
        a = Integer.parseInt(k);
        return a;
    }
    catch (NumberFormatException exc) {
        System.out.println(exc);
        return a = 0;
    }
    finally {

    }
}

and I have a = 0, I could write case in main() body:

case 0: {
    System.out.println("Your entered an incorrect number...");
}

My question is: how can I add a line that moves me to exactly that line of code?

Upvotes: 0

Views: 140

Answers (2)

user1985027
user1985027

Reputation:

Call the "getchartoint" method before your switch/case statements.

Then when it returns integer 0 it will execute the case statement.

Upvotes: 3

tterrace
tterrace

Reputation: 1985

It looks like you just want to return 0; instead of return a=0;.

Upvotes: 2

Related Questions