Perplexing problem with inserting into a linkedlist object

A

Anonymous Sender

O great java gurus I conjure thee from the abyss
in the hopes that your combined wisdom can figure out this
problem.

I'm trying to build a list of lists; each object of class Bomlist
is an object of class Assynode, which in turn consists of a string
and two LinkedLists.

But I'm having trouble just populating Bomlist - this program executes
fine when Collections.binarySearch is called only once (for "B"). But
when lines 46-55 are uncommented so that "A" is attempted to be inserted
the program fails.

In the real app, inLine will be supplied by reading from a file.


TIA,

Steve







//----Bomlist.java-------------------------------------------------------
// Bomlist application - not an applet
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

public class Bomlist {
private LinkedList bom_list;

public Bomlist() {
System.out.println("inside Bomlist constructor");
bom_list = new LinkedList();
readRecords();
printList();
}

private void readRecords() {
int indx_cur;
int indx_nha;
int indx_tmp;
System.out.println("inside readRecords");

StringTokenizer tokenizer;
Vector rows = new Vector(3);

int num_tokens = 0, ii =0;
String inLine, aaa, bbb, ccc;
inLine = "B;A;A001";
String strarry[] = new String[3]; // edgelist only contains three fields

tokenizer = new StringTokenizer( inLine,";");
num_tokens = tokenizer.countTokens();
for( ii = 0; ii < num_tokens ; ii++ ) { //loop to get all the elements of line
strarry[ii] = tokenizer.nextToken();
}
//find current and insert if nessary
indx_cur = Collections.binarySearch(bom_list, strarry[0]);
System.out.println(strarry[0] + " -- indx_cur=" + indx_cur);
System.out.println("got to 1");
if( indx_cur < 0 ) {
System.out.println("got to 2a");
Assynode curnode = new Assynode( strarry[0]);
insertOrdered(curnode);
}
// why does the program fail when this section is included???
/*
indx_nha = Collections.binarySearch(bom_list, strarry[1]);
System.out.println(strarry[1] + " -- indx_nha=" + indx_nha);
System.out.println("got to 3");
if( indx_nha < 0 ) {
System.out.println("got to 3a");
Assynode nhanode = new Assynode( strarry[1]);
insertOrdered(nhanode);
}
*/
}

public void printList() {
String build_str = "";
System.out.println("should be in printList");
ListIterator lili = bom_list.listIterator();
while (lili.hasNext()) {
Assynode current = (Assynode) lili.next();
current.printAssy();
}
}

public void insertOrdered( Object newobj) {
Assynode screw = (Assynode) newobj;
System.out.println("got inside insertORdered for part - " + screw.partno);
ListIterator li = bom_list.listIterator(); // sets pointer at BEGINNING of list
while (li.hasNext()) {
Assynode current = (Assynode) li.next();
if( current.compareTo(newobj) > 0 ) {
li.previous();
break;
}
}
li.add( newobj);
}

public static void main( String args[] ) {
System.out.println("inside main");
final Bomlist bom = new Bomlist();
System.exit( 0 );
}
}

class Assynode implements Comparable {
public String partno;
public LinkedList nha_list;
public LinkedList sub_list;

public Assynode( String aa) {
System.out.println(" inside default Assynode constructor");
partno = aa;
LinkedList nha_list = new LinkedList();
LinkedList sub_list = new LinkedList();
}

public Assynode( String aa, String bb, String cc) {
partno = aa;
LinkedList nha_list = new LinkedList();
nha_list.add( bb);
LinkedList sub_list = new LinkedList();
sub_list.add( cc);
}

public int compareTo( Object other) {
System.out.println(" inside custom Assynode compareTo");
Assynode ddd = (Assynode) other; // <-- fails here on SECOND call!!
System.out.println(" got to compareTo-a");
int compare = this.partno.compareTo( ddd.partno);
System.out.println(" got to compareTo-b");
String outmsg = " " + this.partno + " compared to " + ddd.partno + " returns " + compare;
System.out.println(outmsg);
return compare;
// the returned int is negative if this.partno is less than other.partno
// the returned int is zero if this.partno is equal to other.partno
// the returned int is positive if this.partno is more than other.partno
}

public boolean equals( Object other) {
Assynode fff = (Assynode) other;
boolean isequal = this.partno.equals( fff.partno);
return isequal;
}

public void printAssy() {
System.out.println("----------------");
System.out.println(partno);
}
}
//----end of Bomlist.java-------------------------------------------------------
 
