Solved

Read - Write to a file with JAVA

Posted on 2004-03-29
21
201 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
Comment Utility
This looks like classwork. Please see the guidelines about classwork
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You can search for sample code here:

http://www.javaalmanac.com
0
 

Author Comment

by:probine
Comment Utility
CEHJ

Where can I see the guidelines for class work ?
0
 

Author Comment

by:probine
Comment Utility
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
Comment Utility
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:mayankeagle
mayankeagle earned 27 total points
Comment Utility
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
You might buy the entire book.

;JOOP!
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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:mayankeagle
Comment Utility
>> You might buy the entire book.

You don't need to.

JOOP ;-)
0
 

Author Comment

by:probine
Comment Utility
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:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
Please proceed with that recommendation.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 59
triangle challenge 4 75
firstChar challenge 13 83
Running Jira on Raspberry PI 2? 3 121
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

17 Experts available now in Live!

Get 1:1 Help Now