Results 1 to 18 of 18
Thread: Passing Strings ?
- 04-23-2009, 12:46 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
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
- 04-23-2009, 01:01 AM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 04-23-2009, 01:04 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
Point noted, wondered where that smiley appeared from.
- 04-23-2009, 01:09 AM #4
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
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; } }
- 04-23-2009, 01:09 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 01: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!
- 04-23-2009, 01:10 AM #6
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
lol thats better.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; } }
- 04-23-2009, 01:13 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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 01: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!
- 04-23-2009, 01:16 AM #8
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 04-23-2009, 01:16 AM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
i hate when im slow to reply...
- 04-23-2009, 01:17 AM #10
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
no problem, sorry, new to the whole forum thing. Want me to indent all the code to make it easier to read ?
- 04-23-2009, 01:19 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 04-23-2009, 01:20 AM #12
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
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.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; } }
- 04-23-2009, 01:25 AM #13
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 04-23-2009, 01:26 AM #14
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
Hmmmm, just realised the constructor for the BST class reads as follows :
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 ?Java Code://fields BSTNode root; //constructor BST() { root = null; }
- 04-23-2009, 01:28 AM #15
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 04-23-2009, 01:40 AM #16
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
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; } }
-
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:
I'm not sure if you'd want to show the password or pin number in the toString method or not. Up to you.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? } }
- 04-24-2009, 05:03 AM #18
Member
- Join Date
- Apr 2008
- Posts
- 11
- Rep Power
- 0
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
-
Subtracting Strings
By lazygun247 in forum New To JavaReplies: 6Last Post: 03-29-2009, 10:05 PM -
comparing strings
By diggitydoggz in forum New To JavaReplies: 7Last Post: 12-23-2008, 04:40 AM -
Comparison of Strings
By Cero.Uno in forum New To JavaReplies: 3Last Post: 02-11-2008, 02:46 AM -
reversing Strings
By Java Tip in forum Java TipReplies: 0Last Post: 11-11-2007, 08:24 PM -
how to compare two strings
By elizabeth in forum New To JavaReplies: 7Last Post: 08-06-2007, 03:57 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks