Scanner class function

Hi,

I'm working with a nested-loop and trying to input numerical values into an array. But to end the loop I'm suppose to type in an | or %, but when I input it, it gives me an error. Could anybody explain to me what type of scanner methods I'm suppose to use in order to type in non-numerical numbers?

[319 byte] By [anonymousiguya] at [2007-10-2 12:52:00]
# 1

> Hi,

> I'm working with a nested-loop and trying to input

> numerical values into an array. But to end the loop

> I'm suppose to type in an | or %, but when I input

> it,

What do you mean "when I input it"? What is the exact code you're using to get that input?

> it gives me an error.

What's the exact error?

The following is not directed only at you. It's a general state of being fed up and it's been building for a while.

<rant>

Why the **** is it so hard for people to figure out that if they want help they should actually provide some information about their problem?

Do you go to the doctor and say, "I'm not well, what's wrong with me?" and then wait for him to cure you?

Come on, people, this is not a Java thing, this is just common sense, basic communication.

Cripes.

</rant>

Sorry. Anyway, back to your questions.

> Could anybody explain to

> me what type of scanner methods I'm suppose to use in

> order to type in non-numerical numbers?

Which ones do YOU think?

Here's a hint: There are methods that can check whether there's a number waiting to be read or not before you try reading it.

(I'm assuming you're using nextInt() or something and getting an exception when it's a % or whatever.)

jverda at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 2

This code :

Scanner scanner = new Scanner(System.in);

while (!scanner.hasNext(Pattern.compile("[%|]"))) {

if (scanner.hasNextInt()) {

System.err.println("This is an int : "+scanner.nextInt());

} else {

System.err.println("This is NOT an int : "+scanner.next());

}

}

System.err.println("Bye-bye");

outputs this :

152

This is an int : 152

36699

This is an int : 36699

ABD

This is NOT an int : ABD

toto

This is NOT an int : toto

%

Bye-bye

--Marc (http://jnative.sf.net)

mdentya at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 3

This is the error I get.

Exception in thread "main" java.util.InputMismatchException

at java.util.Scanner.throwFor(Scanner.java:819)

at java.util.Scanner.next(Scanner.java:1431)

at java.util.Scanner.nextLong(Scanner.java:2144)

at java.util.Scanner.nextLong(Scanner.java:2105)

at MProfit.main(MProfit.java:29)

I use

array = input.nextLong();

The array being a loop to get all of the values.

anonymousiguya at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 4
I see that there is a hasNextLong() methodBut then I'm not very sure on how to use it.When I type it as input.hasNextLong()I get the errorfound: booleanrequired: longarray = input.hasNextLong();^1 error
anonymousiguya at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 5
if (input.hasNextLong()){array[i] = input.nextLong();}Please use code tags (see button above posting box) when posting code.
MLRona at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 6

> I see that there is a hasNextLong() method

> But then I'm not very sure on how to use it.

> When I type it as input.hasNextLong()

> I get the error

> found: boolean

> required: long

> array[ i] = input.hasNextLong();

It's telling you that array [ i ] is a long, so you have to assign a long to it.

But hasNextLong doesn't return a long. It returns a boolean that, as the docs explain, will be true if the next token can be interpreted as a long, and false otherwise.

So you either do like MLRon showed you: if (input.hasNextLong()) {...} or, if you want, you can use a boolean variable, though that's not necessary: boolean nextIsLong = input.hasNextLong();

if (nextIsLong) {

// read the next long

}

else {

// it's one of the "special" inputs

}

jverda at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...
# 7
Thanks for the help everyone. This helped me out a lot.I'll be sure next time to type the code properly with the indents and etc.I'm still a novice at all of this, but again thanks.
anonymousiguya at 2007-7-13 10:04:55 > top of Java-index,Java Essentials,New To Java...