• 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
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Text Processing: "The method readLine() is undefined for the type String"

 
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm creating a program to do various forms of analysis on text input either from a file or directly into the TUI. Managed to get the wordCount method working find but the sentenceCount method needs some work.

I am receiving the error that  

"The method readLine() is undefined for the type String"



So I understand that this is not the correct method to be using, but I'm not sure which one to use instead.

Thanks in advance for any assistance.


 
Marshal
Posts: 80950
522
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I see a readLine() method, I start thinking of a BufferedReader. Strings and Scanners don't have a method of that name.
Don't mix Scanners and BufferedReaders.
This is one way to read from a file:-
  • 1: Don't use try if you are reading System.in. Don't close a Scanner reading System.in.
  • 2: The reading assumes a particular format for the file, e.g. “Campbell 123 456.789”
  • Relevant Java™ Tutorials links: 1 2.
     
    Campbell Ritchie
    Marshal
    Posts: 80950
    522
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Only use classes with names ending input/output stream for binary files. For text files use classes with names ending reader/writer. You may need to specify the encoding.
     
    Bartender
    Posts: 11148
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "sentence" on line 65 is a String. String does not have a readLine() method. Because your sentence is, in reality, one very long String containing multiple sentences, you could use the same approach that you used in your wordCount() method. The split() method takes a regular expression as an argument and you want to split on any one of three characters you'll need a regex of "[?!.]". The square brackets enclose a character set which is '?', '!', and '.'. You need the brackets because two of those characters are normally treated as control characters in regular expressions, which we don't want to do.

    Also,  a subtle issue with your wordCount() split: the regex should be " +" in order to treat multiple adjacent spaces as a single delimiter.

    And lastly, on line 27 where you are pasting lines together you are not leaving any space between the lines, so the last word ot the previous line will butt up against the fist word of the new line which will then be counted as a single  word later.
     
    Charles Ormond
    Ranch Hand
    Posts: 52
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Only use classes with names ending input/output stream for binary files. For text files use classes with names ending reader/writer. You may need to specify the encoding.



    Never heard this before. Will do.
     
    Campbell Ritchie
    Marshal
    Posts: 80950
    522
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Charles Ormond wrote:. . . Never heard this before. . . .

    Go through this Java™ Tutorials section and you should find that in it. We do see people not believing that, using a file input stream to read a text file; the input stream reads byte by byte and if you stick to ASCII characters only (range=0...\u007f or 0...127) you won't notice any problems. The byte ≤ 0x7f can easily be converted to a char because they would both have the same value. So they think they are all right with the input stream. Then they encounter a file containing non‑English letters (e.g. α é ß) and these can't be read properly, so we get posts about, “Why has my text file got corrupted?” A file reader would have no difficulty picking up those letters but it might need to know the encoding used. I am lucky: I use Linux ≥99% of the time, so I can almost always guess the correct encoding: UTF‑8.
     
    Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
    Clean our rivers and oceans from home
    https://www.kickstarter.com/projects/paulwheaton/willow-feeders
    reply
      Bookmark Topic Watch Topic
    • New Topic