Jump to content

Anyone help me solve my current problem ?

- - - - -

  • Please log in to reply
76 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
My problem is I can't "AND" one file with another file.


As you know for "AND" function you will get "True" if both inputs are true.

Edited by xxxxjayxxx, 19 April 2011 - 10:51 AM.


#2
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
anybody ????? :(

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think it will all become a lot more clear if you do

                        if (strLine.charAt(i) == '1') {

                            result1.set(i, true);

                            System.out.print([B][SIZE="4"][COLOR="purple"]i +": " +[/COLOR][/SIZE][/B] result1.get(i) + "  ");

                        } else if (strLine.charAt(i) == '0') {

                            result1.set(i, false);

                            System.out.print([B][COLOR="purple"][SIZE="4"]i +": " +[/SIZE][/COLOR] [/B]result1.get(i) + "  ");

                        }


So just add i + ": " to everything you print.

I bet it's gonna be something like

1: false  3: true  4 false

1: false  2: true  4 true 5: false

At set1, number 2 is missing, at set 2, number 3.
That's because if the character is not '0' and not '1', it will not put anything in your BitSet,
but still up the counter i and put the default value of a boolean there-> false.

#4
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts

wim DC said:

I think it will all become a lot more clear if you do

                        if (strLine.charAt(i) == '1') {

                            result1.set(i, true);

                            System.out.print([B][SIZE="4"][COLOR="purple"]i +": " +[/COLOR][/SIZE][/B] result1.get(i) + "  ");

                        } else if (strLine.charAt(i) == '0') {

                            result1.set(i, false);

                            System.out.print([B][COLOR="purple"][SIZE="4"]i +": " +[/SIZE][/COLOR] [/B]result1.get(i) + "  ");

                        }


So just add i + ": " to everything you print.

I bet it's gonna be something like

1: false  3: true  4 false

1: false  2: true  4 true 5: false

At set1, number 2 is missing, at set 2, number 3.
That's because if the character is not '0' and not '1', it will not put anything in your BitSet,
but still up the counter i and put the default value of a boolean there-> false.

Yea .. that because I need to print out just the number only ... In csv file , the number will be 1,1,0 ... that explain why 1 and 3 is missing due to
strLine.replace(",", "");

my problem now I'm facing is it will "AND" the input with the commas and prompt the results with the comma :( ...

#5
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
anyone ? I need this project to be done by fri :( ... anyone can help me ?

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Can you copy paste the contents of the 2 files here (or add as attachment)

#7
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts

wim DC said:

Can you copy paste the contents of the 2 files here (or add as attachment)

Sorry I don't how to attach the files... Here the screenshot ... hope it help :)


Here's the Testing file.

Edited by xxxxjayxxx, 19 April 2011 - 10:52 AM.


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Aha, i found 2 things:
  • With me, using excel 2003 :/ it saves a csv file using a ';' semicolon, to split not a comma. Open the csv file with notepad to see what it really is.
  • You're not catching the String that replaceAll returns:
    
    strLine.replace(",", "");
    
    
    Should be:
    
    strLine = strLine.replace(",", "");
    
    
But possibly with a semicolon instead of a comma.. Dependant on the file (see #1)

#9
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts

wim DC said:

Aha, i found 2 things:
  • With me, using excel 2003 :/ it saves a csv file using a ';' semicolon, to split not a comma. Open the csv file with notepad to see what it really is.
  • You're not catching the String that replaceAll returns:
    
    strLine.replace(",", "");
    
    
    Should be:
    
    strLine = strLine.replace(",", "");
    
    
But possibly with a semicolon instead of a comma.. Dependant on the file (see #1)

WOW >>> THX WIM YOU SOLVE MY PROBLEM ... :)
hmm .. If there's more than one line I need to "AND " with ...
i can't use this correct ?
result1.and(result2);

                    for (int i = 0; i < strLine.length(); i++) {

                        strLine.replace("false", "");

                        System.out.print(result1.get(i) + " ");

                    }

because the result will be store in result1 everytime I "AND" as a result result1 can't be reuse to "AND" with other input ?

#10
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
For multiline, i see 2 options:
  • Just keep on adding to the same BitSet
    A file that looks like:
    
    100
    
    010
    
    
    Will result in the bitset {true, false, false, false, true, false}

    This can be achieved by putting your counter i outside the loops, so it won't reset to 0:
    
    [B][SIZE="4"]int i=0;[/SIZE][/B]
    
    while ((strLine = br1.readLine()) != null) {
    
        for(char character : strLine.toCharArray() ) {
    
            if( character == '1' ){
    
                result1.set(i, true);            
    
            } else if( character == '0' ){
    
                result1.set(i, false);            
    
            }
    
            System.out.print(i + ": " + result1.get(i) + "  ");
    
            [B][SIZE="4"]i++;[/SIZE][/B]
    
        }
    
    }                       
    
                       
    
    
  • Or, you can create an array of BitSets, so with a file that looks like:
    
    100
    
    010
    
    
    You will in the end, have 2 BitSets in an array, each containing 1 line:
    {{true, false, false },
    {false, true, false }}

    This requires some more changing, and I don't wanna write all of this, if in the end you choose for option 1 anyway ^^
    Just say which you prefer, and we'll see.


#11
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
Nvm ... I think I choose step 1...
Here the code I had modify with your help:

Isit like this for step 1 ?

Edited by xxxxjayxxx, 19 April 2011 - 10:53 AM.


#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, a regular array would be created like so:

BitSet[] bitSets = new BitSet[10]

This creates an array of 10 BitSets. Now, because we don't know how many lines there will be, a normal array is not really the best solution here.
A very common alternative is the ArrayList. It's used a lot, but in this case I think the LinkedList is better (Google ArrayList vs LinkedList to find out why, I could be wrong tho. But in this case, the performance impact isn't important or noticable anyway).
Both of them are dynamic arrays, meaning their size can be changed, while a normal array []has a fixed size.

A LinkedList of BitSets is created by doing:

LinkedList<BitSet> bitSets = new LinkedList<BitSet>();

You can add to it by doing:

BitSets.add( new BitSet() );

And take out of it by doing

BitSet setNumber0 = BitSets.get(0);


In your code, you will get:

    [B][SIZE="4"]LinkedList<BitSet>[/SIZE][/B] result1 = new [SIZE="4"][B]LinkedList<BitSet>();[/B][/SIZE]

    [SIZE="4"][B]LinkedList<BitSet>[/B][/SIZE] result2 = new [B][SIZE="4"]LinkedList<BitSet>();[/SIZE][/B] 


Now, when you're looping trough the file, line per line using the while-loop. You gonna need 1 new BitSet every loop.
And at the end of every loop, you store it in the list:


while ((strLine = br1.readLine()) != null) {

    BitSet bitSet = new BitSet();


    //In here, keep your code, but use "bitSet" instead of "result1"


   result1.add(bitSet);

}

Next, you have this piece of code:

while ((strLine = br2.readLine()) != null) {

                    System.out.println();

                    strLine.replace(",", "");//.substring(strLine.indexOf(".")+5);

                    if (strLine.trim().startsWith("Name")) {

                        continue;

                    }

                    for (int i = 0; i < strLine.length(); i++) {

                        if (strLine.charAt(i) == '1') {

                            result2.set(i, true);

                            System.out.print(result2.get(i) + " ");

                        } else if (strLine.charAt(i) == '0') {

                            result2.set(i, false);

                            System.out.print(result2.get(i) + " ");

                        }

                    }



                    [B][COLOR="red"][SIZE="4"]System.out.println();

                    result1.and(result2);

                    for (int i = 0; i < strLine.length(); i++) {

                        strLine.replace("false", "");

                        System.out.print(result1.get(i) + " ");

                    }[/SIZE][/COLOR][/B]

                }

I would suggest to not do the "AND" in there, but do it outside the loop. Just store it in a new BitSet, and add this bitSet to result2, like it's done for result1 in the code above.
So cute/delete the red part.

At this moment, you should have 2 lists, 1 for each file, each containing 1 BitSet per file.
Now you must do the "AND" operation. This is done by looping trough the both lists, take 1 BitSet out of it and do "AND"


for( int i=0 ; i<result1.size() ; i++){

    BitSet bitSet1 = result1.get( i );

    BitSet bitSet2 = result2.get( i );


   bitSet1.and(bitSet2);


   System.out.println();

   for(int j=0 ; j<bitSet1.size() ; j++){

       System.out.print( bitSet1.get( j ) + " ");

   }

}

And that should be about it.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users