• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Count the number of characters, words, and lines in a file

 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you do this question? I can’t use the split method and BufferedReader.
info.PNG
[Thumbnail for info.PNG]
 
Master Rancher
Posts: 195
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ana,

Can you show us the code of what you have already tried yourself so far? What is working and where do you get stuck?
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:Hi Ana,

Can you show us the code of what you have already tried yourself so far? What is working and where do you get stuck?


This is what I have so far:

}
 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Notice that you set up a Scanner to read from the file that was chosen. Then you want to use it to read from that file... but instead you read from the Scanner where you asked for the file name.
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I use only one scanner the whole program and which should I delete?
 
Paul Clapham
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Should I use only one scanner the whole program



No. Your design with two scanners is perfectly fine.

and which should I delete?



You shouldn't delete either of them. To the contrary, you should use both of them. Your existing code doesn't do that.

Use one scanner to read from System.in and ask for the file name. Use the other one to read from that file.
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how to get the number of words or characters in the file.
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Ana Yo wrote:Should I use only one scanner the whole program



No. Your design with two scanners is perfectly fine.

and which should I delete?



You shouldn't delete either of them. To the contrary, you should use both of them. Your existing code doesn't do that.

Use one scanner to read from System.in and ask for the file name. Use the other one to read from that file.



Can you help me with the code and I will try to understand it and ask questions if I have any?
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:I don't know how to get the number of words or characters in the file.

Look up the documentation for String#split().
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ana Yo wrote:I don't know how to get the number of words or characters in the file.

Look up the documentation for String#split().

Our teacher doesn’t allow us to use split or BufferedReader
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Ana Yo wrote:Should I use only one scanner the whole program



No. Your design with two scanners is perfectly fine.

and which should I delete?



You shouldn't delete either of them. To the contrary, you should use both of them. Your existing code doesn't do that.

Use one scanner to read from System.in and ask for the file name. Use the other one to read from that file.


Your specs say file names is passed on the command line, so you'll need to remove the Scanner based on System.in.

Also, your specs are incomplete. How are we to know you can't use String#split()? What else haven't you told us?
 
Ana Yo
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Paul Clapham wrote:

Ana Yo wrote:Should I use only one scanner the whole program



No. Your design with two scanners is perfectly fine.

and which should I delete?



You shouldn't delete either of them. To the contrary, you should use both of them. Your existing code doesn't do that.

Use one scanner to read from System.in and ask for the file name. Use the other one to read from that file.


Your specs say file names is passed on the command line, so you'll need to remove the Scanner based on System.in.

Also, your specs are incomplete. How are we to know you can't use String#split()? What else haven't you told us?


Sorry about that, I edited my question:)
 
Marshal
Posts: 65466
249
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:. . . I edited my question:)

Please don't edit posts with replies.

I don't like over‑specified assignments, like the prohibition on buffered readers and split(). A buffered reader is a standard way to read files. Maybe prohibiting split() isn't quite as bad.

But, I think you are going the wrong way about the whole task. I think you have two tasks, one being to read the file, and the other being the counting. So divide the task up. Let's imagine you are doing the file reading first. Forget about getting the file name from the keyboard. Until you are reading your file correctly, do file reading and nothing else. Then consider the counting after that. Don't do any more counting until you have the file correctly read. I can see some pitfalls which can give you the wrong counts. But I am keeping quiet about them for the time being.
If you go through the Java™ Tutorials, you will find you are using legacy code. Don't create a File object. Do this:-That code still isn't right because you have to handle IOException or FileNotFoundException. This Java™ Tutorials part shows the correct way to handle such exceptions whilst also ensuring that the file is closed, which you are not doing at present.
Anything reading from or writing to a file must be closed after use; anything reading from or writing to System.in, System.out, or System.err should be left open for ever.

When you have got all that working reliably, then you should consider the next stage of the process.
 
Oh. Hi guys! Look at this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!