aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused about Scanner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused about Scanner class" Watch "confused about Scanner class" New topic
Author

confused about Scanner class

Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
this code is from the K & B scjp 6 book(pg 505)



if this program is invoked with
% java ScanNext "1 true 34 hi"

it produces the output
hits ssssibis2

can anyone explain me the program....how does it work...i am confused


SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

can anyone explain me the program....how does it work...i am confused


It would help us, if you explain what are you confused with, as the example looks straightforward.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
i know its simple...but how does it print the output...that is confusing me
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

hi Faisal,
Just check your program you are using "s", "i", "b", "s2" all these are strings not reference variable or primitive variable.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
i knw they are strings....but how does the pattern matching takes place....
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 791

Would you tell me what you want to know from your question ? You can see Sun's Java Tutorial for Pattern matching.
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Posts: 174
Hi i feel you are confused about the output.

So here the pattern you give is 1 true 34 hi
so the first while loop executes for all 4 and it prints s for each, so this gives the output ssss
now next while loop starts
so first the output is i since the first input is 1
then it sees the next input is true a boolean, so the output is b
then it sees the next input is again an int, so the output is 34
lastly the input is hi, which is neither boolean nor int, so the last case is successful and the output is s2

so this gives ibis2
so the total output is ssssibis2

hope it is clear. The best thing to do now is try with different inputs and see if you getting what you predict
Asmita Khamkar
Greenhorn

Joined: Mar 28, 2009
Posts: 25
Scanner class has the methods hasNext(), hasNextInt(), hasNextBoolean(). So in the first while loop it is only checking s1 i.e s1.hasNext() and concatenating s in hits variable every time this loop is run. In the next while loop it is checking s2 i.e. s2.hasNext() and within that loop it is checking s2.hasNextInt() or s2.hasNextBoolean() in if and is accordingly concatenating "i", "b" & "s2" in hits. Remember there are variables b and b2 declared as boolean but what you are concatenating in hits are strings "s", "b", "i" and not the values in variable. Try to understand the flow and I hope this clears your doubt.

Asmita
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
thanks a lot ranchers for clearing my silly doubt
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
here's another question



when it is invoked with the command line
java Regex2 "\d*" ab34ef

the output is
01234456

can anyone help me as to why the 4 is printed twice
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Faisal Ansari wrote:here's another question

Please use one thread per question - so if you have a new question, just open a new topic. That way you'll have a better chance of getting your question answered, than when you hide it on the back of another topic.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
il keep that in mind from the next time onwards....

can anyone help me with the answer to the above question
Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Posts: 41
Faisal Ansari wrote:il keep that in mind from the next time onwards....

can anyone help me with the answer to the above question


Well it goes like this

Pattern is = \d* --> That means zero or more occurrences of the integer
input is = ab34ef

so the pattern matches with all characters and the index of the occurence is returned with the method start() and group() will return the matching string. Because of * a,b, e and f are matched. At the 2nd index 34 will be matched and it will be printed, since it has *, it will also match the index beside the matched string which is 4 here, so 34 is the matching string and the 4 is the matching location for nothing, hence 4 printed twice.

So it is like - (0) a (1) b (2) 34 (3) wont be considered as the string is consumed, thats why 3 is not printed in the output (4) for nothing (5) e (6) f...all other indexes matching except 2nd index ( 34 ) are for the "*", means it matches the empty.

HTH
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
why isnt 3 printed twice...it is also an integer and is present at index 3
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

Faisal Ansari wrote:why isnt 3 printed twice...it is also an integer and is present at index 3


The "3" is actually at index 2.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

And BTW, this question is actually in the JavaRanch FAQ....

http://faq.javaranch.com/java/ScjpFaq#kb-regexp

Henry
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
Tnx Mr. Henry
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Posts: 124
Regarding your confusion on the program (your first question), you know that the source, '1 true 34' or whatever, which is
used to construct the Scanner object, is split by Scanner at every SPACE by default--so there's not any matching per se[/]
going on, it's [i]tokenizing
. And you can change the delimiter with the Scanner.useDelimiter() method, as you probably have read.

Then you work through the source with your Scanner object methods next() (gets the next token, and the tokens are all the
strings essentially that were separated by spaces in the source), nextInt() (gets the next int in the source and returns it AS AN
INT), etc.

Because you could get an exception if you call next(), nextInt() or other similar methods when there are no more tokens,
programs that use Scanner always make use of the hasNext() methods to make sure there is another token. The main
thing to remember about these is that the hasNext() method and hasNextInt() method and so on do not retrieve tokens
and so they do not advance the parser through the set of source tokens. That is, you could call hasNext() in an infinite
loop and never reach the end of the source--but if you called next(), or nextInt() in an infinite loop, you would definately
run out of tokens and get an exception.

I just thought that might have been what you were confused about.

SCJP 88% | SCWCD 84%
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Posts: 36
Tnx Mr.Ken
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused about Scanner class