• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading input from a file  RSS feed

 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!
I need to get some input from a file and process the input in an
application. The input could f.eg. be
----
1 2 3
12 3 4
----
And I have to print the integers
My approach:

The "funny" thing is, that I get this output:
49 32 50 32 51 13 10 49 50 32 51 32 52
Can anyone help?
Thanks in advance
- Svend Rost
 
Oliver Nautsch
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Svend,
the fastest solution:

Regards
Olli N.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice how you are also printing out all the spaces and returns? Not to mention that "12" isn't going to show up as "12", it is going to show up as "1" then "2" (two separate characters).
Instead, I would use a BufferedReader, read in each line, use a StringTokenizer to separate the fields and print those out. That ensures each integer is printed out the way it is in the file, and you don't get all the extra characters that you don't want to see.
 
Ouaknin lionel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

if you would treat the numbers you have to use Integer.parseInt(String s);
[ September 20, 2003: Message edited by: Ouaknin lionel ]
* Code editing by Jason so that it actually compiles... must be the nitpicker in me
[ September 23, 2003: Message edited by: jason adam ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While that is all nice and complex looking, it doesn't really meet the requirement of just reading lines from a file and printing out each number. No need to make things harder than they need to be!
 
Ouaknin lionel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(nastiness removed)
[ September 22, 2003: Message edited by: Paul Wheaton ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy Mr. Lionel,
Might I remind you of the number one rule at JavaRanch:
Be Nice!
You'll find we don't much tolerate varmints what won't follow this one. Consider yourself warned.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you can show me where in the original post there is any mention of needing to search for a particular character pattern? I'd gladly rescind my earlier post if you can. From what I can tell, the program simply needs to print out the data in a file.
Also, if speed isn't an issue or a requirement, I doubt saving those extra micro or milliseconds is worth it, especially if the individual is just learning how to use the I/O API. Cutting and pasting your code may work, but there is no learning involved.
Perhaps Svend might like to elaborate on the needs of the program.
So as Ernest nicely stated, Ouaknin, refrain from insulting the volunteers who are here to help people learn, or anyone else on this site for that matter. I'm sure you will have plenty of opportunities to contribute your technical prowess in the forums, would hate to have that privilege (and it is a privilege, mind you) revoked simply because you can't play nice.
 
Ouaknin lionel
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bye all.
 
paul wheaton
Trailboss
Posts: 22534
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(sorry for the hijack ...)
I thought Bill said that nobody would ever need more than 640k of memory. Perhaps my memory fails me ...
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, Paul
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!