Results 1 to 3 of 3
  1. #1
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Question [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 :confused:

    This is the code I currently have:

    Java 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:

    Java 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.
    Last edited by DonCash; 04-02-2008 at 04:54 PM.
    Did this post help you? Please me! :cool:

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    How about the use of java.util.Arrays.equals()

    Java Code:
    if(Arrays.equals(myarrayA[i], myarrayB[j])){
    // Do the processing here
    }

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Thumbs up

    Ah im such a fool!! :o

    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 :D
    Last edited by DonCash; 04-02-2008 at 04:53 PM.
    Did this post help you? Please me! :cool:

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 01:51 AM
  2. Replies: 5
    Last Post: 07-30-2008, 10:30 AM
  3. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 02:51 AM
  4. How to read attributes and values in a xml file using servlet
    By pragathi_forum in forum Advanced Java
    Replies: 1
    Last Post: 12-18-2007, 06:46 PM
  5. How to compare file extensions?
    By Rubin in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •