This week's book giveaway is in the Design forum.
We're giving away four copies of Experimentation for Engineers: From A/B testing to Bayesian optimization and have David Sweet on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

should be simple....reading char...

 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read from files before using the File class and readLine() methods but how do I read the file one character at a time??
I need to read in a large file, compare every character in the file count how many times it occurred, etc....
I can't seem to find anything that will do a readChar(), all of them have some sort of int as a return type and read in a character array....
What do I need?
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Readers, like InputStreamReader and BufferedReader have methods that read the input into a char array of your choosing, which you can then process one arrayfull at a time.
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, but won't that Array get huge??
Let's say I wanted to count the occurences of each letter of the alphabet in the Bill of Rights.
My array is going to have hundreds of thousands of entries.
Is there not a way to say :
read first character, do something, read next character do something, etc.
I am not interested in speed or effieciency, its just an exercise am I working on with some fellow java beginners.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The read() method of BufferedReader reads one character. Since it returns an int, I believe you just need to cast the result to char, and then you can do the desired processing on it before calling read() again.
Someone correct me if I am wrong...
(Like I even have to type that )
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I'll try that.
It "should" work, but hoping to avoid even having to cast, but it makes no real difference.
Thanx
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you use a bigger array, you just read one bufferfull at a time, say 1000 characters, process them (do your counting etc), then re-fill the buffer with the next 1000 characters and continue processing, etc.
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why not store it in collection it's easier and you can achieve the result with minimum work.
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because the I/O classes (Readers and Streams etc.) do not have methods that use collections.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need for that unless i missed something. Why not just use StreamTokenizer and then whenever reading from IO read it into it , it has ttype to check what type of characters dealing with and then append it to collection. I have sample to show if needed.
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure, I'd love to see a sample...
thanx
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi , this is dirty code , which should have been written much better but at the time i dind't and so its reduntant.
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Val,
Have you tried compiling and running that code? I ask because you have declared your main() method to throw an exception, rather than calling the other method that throws that exception in a try / catch block within main().
Is that legal? My understanding of exceptions is that you would only do something like that when you expect the caller of the method to deal with the exception (in a try /catch block). But in the case of main(), there will be no one calling that method but the VM...thus no way to deal with the exception if one is thrown.
Can anyone shed some light on this situation? Is it allowed? If it is allowed, it seems like it would be bad practice, since this would basically nullify the purpose of throwing an exception (by not dealing with it)...
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's legal. It's bad practice.
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like i said this is not a good code. It's just a toy that shows you that it can be done using Collection classes this won't be used in reall application ofcourse when your main throws any Exceptoins this is bad . It's just a toy.
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far I like the idea of reading into a character array.
I've done that, HOWEVER, in order to initialize that array I have to give it an initial size. Here begins the drama. I will never know the number of characters in the file being passed, therefore how will I know how large to set my array.
Here is what code I have so far:

Okay, that will read in what ever file I have but only up to 250 characters. If the file is too small, it appears that the rest of the data in the array should be null, however it won't let me compare char to null in order not to print extra spaces. I did a test to print what would be in one of the unoccupied spaces of the array and it appears to be a space. I can't compare char to space because even if that works, I want it to print the necessary spaces, (i.e. spaces between words).
Anymore suggestions?
Thanks
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could turn your char into a Character and store them in an ArrayList. Then it wouldnt matter what size the file is.
 
ryan headley
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I finally got it.
Created a FileReader, passed it the input file, created a LineNumberReader and passed it the FileReader. Then assigned a String readnums to null.
Inside a while I did something like this:

Now i've added GUI, a filechooser to choose different files. Then I will try to provide a graphical histogram...wish me luck.
Ryan
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So far I like the idea of reading into a character array.
I've done that, HOWEVER, in order to initialize that array I have to give it an initial size.

no no no. It only returns enough characters to fill the array. Then on the next read it fills the array again from where it left off.
From the API:

Note that it returns the number of chars actually read. So if you gave it an array of 1,000 entries but there were only 10 chars left to read, it returns 10. If there are 5,000 chars to read it only returns 1,000 and waits for the next read().
 
The City calls upon her steadfast protectors. Now for a tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic