• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Scanner class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody.. the following code creates scanner objects that read characters from a string and attempt to match the specified pattern.
Is there any way to read the whole file with the scanner object ? i mean, scan for the next subsequence that matches the pattern ? I want to avoid creating one scanner object per line readed.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Emanuel! Welcome to javaranch.

Why don't you read the file completely first, then scan it. like this



I have not tested this code but I think it will work...
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use scanner.next(), it will read the whole file:

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Emanuel,
the following code might help you


a scanner can directly take file as an argument...and the scanner's findInLine() can take only a String...so specify the pattern you want in a String and pass it to findInLine() and check the result

 
Emanuel Campolo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ! It works great, thank you.. just a little comment.. I replaced the hasNext(Pattern p) method with the no-arg hasNext() method. That's because, i suppose, if in the first token the pattern is not matched, the expression returns false and it will never get into the loop.

PS:. Sorry for my english, i learned the language reading kathy sierra's certification book


Originally posted by Ankit Garg:
Hi Emanuel! Welcome to javaranch.

Why don't you read the file completely first, then scan it. like this



I have not tested this code but I think it will work...

 
Emanuel Campolo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you saipavan vallabhaneni ! but with that code it doesn't even get into the loop.. I tried this and it runs but, while expression returns a false value when it is evaluated the first time.

Scanner s=new Scanner(texto)
while (s.findInLine(p) != null)
{
System.out.println(s.next());
}

Originally posted by saipavan vallabhaneni:
hi Emanuel,
the following code might help you


 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic