Computer Association of SIUE - Forums

CAOS Forums => Questions and Answers => Topic started by: Rick Lynch on 2008-08-21T22:07:11-05:00 (Thursday)

Title: Java Input and Output Help
Post by: Rick Lynch on 2008-08-21T22:07:11-05:00 (Thursday)
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!
Title: Re: Java Input and Output Help
Post by: William Grim on 2008-08-21T23:17:44-05:00 (Thursday)
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.
Title: Re: Java Input and Output Help
Post by: Tony on 2008-08-21T23:25:42-05:00 (Thursday)
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 (http://java.sun.com/j2se/1.5.0/docs/api/).
Title: Re: Java Input and Output Help
Post by: Rick Lynch on 2008-08-23T01:32:18-05:00 (Saturday)
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!
Title: Re: Java Input and Output Help
Post by: Tony on 2008-08-24T21:47:52-05:00 (Sunday)
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.
Title: Re: Java Input and Output Help
Post by: Shaun Martin on 2008-08-27T16:57:03-05:00 (Wednesday)
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
Title: Re: Java Input and Output Help
Post by: Tony on 2008-08-27T19:12:45-05:00 (Wednesday)
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. 
Title: Re: Java Input and Output Help
Post by: 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.
Title: Re: Java Input and Output Help
Post by: Tony on 2008-11-12T19:44:50-06:00 (Wednesday)
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.
Title: Re: Java Input and Output Help
Post by: Kamek on 2008-11-12T22:21:30-06:00 (Wednesday)
I just wanted to get my number of posts to 4.  With 4 posts you earn the position "I'm a Jason Davis"...
Title: Re: Java Input and Output Help
Post by: Feather on 2008-11-12T22:27:33-06:00 (Wednesday)
I think Rick has an AWESOME last name.
Title: Re: Java Input and Output Help
Post by: Shaun Martin on 2008-11-13T08:52:06-06:00 (Thursday)
 :offtopic: