Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read - Write to a file with JAVA

Posted on 2004-03-29
21
Medium Priority
?
223 Views
Last Modified: 2010-03-31
I want to create a simple program that allows me to read from a file and store the information contained in the file into an array.

The file should contain this:

ClientId        ClientAge
1                   23.6
2                   21.7
3                   15
4                   67
5                   54.1

The program should read this values and keep them in an array or list or something, then when I press for example 'a', then I list all the clients with their ages. When I press 's', then I can select an specific age.

Please give me a hand and post the whole code of the progra, so I can earn some java.

Thank you.
0
Comment
Question by:probine
  • 9
  • 4
  • 4
  • +1
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10703770
This looks like classwork. Please see the guidelines about classwork
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703831
You can search for sample code here:

http://www.javaalmanac.com
0
 

Author Comment

by:probine
ID: 10703841
CEHJ

Where can I see the guidelines for class work ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:probine
ID: 10703860
mayankeagle I have been to that link, there are many sub links... which one is the one for reading and writing to a file. There are many, but no one suggestes anything like it

Does it, which one ?
0
 
LVL 24

Accepted Solution

by:
sciuriware earned 112 total points
ID: 10703879
http://www.javaalmanac.com/egs/java.io/File2ByteArray.html

you will want to read into an array of floats or doubles, but the principle is the same.

;JOOP!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 108 total points
ID: 10703891
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703921
For your case, it would be better to make a Class containing clientID and clientAge as data-members and you should make a Vector or an array of such objects and write the whole collection/ array to a file and read it from the file:

http://www.javaalmanac.com/egs/java.io/SerializeObj.html
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703927
You can de-serialize this way:

http://www.javaalmanac.com/egs/java.io/DeserializeObj.html?l=rel

(the link is also available in the previous page) in "Related Examples".
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10703939
You might buy the entire book.

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703943
>> Where can I see the guidelines for class work ?

Perhaps the member-agreement. I guess you did not read it completely while joining:

http://www.experts-exchange.com/memberAgreement.jsp
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703952
>> You might buy the entire book.

You don't need to.

JOOP ;-)
0
 

Author Comment

by:probine
ID: 10703953
I do not really know java, I am trying to learn... so if you give me some code I do not know what classes to incude or anything ike it.

I would like to have something like:

Classses to include
The main program with its functions

When I have this, I can compile it and start analyzing the codes.

Can you code a simple main() for me with a class that reads from a file and prints what the file has in the screen ?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10703975
>> Can you code a simple main() for me with a class that reads from a file and prints what the file has in the screen

Well, now THAT is exactly against the member-agreement. We cannot provide full-code for homework. But I can give you a rough idea (just a skeletal code). Something like:

class Client
{
  public int ID ;
  public int age ;

}

(You can make the data-members private and give get/ set methods instead.)

Further, in your main () method, you can make an array to store objects: Client[] array = new Client[10] ;

Read from the array using a for loop:

for ( int i = 0 ; i < array.length ; i ++ )
  // read values for array[i].ID and array[i].age

Write it to a file using the code at the links posted above. Then you can read from the file and display the contents.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10703983
So it's clear: you really need a good book or a basic course.
JAVA almanac is an excellent book, but it gives examples for those who know some JAVA.
I think you should run or download the JAVA tutorial from SUN first:

http://java.sun.com/docs/books/tutorial/index.html

;JOOP!
0
 

Author Comment

by:probine
ID: 10747686
WELL... WELL... WELL...

As I said... I need some code... I do not want to buy a book or anything like it... the code should be simple.

I already made it with out having to buy a good book or anything like it.

I would very much like to get my points refund... unless some of you thinks that deserves them !!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10747700
You won't get your points refunded probably. That only happens when you ask a legitimate question that no-one can answer. I suggest you try a simple web search for the answer if that's not too much trouble for you.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10751621
You already have enough code samples on this page, and links to more samples too. You cannot state that you need a refund after we have given so many answers (because they would be helpful to you) - its not that they are irrelevant.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 10752416
The problem is that 'probine' doesn't want to learn (enough basical) JAVA (yet).

It's like asking info about the steering wheel, but avoiding to learn to ride the car.

EE is an excellent place to get yourself put on the right track, but we can't write all
programs for you entirely. If you look around, you'll see that we all seek knowledge,
not take-away-food.

;JOOP!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11683335
Please proceed with that recommendation.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question