Dimebag
Dimebag

Reputation: 843

Scanner delimiter passed with parameter?

I need to parse an input-line character by character, and this will be done through several methods. To do it char by char, I am using useDelimiter(""). My question is: do i need to set this delimiter in every method? Or is it enough once, at the beginning?

e.g.

void start() {
    Scanner in = new Scanner(System.in);
    in.useDelimiter("");
    char first = in.next();
    readSecond(in);
    ...
}

void readSecond(Scanner in) {
    //in.useDelimiter(""); <-- is this needed?
    char second = in.next();
    ...
}

Example input: A5c*vd

Thanks !

Upvotes: 0

Views: 216

Answers (2)

Jean Logeart
Jean Logeart

Reputation: 53819

Once set, the delimiter stays the same.

Therefore, you do not need to set it again to the same value.

Upvotes: 1

Timothy Nguyen
Timothy Nguyen

Reputation: 68

You wouldn't have to set it every time if you declare and initialize the Scanner object in the class body that the methods are in. If you initialize the Scanner in each method, then I think you would have to set the delimiter in each method body.

Upvotes: 2

Related Questions