• 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

scanners

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Tester{
public static void main(String[] args){
Scanner sc = new Scanner("javachamp 2009, true 239, 309 false");

while (sc.hasNext()) {
if (sc.hasNextBoolean())

System.out.print("Boolean");
if (sc.hasNextInt())

System.out.print("Int");

sc.next();

}
}
}the answer i prediced was int boolean and int..but its boolean and int???m not getting the logic???
 
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how did you expect int boolean int? can you explain?
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raju Champaklal wrote:how did you expect int boolean int? can you explain?



first it will go to javachamp ..since its neither a boolean or int neither of if loops will be execute..next it goes to 2009 ..since its an integer it will go to for loop and then after ","it will go to boolean and int....
thats how i thought that i shud be int boolean and int
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well i get it like this first you get javachamp on which both int and boolean fail now we call the next method which returns the next token i.e 2009 ok then the loop ends now next time we agian call hasnextboolean method which checks the next token i.e true so it checks it and returns Boolean


may be i am wrong but this should be the reason according to me
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be BooleanIntBoolean.
Hint: Check the API documentation of hasNextXXX methods!
 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey i got you...thanks..it was very helpful.....
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well neha can you explain it in a somewhat more elaborate way
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude 2009, is not an int..you aremissing the comma here....the delimiters in scanner are space by default
 
Phungsuk Wangdu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok got it now
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic