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-------------------------------------------------------
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-------------------------------------------------------