R

Ryan Stewart

Anonymous Sender said:
O great java gurus I conjure thee from the abyss
in the hopes that your combined wisdom can figure out this
problem.

I'm trying to build a list of lists; each object of class Bomlist
is an object of class Assynode, which in turn consists of a string
and two LinkedLists.

But I'm having trouble just populating Bomlist - this program executes
fine when Collections.binarySearch is called only once (for "B"). But
when lines 46-55 are uncommented so that "A" is attempted to be inserted
the program fails.

In the real app, inLine will be supplied by reading from a file.


TIA,

Steve

//----Bomlist.java-------------------------------------------------------
// Bomlist application - not an applet *snip*
public class Bomlist {
I'm not sure what a Bomlist is, but each new word in a class name should
start with a capital letter.
private LinkedList bom_list;
Variable names should not have underscores. As with class names, each new
word should begin with a capital letter.
public Bomlist() {
System.out.println("inside Bomlist constructor");
bom_list = new LinkedList();
readRecords();
printList();
}
This is way too much to do in the constructor. A constructor simply has to
provide you with an object that's in a valid state.
private void readRecords() {
int indx_cur;
int indx_nha;
int indx_tmp;
Descriptive variable names are to be preferred.

*snip*
StringTokenizer tokenizer;
StringTokenizer is discouraged from use. Check out the java.util.regex
package instead.
Vector rows = new Vector(3);

int num_tokens = 0, ii =0;
You declare ii here but only use it within the for loop below. It's a good
practice to make variable scope as limited as possible. The following should
be preferred:
for (int ii = 0; ...
String inLine, aaa, bbb, ccc;
I assume you intend to use those last three for something? They're also your
least descriptive variable names.

*snip*
num_tokens = tokenizer.countTokens();
Why initialize this to zero just to reassign it: int num_tokens =
tokenizer.countTokens(); (Just a nitpick, but a slight performance hit.)
for( ii = 0; ii < num_tokens ; ii++ ) { //loop to get all
the elements of line
Here you start including a space after some of your open parentheses. Code
conventions dictate a space between a keyword and an open parenthesis but
not after the parenthesis.

*snip*
// why does the program fail when this section is included???
Because you're doing a binary search for a String in a List of Assynodes.
/*
indx_nha = Collections.binarySearch(bom_list, strarry[1]); *snip*
*/

public void printList() {
String build_str = "";
Another unused variable.

*snip*
public static void main( String args[] ) {
System.out.println("inside main");
final Bomlist bom = new Bomlist();
System.exit( 0 );
The spacing thing again, and what's the purpose of this? Without it, the
next step is program termination anyway.

*snip bad variable names*
public int compareTo( Object other) {
System.out.println(" inside custom Assynode compareTo");
Assynode ddd = (Assynode) other; // <-- fails here on SECOND
call!!
No surprise. Because of what I mentioned above, you're trying to cast a
String to an Assynode.

*snip*
public boolean equals( Object other) {
Assynode fff = (Assynode) other;
boolean isequal = this.partno.equals( fff.partno);
return isequal;
}
Another bug that may rear its head. What happens on assyNode.equals("X")?
Another ClassCastException because you don't check the object's class before
attempting to cast.
 
P

privacy.at Anonymous Remailer

*major snippage of irrelevant style comments*
// why does the program fail when this section is included???
Because you're doing a binary search for a String in a List of Assynodes.

indx_nha = Collections.binarySearch(bom_list, strarry[1]);
indx_nha = Collections.binarySearch(bom_list, strarry[1]);

Why does the line
indx_cur = Collections.binarySearch(bom_list, strarry[0]);
pass a String to compareTo but
indx_nha = Collections.binarySearch(bom_list, strarry[1]);
passes an Assynode?


Steve
 
A

Andrew Thompson

*major snippage of irrelevant style comments*

Those 'irrelevant' styles are what might
make the difference between someone else
looking at your problem, or not.

So, how relevant is your problem?
 
R

Ryan Stewart

privacy.at Anonymous Remailer said:
*major snippage of irrelevant style comments*
As Andrew said, they're hardly irrelevant, and not all were style comments.
// why does the program fail when this section is included???
Because you're doing a binary search for a String in a List of Assynodes.

indx_nha = Collections.binarySearch(bom_list, strarry[1]);
indx_nha = Collections.binarySearch(bom_list,
strarry[1]);

Why does the line
indx_cur = Collections.binarySearch(bom_list, strarry[0]);
pass a String to compareTo but
indx_nha = Collections.binarySearch(bom_list, strarry[1]);
passes an Assynode?
What makes you think this is the case? strarry is an array of Strings. It
doesn't matter which element you reference. You're always going to get a
String.
 
M

Max Mustermann

*major snippage of irrelevant style comments*
Those 'irrelevant' styles are what might
make the difference between someone else
looking at your problem, or not.

So, how relevant is your problem?

To me, very, but I try to stay focused on the issue at hand.
First I need to make it work, then I can worry about making
it pretty.
To you, I suspect not at all. You needn't spend any more time
on it.


Thanks anyway,

Steve
 
A

Anonymous Sender

Why does the line
indx_cur = Collections.binarySearch(bom_list, strarry[0]);
pass a String to compareTo but
indx_nha = Collections.binarySearch(bom_list, strarry[1]);
passes an Assynode?

What makes you think this is the case? strarry is an array of Strings. It
doesn't matter which element you reference. You're always going to get a
String.

True, but in each case I pass only one element of the String array, and that
element should itself be a String. with indx_cur it is but with indx_nha it
somehow becomes not !?!

I've improved (?) the code somewhat by building a Comparator and explicitly
calling it within the binarySearch parameters. The compare function within
should make explicitly clear that I'm trying to compare the value of the partno
String field of an Assynode object with a String plucked from strarry. Lots of
print statements show that the first time through that oth_1 is an Assynode
and oth_2 is a String, but the second time through one gets transmorgified
somehow.


TIA,

Steve

----Bomlist.java---------------------------------------
// classes for mybox
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;

public class Bomlist {
private LinkedList bom_list;

public Bomlist() {
System.out.println("inside Bomlist constructor");
bom_list = new LinkedList();
readRecords();
printList();
}

private void readRecords() {
int indx_cur;
int indx_nha;
int indx_tmp;
System.out.println("inside readRecords");

StringTokenizer tokenizer;
Vector rows = new Vector(3);

Comparator comparator = new assyComparator();
int num_tokens = 0, ii =0;
String inLine, aaa, bbb, ccc;
inLine = "B;A;A001";
String strarry[] = new String[3]; // edgelist only contains three fields

tokenizer = new StringTokenizer( inLine,";");
num_tokens = tokenizer.countTokens();
for( ii = 0; ii < num_tokens ; ii++ ) { //loop to get all the elements of line
strarry[ii] = tokenizer.nextToken();
}
//find current and insert if nessary
indx_cur = Collections.binarySearch(bom_list, strarry[0], comparator);
System.out.println(strarry[0] + " -- indx_cur=" + indx_cur);
System.out.println("got to 1");
if( indx_cur < 0 ) {
System.out.println("got to 2a");
Assynode curnode = new Assynode( strarry[0]);
insertOrdered(curnode);
}
// why does the program fail when this section is included???
indx_nha = Collections.binarySearch(bom_list, strarry[1], comparator);
System.out.println(strarry[1] + " -- indx_nha=" + indx_nha);
System.out.println("got to 3");
if( indx_nha < 0 ) {
System.out.println("got to 3a");
Assynode nhanode = new Assynode( strarry[1]);
insertOrdered(nhanode);
}
}

public void printList() {
String build_str = "";
System.out.println("should be in printList");
ListIterator lili = bom_list.listIterator();
while (lili.hasNext()) {
Assynode current = (Assynode) lili.next();
current.printAssy();
}
}

public void insertOrdered( Object newobj) {
System.out.println("got inside insertOrdered");
if ( newobj instanceof Assynode)
System.out.println(" newobj IS an instance of Assynode");
else
System.out.println(" newobj is not an instance of Assynode");
if ( newobj instanceof String) {
System.out.println(" newobj IS an instance of String");
System.out.println(" newobj = " + newobj);
}
else
System.out.println(" newobj is not an instance of String");
Assynode screw = (Assynode) newobj;
System.out.println(" got inside insertOrdered for part - " + screw.partno);
ListIterator li = bom_list.listIterator(); // sets pointer at BEGINNING of list
System.out.println(" got past li creation");
while (li.hasNext()) {
Assynode current = (Assynode) li.next();
if( current.compareTo(newobj) > 0 ) {
li.previous();
break;
}
}
li.add( newobj);
}

public static void main( String args[] ) {
System.out.println("inside main");
final Bomlist bom = new Bomlist();
System.exit( 0 );
}
}

class Assynode implements Comparable {
public String partno;
public LinkedList nha_list;
public LinkedList sub_list;

public Assynode( String aa) {
System.out.println(" inside default Assynode constructor");
partno = aa;
LinkedList nha_list = new LinkedList();
LinkedList sub_list = new LinkedList();
}

public Assynode( String aa, String bb, String cc) {
partno = aa;
LinkedList nha_list = new LinkedList();
nha_list.add( bb);
LinkedList sub_list = new LinkedList();
sub_list.add( cc);
}

public String getPartno() { return partno;}

public int compareTo(Object other) {
return (defaultComparator.compare(this, other));
}
/*
public boolean equals(Object other) {
return this.getPartno().equals((Assynode)other.getPartno() );
}
*/
// Comparator
private Comparator defaultComparator = new assyComparator();

public void printAssy() {
System.out.println("----------------");
System.out.println(partno);
}
}

class assyComparator implements Comparator {
public int compare(Object oth_1, Object oth_2) {
System.out.println(" got inside assyComparator");
if ( oth_1 instanceof Assynode)
System.out.println(" oth_1 IS an instance of Assynode");
else
System.out.println(" oth_1 is not an instance of Assynode");
if ( oth_1 instanceof String) {
System.out.println(" oth_1 IS an instance of String");
System.out.println(" oth_1 = " + oth_1);
}
else
System.out.println(" oth_1 is not an instance of String");
if ( oth_2 instanceof Assynode)
System.out.println(" oth_2 IS an instance of Assynode");
else
System.out.println(" oth_2 is not an instance of Assynode");
if ( oth_2 instanceof String) {
System.out.println(" oth_2 IS an instance of String");
System.out.println(" oth_2 = " + oth_2);
}
else
System.out.println(" oth_2 is not an instance of String");
return ((Assynode)oth_1).getPartno().compareTo((String)oth_2);
// the returned int is negative if oht_1.partno is less than oth_2.partno
// the returned int is zero if oht_1.partno is equal to oth_2.partno
// the returned int is positive if oht_1.partno is more than oth_2.partno
}
}
----end of Bomlist.java---------------------------------------
 
A

Andrew Thompson

To me, very, but I try to stay focused on the issue at hand.
First I need to make it work,

To achieve such you came to usenet,
which is pretty pointless if no one
reads the code.
...then I can worry about making
it pretty.

First it is 'irrelevant styles', now it
is 'making it pretty'. You are obviously
not paying attention, which will hinder
your chances of getting an answer.
To you, I suspect not at all. You needn't spend any more time
on it.

Don't worry about that Steve, ..Max,
...Anonymous Remailer, *whatever* you
are calling yourself today..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top