Solved

Read - Write to a file with JAVA

Posted on 2004-03-29
21
202 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
 

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 28 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 27 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now