• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

confused about Scanner class

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know its simple...but how does it print the output...that is confusing me
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Faisal,
Just check your program you are using "s", "i", "b", "s2" all these are strings not reference variable or primitive variable.
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i knw they are strings....but how does the pattern matching takes place....
 
Ninad Kulkarni
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would you tell me what you want to know from your question ? You can see Sun's Java Tutorial for Pattern matching.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot ranchers for clearing my silly doubt
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
il keep that in mind from the next time onwards....

can anyone help me with the answer to the above question
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why isnt 3 printed twice...it is also an integer and is present at index 3
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And BTW, this question is actually in the JavaRanch FAQ....

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

Henry
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx Mr. Henry
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Faisal Ansari
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnx Mr.Ken
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic