473,411 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Simple Java Question

[color=#0040BF]Dear All,

I have a huge text file with name animal.txt, I have the following sample data:

>id1
lion
>id2
horse
cat
>id3
mouse
tiger


I need to save the contents of this file in an array or String without the id line. i.e
string1 = "lion"
string2= "horse
cat"
string3= "mouse
tiger"

So how can I store the data in string or array without the id line .. just only the names?

Thanks for your cooperation

[/color]
Oct 28 '08 #1
3 1492
Nepomuk
3,112 Expert 2GB
Hi newjavastudent! Welcome to bytes.com!

It's great to have you here!

When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags.

About your question: You can search for certain things in a String, e.g. the indexOf(String str) method mentioned in the API should do the job in your case. Read the description in the Link I gave you and you should be able to solve the rest of the problem yourself.

Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

Greetings,
Nepomuk
Oct 28 '08 #2
chaarmann
785 Expert 512MB
First, you need to load the file and store it as a string in memory.
Then you can use String methods to find and replace characters.
Read about "regular expressions", they are very useful here.
Then you need to store the string back on disk as a file, overwriting the old one.

Try to write your own load and store methods and regular expressions.
It's not that complicated or long (my code is has 7 lines for reading a string, and 1 line for text change by using a regular expression), and you will learn a lot.
If you are stuck, list them here what you have done so far, and I will correct them or will give you more hints or will show you the code that solves your problem.

Tip:
for reading a file, you should make a re-usabel method:
"public static String readFileAsString(String sourceFileName) throws IOException"
This method contains source code that uses following classes:
- class File, methods: File(filename), exists()
- class FileInputStream, methods: FileInputStream(name), available(), read (byte[] data)
- class String, methods: new(byte[] data)
- class IOException, methods: IOException(errorMessage)

Happy programming,
Chris
Oct 29 '08 #3
Nepomuk
3,112 Expert 2GB
First, you need to load the file and store it as a string in memory.
[...]
Then you need to store the string back on disk as a file, overwriting the old one.

Try to write your own load and store methods and regular expressions.
It's not that complicated or long (my code is has 7 lines for reading a string, and 1 line for text change by using a regular expression), and you will learn a lot.
Another Tip: We actually have a howto on how to read and write files in Java here. Try to follow that tutorial and you should learn a lot and get your task solved at the same time! :-)

Greetings,
Nepomuk
Oct 29 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: martin | last post by:
Hello, I just got the SUN Java IDE. (Netbeans IDE 3.5.1) Very very nice, and I worked myself through the tutorial (about making a colorswitch). Now, When I compile it gives no errors at all. So...
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
13
by: aum | last post by:
Hi, I'm a Python programmer, just starting to get into javascript. On reading some of the js guides, and not liking any of the OO usage patterns I saw, I've cooked up something which python...
7
by: James Harris | last post by:
My Googling has let me down here - maybe because when it comes to Java I don't really know which type I need! If someone could point me in the right direction I'd appreciate it. My requirement...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
11
by: Olaf Krumnow | last post by:
Hi, I have a requirement that, I thought, was very simple, but in fact turned out as impossible for me. I read some posts here and the language specs from MS, but couldn't get a solution. ...
1
by: epilogue | last post by:
Hey guys Im pretty new to Java and while I am finding it enjoyable i am getting several errors!!! Do you think you could help me on this particular question of an Assignment im doing. I have to...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
5
by: sayeo87 | last post by:
Hi, I am quite new to JSP so please forgive me if I ask really simple things... I am trying to run system commands on the server and display the output on a webpage. This is what I've got: <%@...
5
by: Ibys | last post by:
Hi, i am just starting to learn javascript, so i am probably doing something very simple wrong. i have read a lot of articles on maths in java, but cant find anything simple enough for my problem. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.