Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-02-2008, 12:07 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
[SOLVED] How to read a file and compare Array values
Hello friends!

I have a problem which i'm stuck on.

I have 2 sets of Arrays. Lets call them ArrayA and ArrayB.

They both contains values like 29396590, 29396591, 29396592 etc.

The idea is, if a value in ArrayA is found, it is replaced with the value in ArrayB.

ArrayA[0] = ArrayB[0]
ArrayA[1] = ArrayB[1] and so on..

I have a text file that contains lots of information. When I read in the text file, I need to be able to check each line for all the values in ArrayA. If one is found then it needs to be replaced with the corresponding value in ArrayB.

I'm using String replace if I find a value but i'm finding it hard to make it loop through ArrayA. The code I am using is causing errors and I cant figure out why

This is the code I currently have:

Code:
FileInputStream in = new FileInputStream(file.txt); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; while ((strLine = br.readLine()) != null){ for(int a = 0; a < myarrayA.length; a++){ if (strLine.contains(myarrayA[a])){ strLine = strLine.replaceAll(myarrayA[a], myarrayB[a]); } } System.out.println(strLine); }
The problem is making it loop through ArrayA looking for the values..

Here is an example of the txt file:

Code:
29396590;SIT PORT ;CHF; -14247.54;CHF; 29396591;SIT PORT ;EUR; 7363.11;EUR; 29396592;SIT PORT ;EUR; 12372.86;EUR; 29396593;SIT PORT ;JPY; -967715.56;JPY; 29396598;SIT PORT ;USD; -3746876.81;USD; 29396625;SIT PORT ;USD; 20354218.51;USD;
I hope you understand all that!! Thanks in advance for any help.
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-02-2008 at 04:54 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 12:45 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,338
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
How about the use of java.util.Arrays.equals()

Code:
if(Arrays.equals(myarrayA[i], myarrayB[j])){ // Do the processing here }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-02-2008, 03:22 PM
DonCash's Avatar
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 237
DonCash will become famous soon enoughDonCash will become famous soon enough
Ah im such a fool!!

The above code does actually work pefectly. The reason it wasn't working originally was because of a small error in another piece of the code. No wonder I couldn't put my finger on the problem.

All sorted now! Result
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|| Don't forget to:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-02-2008 at 04:53 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Reading a text file into an Array DonCash New To Java 6 04-01-2008 01:18 PM
[SOLVED] getting values from a text file dav9999 New To Java 8 04-01-2008 02:51 AM
How do you read from a file, and then store the info in an array? szimme101 New To Java 3 03-23-2008 10:29 PM
How to read attributes and values in a xml file using servlet pragathi_forum Advanced Java 1 12-18-2007 06:46 PM
How to compare file extensions? Rubin New To Java 1 08-05-2007 10:51 PM


All times are GMT +3. The time now is 02:45 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org