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

Input/Output Question...

Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Hey guys I need to write this program that ask the user to enter a file name, such as (number.txt), and then whatever numbers are on there, well I have to tell the largest number, smallest number, and the average.
It would be pretty simple to do, if I was using my SavitchIn.readLine command, but now I need to use file inputs and outputs...

I have no problem figuring out how to get input, like text, from a file, but I am having problems doing 'numbers', and then doing 'math' with them.
Any suggestions? My text file has to only have one number per line. (And that's simple enough to do).

Here's what I have so far....I'm just beginning, so don't chuckle. HA.
The output should go to the screen...hum??!!



) ) ) )
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
It would help if you would show us the error you are getting.

Ok, so I posted the error I get trying to compile it. It says, cannot find symbol "method readLineInt()" in "class java.io.BufferedReader" on the line you show getting an error.

So, if you go to the BufferedReader API, do you see a method readLineInt()?

Hmmm, I don't see that method. I do see a method readLine() that returns a String. Hmmmm. Could you get a String and then convert it to a number (such as an int if that's what you're looking for)?
[ February 16, 2005: Message edited by: Carol Enderlin ]
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Well see thats just it. I had a program already written....but using Strings..text files...and I need to work with numbers, (Integers).
I just don't understand how to switch it to numbers...does that make since? I guess I am totally brainless when it comes to java.
I'm trying to modify my old program to make a new one.....
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
I dont know that much about buffered readers.......just now trying to teach that to myself.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
That's what the API is for!

So, you've got Strings and you want Integers (or int). go take a look at the methods for String and Integer and see if you see anything to convert from a String to an int or Integer.

1.4.2 API

1.5.0 API
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Never even heard of API...but I'll take a peek at it. Thanks!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Alternatively, you could look at java.io.DataInputStream which can give you primitive data types directly from the input stream. To create a DataInputStream, you will probably want to use the constructor that takes an InputStream. This is what is commonly known as the Decorator pattern. You decorate InputStreams with other InputStreams to add certain functionality. In this case, it would look something like this:

The whole java.io package is a little daunting, but it is VERY flexible for many tasks. In the above example, the BufferedInputStream isn't strictly necessary, but it will improve performance slightly since it buffers the data from the FileInputStream and passes it along as necessary to the DataInputStream which is responsible for converting it into primitive data types.

HTH

Layne


Java API Documentation
The Java Tutorial
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Layne Lund:
Alternatively, you could look at java.io.DataInputStream which can give you primitive data types directly from the input stream. To create a DataInputStream, you will probably want to use the constructor that takes an InputStream. This is what is commonly known as the Decorator pattern. You decorate InputStreams with other InputStreams to add certain functionality. In this case, it would look something like this:

The whole java.io package is a little daunting, but it is VERY flexible for many tasks. In the above example, the BufferedInputStream isn't strictly necessary, but it will improve performance slightly since it buffers the data from the FileInputStream and passes it along as necessary to the DataInputStream which is responsible for converting it into primitive data types.

HTH

Layne



Layne,

The DataInputStream class is for binary input (reads 4 bytes for ints) rather than textual, which is what it looks like we're dealing with here.

Jim


James Carman, President<br />Carman Consulting, Inc.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Rose Evans:
Never even heard of API
API means Application Programming Interface and is just a fancy-pants way of saying "the public methods for a class" in this context. The API of all classes provided by Sun is referred to as the standard JavaDocs and should become your best friend when learning and using Java.

Get a feel for how it's laid out. In the frames view, you have packages in the top-left, all classes or classes for a particular package in the bottom-left and various views in the main right-hand pane like a class's detail, an index of all methods, a tree (class hierarchy) of all classes, etc.

If you only have a basic idea of what you want to do (e.g. turn a String into an int), your best bet is to look at the detail of a class that seems like it would do what you want. Carol has already given you enough hints to find the right class. Then just skim the method summaries at the top to see if something pops out at you.

