• Welcome to Computer Association of SIUE - Forums.
 

Java Input and Output Help

Started by Rick Lynch, 2008-08-21T22:07:11-05:00 (Thursday)

Previous topic - Next topic

Rick Lynch

This may or may not be a dumb question...

Well, I've been learning some Java (go ahead and make fun of me now). The thing is, there is no longer the easy "cin" statement anymore. It is also even more of a pain when reading files. Can anyone please tell me the most proper way of doing input in Java? I have been using Scanner for file input and command line input and PrintWriter for file output. They work and what not.

I just set up this for command line input:
Scanner in = new Scanner(System.in);

I set up this for file input:
Scanner in = new Scanner(new File(filename));

And finally this for file output:
PrintWriter out = new PrintWriter(new File(filename));

I am just wondering if these are the best ways? I have been searching around and it seems there are so many different ways... i.e. BufferedReader, BufferedWriter, InputStreamReader, Console, etc...

Also, while going through some of the programming contest files (the one that is held each fall), I noticed that sometimes the way I am doing it didn't work well and I also noticed that the runners of the contest created a class to handle file input. There are problems when you switch up the types and have two of the same kind on one line, such as:

File:
10 23
Name

It wouldn't read anything after the second integer after telling it to in.nextLine() using Scanner, even if there wasn't a space after it!

Thanks a lot!
Rick Lynch - Junior
Applied Mathematics Major
(Don't let this fool you. I love CS as well!)

William Grim

#1
There are indeed many ways to do it in Java.  I haven't personally used Scanner.

You want to use an InputStream as your interface and FileInputStream as your concrete instance if you want to read binary data.  Of course, with the proper code, you can turn the binary data into unicode or ASCII, but it may be more difficult, depending on the situation.

If you want to read character data, as it seems you do, you want to use a Reader interface with a BufferedReader containing a FlieReader.  I'll leave figuring out why you want to use a BufferedReader wrapped around a FileReader instead of just a FileReader as an exercise for the reader (puns!).


Reader fin = new BufferedReader(new FileReader(filename));
Reader cin = new BufferedReader(new InputStreamReader(System.in));


P.S. There is nothing wrong with Java.  It wins in a lot of areas where using C++ would be tedious without sacrificing speed in most cases, and in some cases it's faster.  About the only real downside to Java is the fact that the VM likes to hold onto too much memory and deprive other system processes of their fair share.
William Grim
IT Associate, Morgan Stanley

Tony

#2
I agree with Grim.  Just google BufferedReader and you should find a javadoc that explains it all.  Any time you need to know something about a class in Java look for its javadoc.  You can find the javadoc for BufferedReader here.
I would rather be hated for doing what I believe in, than loved for doing what I don't.

Rick Lynch

Thanks guys. Don't worry Tony, I have the API downloaded already and bookmarked, haha.

Using BufferedReader just seems so hard... and in that certain situation where I have two ints on one line, you can't simply just do

String input = fin.readLine();
int numOne = Integer.parseInt(input);

It will produce an error.

Scanner was introduced in Java 5.0 I think. It is super easy, but for whatever reason, it too sometimes runs into problems reading endlines and whitespace (such at the end of the line after reading in the two integers). I don't know.

Thank you guys for your input!
Rick Lynch - Junior
Applied Mathematics Major
(Don't let this fool you. I love CS as well!)

Tony

Yeah, Java isn't the best for reading and writing.  If you use the BufferedReader you can use read and just read in single characters.  I believe there is even a readByte or something like that.  I had to write a program that involved parsing a lot of strings and looking for various tokens and I just spent some time and used the BufferedReader to make my own class that can do whatever I need.  I am not sure why they didn't write an easy to use one like in C++.  Maybe they just figure they gave you the basics and if we want more we can do it ourselves.

Good luck.
I would rather be hated for doing what I believe in, than loved for doing what I don't.

Shaun Martin

Quote from: Tony on 2008-08-24T21:47:52-05:00 (Sunday)
Maybe they just figure they gave you the basics and if we want more we can do it ourselves.

That's why Java has interfaces Tony.  Don't like their List implementation?  Implement it yourself.  :D
Shaun Martin
SIUE Alumni
Associate IT Analyst, AT&T Services, Inc. St. Louis, MO.

Tony

Quote from: Shaun Martin on 2008-08-27T16:57:03-05:00 (Wednesday)
That's why Java has interfaces Tony.  Don't like their List implementation?  Implement it yourself.  :D

Yeah, that was a facetious comment. 
I would rather be hated for doing what I believe in, than loved for doing what I don't.

Kamek

That is one of only a handful of words in the English language that has the vowels in order.  Another one is abstemious.

I agree with Grim.  You should use the BufferedReader and BufferedWriter.

Tony

Quote from: Kamek on 2008-11-12T18:17:46-06:00 (Wednesday)
That is one of only a handful of words in the English language that has the vowels in order.  Another one is abstemious.

I agree with Grim.  You should use the BufferedReader and BufferedWriter.

Wow, I never noticed that.  It is funny that you found this old post just to comment on that lol.
I would rather be hated for doing what I believe in, than loved for doing what I don't.

Kamek

I just wanted to get my number of posts to 4.  With 4 posts you earn the position "I'm a Jason Davis"...

Feather

I think Rick has an AWESOME last name.
Heather Lynch - Senior
Major - Computer Science
Minor - Mathematics and Business Administration

Shaun Martin

Shaun Martin
SIUE Alumni
Associate IT Analyst, AT&T Services, Inc. St. Louis, MO.