• 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

Reading input from a file

 
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Svend,
the fastest solution:

Regards
Olli N.
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(nastiness removed)
[ September 22, 2003: Message edited by: Paul Wheaton ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bye all.
 
Trailboss
Posts: 23778
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
(sorry for the hijack ...)
I thought Bill said that nobody would ever need more than 640k of memory. Perhaps my memory fails me ...
 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right, Paul
reply
    Bookmark Topic Watch Topic
  • New Topic