IntelliJ open source
[Logo] JavaRanch » Big Moose Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
Author

confused about Scanner class

Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Messages: 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
Bartender

Joined: Sep 28, 2004
Messages: 10034

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
Messages: 36

i know its simple...but how does it print the output...that is confusing me

SCJP 6
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Messages: 651

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 97% JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - API - Generics FAQ - JLS - JVM Spec - Java FAQs
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Messages: 36

i knw they are strings....but how does the pattern matching takes place....

SCJP 6
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Messages: 651

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

SCJP 5.0 97% JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - API - Generics FAQ - JLS - JVM Spec - Java FAQs
Jacob Sonia
Ranch Hand

Joined: Jun 28, 2009
Messages: 104

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
Messages: 20

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
Messages: 36

thanks a lot ranchers for clearing my silly doubt

SCJP 6
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Messages: 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

This message was edited 1 time. Last update was at by Faisal Ansari


SCJP 6
Jesper Young
Java Cowboy
Bartender

Joined: Aug 16, 2005
Messages: 7872

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 6.0 API documentation
Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Messages: 36

il keep that in mind from the next time onwards....

can anyone help me with the answer to the above question

SCJP 6
Parashuram Hallur
Ranch Hand

Joined: Sep 18, 2006
Messages: 40

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

This message was edited 2 times. Last update was at by Parashuram Hallur

Faisal Ansari
Ranch Hand

Joined: Jun 23, 2009
Messages: 36

why isnt 3 printed twice...it is also an integer and is present at index 3

SCJP 6
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10034

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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Bartender

Joined: Sep 28, 2004
Messages: 10034

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

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

Henry

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

Joined: Jun 23, 2009
Messages: 36

Tnx Mr. Henry

SCJP 6
Ken Truitt
Ranch Hand

Joined: Aug 23, 2007
Messages: 114

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
Messages: 36

Tnx Mr.Ken

SCJP 6
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Professional Certification » Programmer Certification (SCJP)
 
RSS feed
 
New topic
replay challenge