Hi Radhika, In this example if loop is looking for a integer value(sc.hasNextInt). And "white space" is a default delimiter of scanner class. That's why you are getting "123 82". They both are only the right entries for what code is looking for. I think you will clear now.
In order to get 123 3 45 76 82, you have to have a space delimiter in between them. for 123 that is first entry having a white space forward, for 3 is not a valid integer(as it is 3b, so you will have it if you have \d\w expression), for 45, it has no white space(it can be read like c,45,), for 76, it has no white space(it can be read like x5x,76), for 82, it has a white space.
Thanks for the replies.I just want to clear one more thing.When the while loop is executed for the 3rd time,the token would be "3b",as "sc.hasNext()" returns "3b".And then the control goes to if condition,where it checks for "sc.hasNextInt()". My assumption is that the condition gives the boolean value 'true' as '3' is an integer.Please tell me if iam correct or wrong. -Radhika.
I know this thread is old but just wanted to comment on the output of this code because when i went through the code and looked at the output posted i thought i was wrong but it seems like no one compiled and ran this code. When i compiled and ran the code i got:
Can you show us what you've tried when you've compiled and run this code? Have you added any System.out.println statements?
We're not trying to be hard on you Dinesh, but we've all experienced the fact that really the ONLY way you're going to be able to learn this stuff is by writing code, running it, testing it, and trying lots of stuff out.
I'm not ashamed to admit to you that when we wrote the book we wrote HUNDREDS of programs and for many of them we tested many variations so that we could learn how this stuff works.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)