Results 1 to 18 of 18
  1. #1
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Unhappy Passing Strings ?

    Hey guys, just had a niggling little problem with passing strings around between objects, a condensed example follows :


    public class BST{

    public Pairs find(String thisName){

    int direction = 0;
    BSTNode current = root;

    for(;;){

    if(current == null)
    return null;

    System.out.println(current.data.username);

    direction = thisName.compareTo(current.data.username);

    if (direction == 0)
    return current.data;

    else if(direction < 0)
    current = current.left;

    else
    current = current.right;


    }
    }
    }


    ************************************************** ****


    public class Pairs implements Comparable<Pairs>{

    public String username;
    public String password;
    public int pin;

    public Pairs(String uname, String pword, int pinno)
    {
    username = uname;
    password = pword;
    pin = pinno;
    }

    public int compareTo(Pairs x){
    return 2;
    }
    public String returnuname(){
    return username;
    }

    public String returnpword(){
    return password;
    }

    public int returnpin(){
    return pin;
    }

    }

    So, trying to write a small app that uses a BST to store/find username/password pairs, represented by a 'Pairs' object. The find method finds the correct object in the tree by comparing the search criteria string with the username of each object. However, my little diagnostic system.out.println gives [C@c7e553 . I keep encountering this elsewhere in my code, like when I am trying to print elements from a list of Pairs. What am I doing wrong. Fully prepared for a newbie beatdown here folks, so let rip !!:D

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    USE CODE TAGS... you very efficiently made the for loop in your first class unreadable, and at the same time, made it impossible for anyone else to compile your code so they could view the results. Code tags are: (code)(/code) with the brackets replaced with square brackets
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    Point noted, wondered where that smiley appeared from.

  4. #4
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    public class BST{
    
    public Pairs find(String thisName){
    
    int direction = 0;
    BSTNode current = root;
    
    for(;{
    
    if(current == null)
    return null;
    
    System.out.println(current.data.username);
    
    direction = thisName.compareTo(current.data.username);
    
    if (direction == 0)
    return current.data;
    
    else if(direction < 0)
    current = current.left;
    
    else
    current = current.right;
    
    
    }
    }
    }
    
    
    ************************************************** ****
    
    
    public class Pairs implements Comparable<Pairs>{
    
    public String username;
    public String password;
    public int pin;
    
    public Pairs(String uname, String pword, int pinno)
    {
    username = uname;
    password = pword;
    pin = pinno;
    }
    
    public int compareTo(Pairs x){
    return 2;
    }
    public String returnuname(){
    return username;
    }
    
    public String returnpword(){
    return password;
    }
    
    public int returnpin(){
    return pin;
    }
    
    }

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    I still cannot read the for loop... it does not appear to have any statements at all
    Last edited by Singing Boyo; 04-23-2009 at 02:12 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  6. #6
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    public class BST{
    
    public Pairs find(String thisName){
    
    int direction = 0;
    BSTNode current = root;
    
    for(;;){
    
    if(current == null)
    return null;
    
    System.out.println(current.data.username);
    
    direction = thisName.compareTo(current.data.username);
    
    if (direction == 0)
    return current.data;
    
    else if(direction < 0)
    current = current.left;
    
    else
    current = current.right;
    
    
    }
    }
    }
    
    
    ************************************************** ****
    
    
    public class Pairs implements Comparable<Pairs>{
    
    public String username;
    public String password;
    public int pin;
    
    public Pairs(String uname, String pword, int pinno)
    {
    username = uname;
    password = pword;
    pin = pinno;
    }
    
    public int compareTo(Pairs x){
    return 2;
    }
    public String returnuname(){
    return username;
    }
    
    public String returnpword(){
    return password;
    }
    
    public int returnpin(){
    return pin;
    }
    
    }
    lol thats better.

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    no need to repost the code time and again... you can simply edit your posts. code does not compile... to many resources I do not have access to. As for your problems with the print statement, I can't help without the code for BSTNode
    Last edited by Singing Boyo; 04-23-2009 at 02:16 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    you coulda just gone back and modified your original post. anyways, your print statement is printing an address, most likely because what you are passing in is not a string. but, it's hard to tell when you don't shows us what exactly that (BSTNode...) is

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    i hate when im slow to reply...

  10. #10
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    no problem, sorry, new to the whole forum thing. Want me to indent all the code to make it easier to read ?

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    its readable, however, using multiple sets of code tags, one for each class, would make it easier. no need to go edit your posts though. We do need to see the code for BSTNode to help you
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    public class BSTNode {
    
    	    Pairs  data;
    	    BSTNode left;
    	    BSTNode right; 
    
    	    public BSTNode (Pairs x, BSTNode l, BSTNode r){
    	    	data = x; left = l; right = r;
    	    	}
    	    
    	    public BSTNode (Pairs x){
    	    	data = x; left = null; right = null;
    	    	}
    	    
    }
    This is the BSTNode class. The data element is a Pairs object, so I'm trying to print the username field of the Pairs object in the 'data' field.

  13. #13
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    One final problem... we do not have access to root. (the object you use in BSTNode current = root; within class BST)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  14. #14
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    Hmmmm, just realised the constructor for the BST class reads as follows :

    Java Code:
    //fields
     BSTNode root;
    
    //constructor
     BST() {
        root = null;
      }
    I'm assuming this means that if the tree is empty, I am at the root, and the root is set to null, so I'm trying to output a null value, and as a result its giving me the address of 'root' . Is this a possiblity ?

  15. #15
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    That would most likely be (at least part of) what is causing your problem
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  16. #16
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default

    No, don't think thats it, because when the system.out is executed at runtime, a single Pairs object has already been entered into the tree, and should now replace 'null' as the value for the 'root' field. The Pairs object is inserted using the following insert method in the BST class ....
    Java Code:
    public void insert(Pairs thisPair) {
    
          int direction = 0;
          BSTNode parent = null;
          BSTNode current = root;
    
          for(;;){
              if(current == null){
    
                  BSTNode ins = new BSTNode(thisPair);
    
                  if(root == null)
                   root = ins;
                  
                      else if (direction < 0)
                       parent.left = ins;
                  
                       else
                        parent.right = ins;
                         return;
              }
    
              direction = thisPair.compareTo(current.data);
    
                  if(direction == 0)
                      return;
                      parent = current;
    
                    if(direction < 0)
                      current = current.left;
    
                     else
                      current = current.right;
          }
        
      }

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you give your Pairs class a toString method, you will get rid of the funny business that is output in your println statement. Something like:
    Java Code:
    public class Pairs implements Comparable<Pairs>
    {
    
      public String username;
      public String password;
      public int pin;
    
      public Pairs(String uname, String pword, int pinno)
      {
        username = uname;
        password = pword;
        pin = pinno;
      }
    
      public int compareTo(Pairs x)
      {
        return 2;
      }
    
      public String returnuname()
      {
        return username;
      }
    
      public String returnpword()
      {
        return password;
      }
    
      public int returnpin()
      {
        return pin;
      }
      
      @Override
      public String toString()
      {
        return username; // not sure if you want to also output 
                      // password or pin her or not?
      }
    }
    I'm not sure if you'd want to show the password or pin number in the toString method or not. Up to you.

  18. #18
    aldo1987 is offline Member
    Join Date
    Apr 2008
    Posts
    11
    Rep Power
    0

    Default Resolved!

    Hey folks, cheers for all the help (and putting up with forum noobness), managed to resolve my issue. Turns out elsewhere in my code I had been passing x.toString() into a method (where x is a charArray), which I now know does not convert the charArray to a string, but is in fact the object method for returning a string representation of an object. What i should have been doing was : String(char[]x)!! Oh, and Fubarable, I did end up writing a new compareTo method to solve another problem, so cheers for the advice !

Similar Threads

  1. Subtracting Strings
    By lazygun247 in forum New To Java
    Replies: 6
    Last Post: 03-29-2009, 11:05 PM
  2. comparing strings
    By diggitydoggz in forum New To Java
    Replies: 7
    Last Post: 12-23-2008, 05:40 AM
  3. Comparison of Strings
    By Cero.Uno in forum New To Java
    Replies: 3
    Last Post: 02-11-2008, 03:46 AM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 09:24 PM
  5. how to compare two strings
    By elizabeth in forum New To Java
    Replies: 7
    Last Post: 08-06-2007, 04:57 AM

Posting Permissions

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