Results 1 to 12 of 12

Thread: easy question

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    This should be an easy question for some of you java gurus to answer.

    Im trying to add a binarySearch into some existing
    code that i had but i keep getting a cannot resolve symbol error.
    ie... method binarySearch (byte[],byte[])
    I want to read a text file which is then stored into an
    array. Then run a binarySearch on that array based on
    the string value being passed into the class.. ie teststring

    the signature for the binarySearch method i want to use
    is public static int binarySearch(byte[] a, byte key);

    what is a key???

    import java.io.*;
    import java.util.Arrays;

    public static void test(String teststring){

    try {
    FileInputStream fin = new FileInputStream("C:\\test.txt");

    byte[] buffer = new byte[64];

    while(true){
    int bytesread = fin.read(buffer);
    if(bytesread == -1) break;
    }

    Arrays.sort(buffer); // first sort the array;
    byte[] hold = teststring.getBytes();
    Arrays.binarySearch(buffer,hold);

    }

  2. #2
    Guest

    Thumbs up

    Like the javac compiler, I didn't find a method that takes two byte arrays as arguments to the binarySearch method.

    Watch the datatypes.

    Note
    I created a text file with just chars
    "weojvewdvqwhellosfdvwqqwvlkjbnvv
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"

    and if I had less than 64 chars, I'd get zeroes at the head of "buffer" due to the sort. I added z's to put them at the end after a sort. But you will still get the index of the sorted chars (including carriage return CR and line feed LF-- 10, 13).
    So when I passed in a byte storing the value 100, it had an index of 3 due to
    10, 13, 98, 100
    CR, LF, b, d, etc.

    I saw this after outputting "buffer" in a for loop.
    "T" was the name of my class.
    main had:
    T t = new T();
    byte b = 100;// "d" is 100 as a byte.
    t.test(b);
    test(byte testByte) had:
    int i = Arrays.binarySearch(buffer, testByte);
    System.out.println("Index is : " + i);

    The docs said this:
    "If the array contains multiple elements with the specified value, there is no guarantee which one will be found."

    although my tests found the first one.

    Make sure the methods in the API do what you intended them to do.

    Maybe you want
    java.lang.String
    and the "substring" methods.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    The problem im having is that how do you go about
    comparing what is in a byte array to a String?
    The read() method for java.io.FileInputStream takes
    a byte array which is fine but then after that im not too
    sure how to test for equality.

    while(true){
    int bytesread = fin.read(buffer); // read the .txt file and store in an array
    if(bytesread == -1) break;
    }

    for(int i = 0; i < buffer.length; i++){
    char p = (char)buffer[i];
    if(p.equals(teststring)){
    System.out.println("teststring");
    }

    }

  4. #4

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    I tried this and it seems to work but say for instance
    the .txt file contains these words

    fish
    apple
    Orange
    harry
    Billy
    Mike
    Richard
    Brandon

    if i pass any of these on the command line only the first
    word(fish) is found.


    byte[] buffer = new byte[64];

    while(true){
    int bytesread = fin.read(buffer); // read the .txt file and store in an array
    if(bytesread == -1) break;
    }
    for(int i = 1; i <= buffer.length; i++){
    String p = new String(buffer,0,i);
    if(p.equals(args[0])){
    System.out.println("Password found!!!");
    }
    }

    i tried to change the offset String(buffer,i-1,i);
    which makes sense to me but i keep getting an error

  5. #5
    Guest

    Lightbulb

    Maybe your design should go like this:

    java.io.BufferedReader
    BufferedReader in = new BufferedReader(new FileReader("test.txt"));

    BufferedReader method: String readLine()
    in.readLine();

    java.lang.String
    various methods

    I don't know about efficiency since some techniques can read an entire file in one call.

  6. #6

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    This looks like the way to go but i cant figure out
    why the equals() method doesnt seem to recognize
    that the two strings are equal.

    If i run the code with System.out.println(s + password)
    it prints
    apple
    orange
    fish
    Brandon Brandon

    that would be a match right???




    public class test{
    public static void main(String[] args){

    Locator("Brandon");

    } // end main

    public static void keyLocator(String password){
    try {
    FileInputStream fin = new FileInputStream("C:\\Java\\passwords.txt");


    byte[] hold = new byte[100];

    while(true){
    int bytesread = fin.read(hold);
    String s = new String(hold);
    if(bytesread == -1) break;
    if(s.equals(password)){
    System.out.print("Found!");
    }
    }

    }catch(Exception e){System.err.println(e);}
    } // end method
    } // end class

  7. #7
    Guest
    On inspection, I think the length (or size) of hold is 100, and s is also 100 and password may be 7.

    I didn't try your code, but is this a typo?
    Locator("Brandon");

    public static void keyLocator(String password)

    due to key.

    Try a String[] as the "hold" variable and use readLine() to get each element of the hold array to be a String.

  8. #8

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Hey thanks!! i fixed it and i seems to work. But do you
    know how to end looping when the end of an array is reached??? Sort of like UBound in Visual Basic. I tried this but i dosnt compile.


    for(int i = 0; i <= myarray.length(); i++){
    String k = buff.readLine();
    if(k.equals(password)){
    System.out.println("Password found!!!" + password);
    }
    }




    import java.io.*;
    import java.util.Arrays;

    public class keyVault{
    public static void main(String[] args){

    keyLocator("Brandon");

    } //end main

    public static void keyLocator(String password){
    try {
    BufferedReader buff = new BufferedReader(new FileReader("C:\\Java\\passwords.txt"));


    for(int i = 0; i <= 7; i++){
    String k = buff.readLine();
    if(k.equals(password)){
    System.out.println("Password found!!!" + password);
    }
    }
    }catch(Exception e){System.err.println(e);}
    } // end method
    } // end class

  9. #9
    Guest

    Thumbs up Here are some ideas

    But this is just a test of techniques rather than true security. I'm sure you wouldn't be printing out the list of passwords in a final product.

    //Test.java
    import java.io.*;
    import java.util.Vector;
    import java.util.Enumeration;

    public class Test{
    private String theFileName;
    private String aPassword;
    private String thePassword = "Mike";// Also try ["Mike ";] and it won't be found.
    private int lineCount;
    private BufferedReader in;
    private boolean found = false;
    private Vector keyVault;

    public Test(String s){
    theFileName = new String(s);
    lineCount = 0;
    keyVault = new Vector(10,5);//Hold 10 objects, then makes room for 5 if more whenever filled
    }

    public void testIt(){
    try{
    in = new BufferedReader(
    new FileReader(
    theFileName
    )
    );
    }catch(FileNotFoundException fnfe){
    System.err.println("testIt: " + theFileName + " isn't a file since...");
    System.err.println(fnfe);
    System.err.println("testIt: Exiting with error code 1!");
    System.exit(1);
    }
    try{
    while((aPassword = in.readLine()) != null){
    System.out.println("Test read '" + aPassword + "' from " + theFileName);
    if(aPassword.equals(thePassword)){
    found = true;
    }
    lineCount++;
    keyVault.addElement(aPassword);
    }
    in.close();
    }catch(IOException ioe){
    System.err.println(ioe);
    System.err.println("testIt: Exiting with error code 2!");
    System.exit(2);
    }
    if(found)
    System.out.println("Password Found!!!");
    else
    System.out.println("Password Not Found!!!");
    }
    public void keyLocator(Vector v){
    boolean foundLate = false;
    System.out.println("keyVault contains " + keyVault.size() + " out of " +

    keyVault.capacity() + " available capacity");
    for(Enumeration e = v.elements(); e.hasMoreElements(); ){
    String tempPassword = (String)e.nextElement();
    if(tempPassword.equals(thePassword)){
    foundLate = true;
    }
    }
    if(foundLate)
    System.out.println("USING LATER PROCESSING: Password Found!!!");
    else
    System.out.println("USING LATER PROCESSING: Password Not Found!!!");

    if(found == foundLate)
    System.out.println("Early and Late techniques agree.");
    else
    System.out.println("Early and Late techniques DISAGREE -- LOGIC ERROR !!!");
    }
    public static void main(String[] args){
    Test t = new Test(args[0]);
    System.out.println("main: Does not know how many lines " + t.theFileName + " contains.");
    System.out.println("main: Does not know if " + t.theFileName + " is a valid file yet.");
    System.out.println("main: " + t.theFileName + " is only a 'String' at this point.");
    t.testIt();
    System.out.println("main: Counted " + t.lineCount + " line(s) in file " + t.theFileName +

    ".");
    System.out.println("main: I might use java.util.Vector\nmain: if I wanted to hold the data

    in a buffer for later processing.");
    System.out.println("main: method Vector.size contains the number of components in a

    Vector\nmain: even if the Vector can hold more components (UBoundISH).");
    t.keyLocator(t.keyVault);
    }
    }

    Invoke with "java Test test.txt"
    or replace "test.txt" with any file

    I used your list of names in a file called test.txt.

  10. #10
    Guest

    Thumbs up

    The previous did cut and paste good. Here is the attached file.

    Rename to Test.java

    Invoke with "java Test test.txt"
    or replace "test.txt" with any file.
    Attached Files Attached Files

  11. #11

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Woah!!!! you didnt have to go through that much trouble!! but it's much appreciated. Thanks.....

    I might as well as you a few questions since you took the time to code....

    In the public class test you have private String theFileName.... and in the main you are accessing it
    with t.theFileName?

    I thought if a member of a class
    is declared private the member is never accessible
    except within the class itself.

    also would it be correct to say that Enumartion allows
    you to loop through the Vector to access it's elements?

    for(Enumeration e = v.elements(); e.hasMoreElements(); ){

    Thanks again........

  12. #12
    Guest
    I think I put a comment (in the download version) after the single class imports just to let you know where the Vector and Enumeration classes came from. Cut and Paste was not being my friend today.

    I thought if a member of a class
    is declared private the member is never accessible
    except within the class itself.
    This is true, but I am calling it from within the class it is a member of ("within the class itself"). There is only one class in this example; class Test. BTW, you might also want to make it a final class if you are playing around with security and use "getters" and "setters" (accessors) for the private members.

    also would it be correct to say that Enumartion allows
    you to loop through the Vector to access it's elements?
    That's a definite, "YES".

    You are welcome.

    In the constructor, I was going to test if the string for the filename was really a valid file, but I was already deviating from your original question.

    I also forgot to point out that this is important code to avoid an infinite loop and exit after the end of the file has been read.
    while((aPassword = in.readLine()) != null){

    I got that technique from the (Networking) Socket examples at Sun http://java.sun.com .

    java.io.BufferedReader
    readLine
    public String readLine()
    throws IOException
    Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
    Returns:
    A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
    Throws:
    IOException - If an I/O error occurs

Posting Permissions

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



Click Here to Expand Forum to Full Width