As you get more familiar with them, you'll learn certain tricks for scanning quickly. For example, clearly you want an Integer or int, so you might scan down the "return type" column for one of those. Otherwise, just go off of the names and first line descriptions. For more detail, click on a method and it tells you exactly what it does, the parameters it expects, exceptions it may throw and why, and its return type, if any.

There are certain "core" classes in the java.lang and other packages that you should probably sit down and read completely -- at least the summaries, e.g. String. You'll save yourself future headache of trying to guess a method name, and you'll use those often. Another good source are Sun's tutorials which will give you a good overview of various packages (like the Streams tutorials). From there you can dig deeper as you need, but you'll have a good enough foundation that you'll usually know where to start looking.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Wow! You guys are certainly smart!!! Question??...Do you guys do Java as a living? I mean are you programmers, or is it a hobby, something you just wanted to learn? I am totally impressed with all of your knowledge....
And another question...how long did it take you to learn all the things you know? I know programming is a daily learning experience.

My hat is off to you all!!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You'll find all types here.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
I develop software for a living, currently using java. I love to learn, so that's a hobby for me.... SCJP, .NET ( ), javaranch book promos (my recent favorite: Pragmatic Project Automation by Mike Clark), tiger ...
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Never even heard of API...but I'll take a peek at it. Thanks!


This is a problem!

I see your posts on here Rose and you're always very nice so I want to be as cordial as possible here. Are you taking a java class, or teaching yourself from a book? If you're taking a class and the professor hasn't taught you about the API, go in there scream at him and ask him what his qualifications for teaching the class are. If you're learing from a book and it didn't tell you about the API pretty quickly find a new book.

