• 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

File IO problems

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to read in a file and then count the number of each digit and each space/tab and every other character? I tried string tokenizer but that didn't work because the data isn't seperated by spaces ... eg 1 j 3 s ... it's more like 1j3s .....
And yes I know this is almost just like what I said in my other message.
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, there is a way.
I would use FileInputStream. I would read byte by byte. I would have an array of 256 ints that would count how many of each byte I saw.
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok I get the part about the InputStreamReader ... I have that already ... what do you mean with the array?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's important to read exactly what people write here. Paul didn't write InputStreamReader, but FileInputStream.
The key point is that any InputStream can read it's data byte by byte, without transforming into or out of unicode. If the data in your file is just ASCII characters, then reading from a FileInputStream will return either an ASCII character (in the range 0..255) or -1 on an error or end of file.
If you declare an integer array with 256 'slots', each slot can hold a count of the number of times that character appears in the file.
For example:

[This message has been edited by Frank Carver (edited February 11, 2000).]
 
paul wheaton
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't use a reader. I would use a plain stream.
Each byte that you encounter can have a value of 0 to 255. The values 32 to 127 are visible characters. If the file has end of line markers, you will certainly get values of 10 and or 13. So if you make an array of 256 ints, you can count each occurance of each byte encountered. As you read in a byte, use that value to index the array. Then increment that array value.
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
::sigh::
I wonder if I'll ever get java. I /think/ I understand what you guys are saying. I'm going to try it and see if it works. If it does *cheer* if not back to the drawing board eh.
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any idea why I'd get this when I run the program?

Enter the data file to be read: c:\jbuilder3\test1.txt
java.lang.reflect.InvocationTargetException: java.lang.ArrayIndexOutOfBoundsExce
ption: 0
at CharCount.main(CharCount.java:23)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<code>
java.lang.reflect.InvocationTargetException: java.lang.ArrayIndexOutOfBoundsException: 0
at CharCount.main(CharCount.java:23)
at java.lang.reflect.Method.invoke(Native Method)
at com.borland.jbuilder.util.BootStrap.invokeMain(Unknown Source)
at com.borland.jbuilder.util.BootStrap.main(Unknown Source)
</code>
Based on the message: look at file CharCount.java, line 23. That's what's throwing the exception. Is there an array there? Should the array have an element 0 (the first element)? The message says element 0 is what is out of bounds, which means the array must not have any elements at all. Is the array initialized in your code? Or does it come from somewhere else?
I gather you used Frank's code from above. There's something unstated about how the code must be run - see if you can track down where the error is coming from.
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok here is line 23 ....
FileInputStream fin = new FileInputStream(args[0]);
is it having a problem with the 0 after the args?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only if there is no element 0. Where does "args" come from? How is it defined? What's it used for?
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:P
Well I got the other problem all fixed and it works really well now. The only problem is it scrolls past the early characters when it displays the array type thing (technical phrase)...is there a command I can put in to have it pause halfway through or so? Or can I change the window somehow? (msdos prompt window)
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just direct the output to a file with your java command:
java -someoptions YourClassName > output.txt
Then you can browse it at your leisure with whatever editor you prefer.
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to do that already. I can't figure out how to find the longest string though. Any suggestions?
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If by "longest String" you mean the one with the largest count,
just keep track of the largest as you print them out, then summarize at the bottom. Or just keep track of the largest, and don't bother printing out each value.
For example: try the following modification to my program above;
replace the 'for' loop which prints out the values with:
 
Nevris
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By largest string I mean the largest line that was read in from the file. .... ie.
alkdisehals
adjladi
it would return the first one since it's longer than the second oen
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Uff, is that the thing what you want ?



------------------
With best of best regards, Pawel S. Veselov ( aka Black Angel )
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic