The Artima Developer Community
Sponsored Link

Java Answers Forum
Searching OBT's

1 reply on 1 page. Most recent reply: May 12, 2004 12:44 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ja Ja Binks

Posts: 14
Nickname: ds99bwood9
Registered: Apr, 2004

Searching OBT's Posted: May 10, 2004 4:23 AM
Reply to this message Reply
Advertisement
I have a OBTComparable and am testing it to find strings, it doesn't work however! Can anyone see where I've gone wrong? Any help would be appreciated, the code is as below.

OBTComparable.java

public class OBTComparable
{
private boolean empty;
private Comparable value;
private OBTComparable left;
private OBTComparable right;

public OBTComparable()
{
setEmpty();
} // constructor

private void setEmpty()
{
empty = true;
value = null;
left = null;
right = null;
} // setEmpty

public boolean isEmpty()
{
return empty;
}

public Comparable getValue()
{
return value;
}

public OBTComparable getLeft()
{
return left;
}

public OBTComparable getRight()
{
return right;
}

private void setValue(Comparable requiredValue)
{
if (empty)
{
empty = false;
left = new OBTComparable();
right = new OBTComparable();
}
value = requiredValue;
} //setValue

public void insert(Comparable insertValue)
{
if (empty)
setValue(insertValue);
else if (insertValue == value)
left.insert(insertValue);
else
right.insert(insertValue);
} // insert

public boolean find(Comparable findValue)
{
if (empty)
return false;
else if (findValue == value)
return left.find(findValue);
else
return right.find(findValue);
} // find
} // class OBTComparable


FindTest.java

import java.util.*;

class FindTest
{
public static void main(String args[])
{
String [] insertStrings = new String [] {"My", "name", "is", "John", "Latham", "and", "my", "hobbies",
"include", "films", "music", "electronics", "and", "computing"};

String [] searchStrings = new String [] {"for latham:", "for films:", "for swimming:",
"for fishing:", "for computing:", "for climbing:", "for paragliding", "for Latham:"};

OBTComparable compareMe = new OBTComparable();

for (int i = 0; i <= 13; i++)
{
compareMe.insert(insertStrings);
System.out.println("Inserting" + " " + insertStrings);
}
for (int i = 0; i <= 7; i++)
{
boolean result = compareMe.find(searchStrings);


System.out.println("Searching" + " " + searchStrings + " " + result);
}
}
}


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Searching OBT's Posted: May 12, 2004 12:44 AM
Reply to this message Reply
Strings (and all other Objects) can't be compared using "==".

You have to use the "equals" method:
firstString.equals(secondString);
It results in a nullPointer-Exception, if firstString == null (obviously).



"==" compares the location of an Object in the System memory, "equals" compares it's content.

Flat View: This topic has 1 reply on 1 page
Topic: applet help Previous Topic   Next Topic Topic: reg. StreamCorruptedException

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use