• 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

Scanner Example from K&B.

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear Friends Please Explain me this code.

import java.util.Scanner;
public class ScanNext{
public static void main(String args[]){
boolean b2,b;
int i;
String s,hits=" ";
Scanner s1=new Scanner(args[0]);
Scanner s2=new Scanner(args[0]);
while(b=s1.hasNext()){
s=s1.next();
hits += "s";
}
while(b=s2.hasNext()){
if(s2.hasNextInt()){
i=s2.nextInt();
hits+="i";
}else if(s2.hasNextBoolean()){
b2=s2.nextBoolean();
hits +="b";
}else{
s2.next();hits +="s2";}
}
System.out.println("hits" +hits);
}
}

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

it produces
hits ssssibis2

THANK YOU.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have modified the code a bit. Now you must be able to figure it out yourself.



Good luck,
Wouter Oet
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot:

Please note that there is a difference between

java ScanNext "1 true 34 hi"
java ScanNext 1 true 34 hi

Good luck,
Wouter Oet
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well thats a bit simple aruna

In the first while loop the default delemiter is 'space'. So as comes new literals may they be boolean, int etc. 's' is being concatenated

In the second while; again the defalut delimeter is space, but here you are being precise , as you are distinguishing between
-integer
-boolean
-others
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic