aspose file tools*
The moose likes Beginning Java and the fly likes I/O Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I/O" Watch "I/O" New topic
Author

I/O

Kim Vue
Greenhorn

Joined: Oct 01, 2003
Posts: 9
I'm reading integer data from a file and want to display them onto the console. I'm using BufferedReader and capturing the data as strings. My results do not appear as integers, as I had expected. I cannot figure how to convert the string back into an integer. Please help.

My code:
public class MyFileReader
{
public static void main(String args[]) {
String file = "nonRT.txt";
BufferedReader reader;
try{
reader = new BufferedReader(new FileReader(file));
String line;
while((line = reader.readLine()) != null){
StringTokenizer st = new StringTokenizer(line);
while (st.hasMoreTokens())
System.out.println(st.nextToken());
}
} catch (FileNotFoundException fileError){
System.out.println("Where is the file?");
} catch (IOException e){ System.out.println("Error with I/O.");}

}
} //end class MyFileReader
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi Kim
What exactly you mean by the following sentence here?
My results do not appear as integers, as I had expected.
Also, it would be more helpful to know,
1. What is the output you are getting?
2. What is the output you were expecting?
3. What is the error/exception you are getting if any?
I don't see problem in your code except that I would like to suggest one change that is to close the 'reader' object once you are done with the file reading. Though this seem not be a problem with your code if you are not seeing the output the way you expect from the System.out.println() statement...
Regards
Maulin
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
When I tried your code, its output seemed OK to me. That is, the strings displayed as integers. Therefore, I am not sure what you mean when you say the "results do not appear as integer". However, I had to create the data file nonRT.txt. Perhaps my nonRT.txt is formatted differently than yours. Thus, perhaps the problem is in the data file. Can you show us what it contains?
However, you also ask how to convert a string containing an integer into its binary (that is, int) equivalent. You can use Integer.parseInt(). I reworked your program to demonstrate its use.


For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi "Herb Schildt"
Sorry for posting this diversion but are you the same author who wrote one of the "C" book (and probably C++ as well)??
Wow!! if thats true then I see elite community joining.
Regards
Maulin
Kim Vue
Greenhorn

Joined: Oct 01, 2003
Posts: 9
Sorry for the ambiguity or incompleteness in my initial request for help. Textfile nonRT contains two integers separated by a comma on each line. I'm trying to read in the data and separate the integers. My first goal here is to simply read from the file and make sure that I'm capturing the correct data--which I was--only I didn't realize that my console window displayed only the last half of the data so it seemed my results were incorrect. I only figured this after incorporating the ..parse(Str)..and receiving the exception message. It seems to function correctly now. Are the integer data being stored as String objects? If they are, will I run into problems trying to use them as integers later on in the program? I will need to use them to do calculations.
Your comments have been very helpful. I appreciate it.
-Kim-
Steve Lovelace
Ranch Hand

Joined: Sep 03, 2003
Posts: 125
Take a look at the docs for StringTokenizer. You just have to create your tokenizer so that it knows that comma is a separator.


The Inner that is named is not the true Inner.
Herb Schildt
Author
Ranch Hand

Joined: Oct 01, 2003
Posts: 239
Kim: In a text file, integers (and other types of numeric data) are normally stored in their human-readable (i.e., text) form. Thus, they are not typically stored as Java String objects. Because of the way that your program is written, you are simply converting them into String objects by your use of StringTokenizer. (Nothing wrong with this.) However, the call to Integer.parseInt() converts a String that contains a valid integer into an int. Once this conversion has been accomplished, you can use that int for computations.
Maulin: Yes, I am the author of various C and C++ books, as well as my books on Java. I have some free time right now and thought that it would be fun to participate in the JavaRanch forums. I have always enjoyed helping beginners and the "be nice" culture of JavaRanch provides the perfect opportunity. Also, the high level of expertise and professionalism at JavaRanch has really impressed me. This is a great Web site! Finally, thanks for your kind words, but the "elite community" is JavaRanch!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Herb: Thanks, and welcome!
Kim: We don't have too many rules here at the Ranch, but besides "be nice", we also have our naming policy. One of your required two names seems to have fallen off! Please head over to here as soon as possible and stick it back on. Thanks, pardner, and see ya 'round the Ranch real soon now, hear?


[Jess in Action][AskingGoodQuestions]
Kim Vue
Greenhorn

Joined: Oct 01, 2003
Posts: 9
My apology to JavaRanch...Sorry...I was not aware that my last name was required. Now I know and it's been taken care of. Thanks to Ernest. Thanks to all who have provided me with helpful responses. It has made my first experience in JavaRanch an extremely effective one. I'll be bock!
Kim Vue
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I/O
 
Similar Threads
how can i pull out every number from a vector
StringTokenizer with hasNext method as condition
Question about Tree Node --Word Tree
help with a statement
I/O: searching an address book text file