The API can be your best friend and answer a lot of the questions that get asked here. Get comfortable with it and soon you'll be answering your own questions. Then you'll realize some of us here really aren't that smart, just know where to look to find the answers (and that's what programming is all about).
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Programming started as a hobby for me at age twelve. I slowly taught myself from a few books BASIC, Pascal, 6502 assembly language (Apple //e woot!), and a smidgen of C. Then at school I learned C and C++ and dabbled in Prolog (which made my brain hurt) and various math languages.

While at school I was hired to do contract programming. This is where my learning sped up greatly. I learned various client-server building tools (4th Dimension on the Mac, Visual Basic, and PowerBuilder), SQL (Sybase, MS, and Oracle). I did this to pay for school for about five years (I took a few quarters off from school).

Then I got a series of "real jobs" and did more contracting and have learned quite a few more languages on my own to satisfy my hobby aspect and do quick tools for work (Perl, PHP, more shell scripting languages, etc). Most of these I've learned less deeply than the others.

However, I've focused on Java since it was introduced in 1995. I played with it at home since it was so cool (in my book) and started using it at work in 1997. I've stuck to it since, which has actually been bugging me since I don't like working on just one thing at a time. Sure, I've picked up a lot of Java tools and APIs, but I miss learning an entirely new language. I think I may just have to go for C# to keep fresh and get a challenge.

I've been coding in one or more languages for over twenty years, so don't feel discouraged that we're smarter than you. Once you pick up multiple languages, picking up a new one is simply a matter of syntax. Right now you're learning new concepts like for loops and arrays.

Once you get those, learning for loops and arrays in C# is merely a matter of learning the syntax differences: where does the punctuation go and what is or isn't available. The other part is learning the APIs, but again you can pretty much count on C# having a substring() method of some sort, and you'll probably be able to guess where to look for it.

I consider myself very blessed to enjoy it so much and be paid highly to do it. I wish everyone could have that, as I think the world would be a much more peaceful place.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Carol, You sound like you have an exciting job!!! I hope one day I can be so fortunate to find a great programming job or something. Thanks so much for all of your help. It is greatly appreciated!!
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Hentay,

First of all, thanks so very much for being so nice!! I don't recall ever studying about API in school, and if we did, my brain must have been on vacation. Before writing this post just now, I got out my trusty java book, and looked up API in the back. I found one page in the Index for it, and so I turned to the page and read...This is all it said on API.....

FAQ: The term API stands for application programming interface. The API for a class is essentially the same thing as the user interface for the class. You will often see the term API when reading the documentation for class libraries.

That is all it said about it, I looked around, thinking I would find something else, but so far, that's it! I'm going to research around on the Internet for some info because my book sure doesn't tell me much. And yes, I'm learning Java in school. I'm in my second semester. Java is easier than C++ was to me. C++ and I didn't get along very well at all. Ha.

I just wonder, after I graduate, and go job hunting, well I just don't believe I am going to be that qualified for a job in Java...I mean I feel like I should know much more by now. I try, I really do. I guess that's all I can do.
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
David,

My hat is off to you! I'm so very glad you are paid well to program. You seem very great at it!! Wow you've been programming for 20 years!!!
I started out in QBasic myself, loved it, totally loved it, matter of fact, I just subsituted at my college for a teacher in a QBasic class because she was sick. I only had to do two classes, and I didn't have to really 'teach' the students anything, mainly just had to give them their assignments and stuff, but a few students asked for my help, and it was so fun to teach them a few of the things I had learned. In our school QBasic is the first programming language we learn. That kind of shows students if they really will like programming or not. I LOVED QBasic, to me, I wish there was still a market out there for it. I would spend hours just programming away until I got my program to do what I wanted it to do.

I only learned one semester of COBOL, that's an OK language, kinda outdated now a days too.
I had two semesters of Visual Basic.NET which I totally loved too. Matter of fact, I'd like to concentrate a little more on VB.NET but it's kind of hard to do when I am trying so hard in this Java stuff. I took two semesters of C++ too, didn't quite like that language. Java is a lot like C++ but less complicated. I like it a whole lot better.

Just curious...when you got your first job programming, I mean did you go in there and just start blindly programming away, or did you have other workers to teach you..or what? I know one instructor told me that school was just a basis to get the feel of something and the real learning would be at a job. Bu I certainly do not want to go in a job, and be like, uh duhhhhhhhhhhhhh I don't know how to do that. Ha.

Any suggestions?
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Ok guys you know me..questions, quetions...

Just wondering, programming in the 'real world' are most programs GUI's or DOS based stuff? To me, and I am just guessing, I have no idea, it would be more of a GUI situation.
I know in Visual Basic.NET everything is GUI. Ot at least everything we studied was GUI.
When I was learning C++, everything we learned was using DOS screens...just black and white, and no colorful GUI's.
And so far, we've mainly programmed using DOS screens in Java too.
I think we did maybe one GUI in Java, and maybe one tiny applet. To me, those are the things that are used in today's world, but I'm just guessing, I have no idea what's out there in the 'real world'.

Patiently awaiting your reply...
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
"real world" programming varies a lot. GUI code is very common, and won't be getting any less common, for the obvious reasons - desktop computers used by non-computer-experts will never become any less common than they are.

but there's still a lot of non-graphical code, and i think there always will be. partly because there will always be a few server computers chugging away in darkened rooms without anyone but experts ever directly touching them, and for these, a GUI is really pointless overkill. but also, once you get good at programming, it's often easier and quicker to write a text-only interface than a graphical one. so i for one like to write that first.

by that i mean, if and when i can, i write just the code to do the job i need done without much of an interface at all; then i bolt on a quick command-line interface that's non-graphical just so i can test it; and once i get it working more or less right, i start writing a graphical interface that uses and extends the classes and code i wrote for the "business logic". often, there's no need to ever throw away the quick, non-graphical interface either. perhaps noone will ever see or use it but me, but command-line-only interfaces take up next to no space, and geeks like me sometimes like them.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
What kind of interface you develop will depend highly on how the tool is used. When I started on the job, it was designing GUI apps backed by a database. I started on 4th Dimension on the Mac. It was very much like VB in that it had a visual drawing program that let you "draw" your GUI by laying out buttons and other controls visually. Then you'd write code attached to those GUI objects that did the work.

Skip ahead to my current job, and different pieces of the same "product" use both web GUIs for the users that buy music and the business users that manage the music database. The operations people that keep the servers running use command-line apps for putting content (the music itself and the data about the music like artist, album title, track information, etc) into the database. There are also several scripts that run directly in the database, but that looks just like the command-line tools really.

Initially, what you do will be determined by the job you get and your background. But as you get more experience, your goal is to find what you prefer to do and try to drive your career in that direction. Let me make it clear: since you'll constantly be learning on the job, your job in school is to learn how to learn. Learn how to research your own answers, choose and read books, find information on the net, etc. You cannot be expected to "just code" when you start your first job just as you don't stick a brand new attorney in front of a judge and jury by herself on a capital murder case!

I'm still learning while I work. That's primarily why I love programming so much -- it changes so fast that you can avoid getting stuck doing the same thing all the time. My belief is that when we stop learning our brains and bodies begin the process of dying. But even if you disagree with that, you'll probably find that you get bored doing the same thing day in, day out.

So again, build your learning muscles and techniques. Absolutely, keep asking questions here (that's just another tool to learn) and with your classmates. With the technology field being so large, you can't expect someone to be an expert in all of it, let alone a large part of it.

So don't worry about not being very experienced yet. They've had you bouncing around between very different languages, so you can't expect to know any one that well. But doing so gives you a good feel for which languages are good for which problems. VB is good for quick (or even large) GUI applications, especially backed by a database. But C++ would better serve you for writing a simple command-line tool that kicks off some process that doesn't require any interaction with the user. You could use VB, but it comes down to choosing the right tool for the job.

One last thing. When people talk about the "Java API," they are typically referring to the JavaDocs. As I said above, get to know how they are laid out as you'll return to them again and again for quick answers. When you find that you'll need to use a new class, it's a good idea to skim its API in the JavaDocs to get a feel for the jobs it will perform for you. You don't have to know that File.delete() will delete a file and not really tell you why it couldn't do it if it fails, but you can at least know that the File class is a good place to go when you want to delete a file.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Rose Evans:
Wow! You guys are certainly smart!!! Question??...Do you guys do Java as a living? I mean are you programmers, or is it a hobby, something you just wanted to learn? I am totally impressed with all of your knowledge....
And another question...how long did it take you to learn all the things you know? I know programming is a daily learning experience.

My hat is off to you all!!!

I am currently in school studying Computer Science. I will be graduating in May and am looking for a job. I'd love to get a job where I use Java every day, but I'll probably have to settle for any programming job I can find. I started learning Java 6 years ago. I already knew C++ quite well at the time, so I picked it up rather quickly. I haven't had any significant jobs where I used my coding skills, but I hope to get one soon.

Layne
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by James Carman:

The whole java.io package is a little daunting, but it is VERY flexible for many tasks. In the above example, the BufferedInputStream isn't strictly necessary, but it will improve performance slightly since it buffers the data from the FileInputStream and passes it along as necessary to the DataInputStream which is responsible for converting it into primitive data types.

HTH

Layne<hr></blockquote>


Layne,

The DataInputStream class is for binary input (reads 4 bytes for ints) rather than textual, which is what it looks like we're dealing with here.

Jim[/QB]


Doh! Thanks for pointing that out. I haven't used Java file I/O for a long time.

Layne
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
Layne,

May I ask....how old are you?
Rose Evans
Ranch Hand

Joined: Oct 14, 2004
Posts: 195
David and M Beck, thank you so much for your time and effort with such a long response to my question. It is greatly appreciated!!!
Wish me luck on my long journey of programming, I'm surely going to need it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Input/Output Question...