sorry for the mess. i see what you are trying to tell me.
i'll work a bit on the project, then i'll try and put here a simplified version of the core method, maybe i'll reduce the number of vectors in each Term from 2 to 1 and get rid of other methods putting some variables public for the sake of showing.
another time around i thank you for your patience and your help. given that it's always very difficult to elaborate code written by someone else (and well written code too, while mine is clearly NOT), you have followed me up until now and gave me some very good advices, so i am really grateful because you already helped me a lot making me improve this code even if it still doesn't wor as intended.
--- Update ---
ok so here is something that's totally executable. first the rewritten and simplified class for terms.
import java.util.ArrayList;
import java.util.Collections;
public class DemoTerm {
/**Class DemoTerm
*
* A simplified version of class "Termine". It
* has variables set at "public" to enhance
* readability and reduce calling methods, each
* Term now doesn't have two vectors one for
* synonyms, one for opposites. it has one single
* vector TermList<DemoTerm> called "list"
* that is used alternatively for simulate the two.*/
// INSTANCE VARIABLES
public String name;
public String desc;
public TermList<DemoTerm> list = new TermList<DemoTerm>();;
// CONSTRUCTORS
public DemoTerm() {};
public DemoTerm (String n) {
this.name = n;
}
public DemoTerm (String n, String d) {
this.name = n;
this.desc = d;
}
public DemoTerm (String n, String d, TermList<DemoTerm> l) {
this.name = n;
this.desc = d;
this.list = l;
}
// METHODS
public void addSyn (DemoTerm t) {
for (int i = 0; i < this.list.size(); i++) {
DemoTerm tempTerm = this.list.get(i);
if (!t.list.contains(tempTerm))
t.list.add(tempTerm);
else
return;
}
// in this oversimplified method we add each Term in the list from
// the parameter "t", to the list of the calling method "this"
for (int i = 0; i < t.list.size(); i++) {
DemoTerm tempTerm = t.list.get(i);
if (!this.list.contains(tempTerm))
this.list.add(tempTerm);
else
return;
}
// here we add each Term in the list from "this" to the list from "t"
// in BOTH cases we run a duplicate check through the overwritten
// contains() method provided by greg in the class TermList extends ArrayList
this.list.add(t);
// in the final step we add "t" to the list inside "this"
}
public String listToString() {
// restituzione della lista dei sinonimi in formato stringa
String s = "\r\n";
Collections.sort(this.list, new DemoTermAlpha());
for (int i = 0; i < this.list.size(); i++) {
s += this.list.get(i).name;
if (i == (this.list.size() - 1))
s += ".";
else
s += "; ";
};
return s;
}
public String toString() {
String s = "\r\n";
s += "\"" + this.name + "\"\r\n";
s += this.desc + "\r\n\r\n";
s += "SYNONYMS or OPPOSITES:" + this.listToString();
return s;
}
}
this is the overwritten contains() method you inspired:
import java.util.ArrayList;
public final class TermList<E> extends ArrayList<E> {
/** Specifica la classe ArrayList inserendo il controllo duplicati,
* sovrascrive il metodo ArrayList.contains(Obj o) ed agisce
* su liste (ArrayList) di oggetti generici (qui vettori di Termine)
*/
static final long serialVersionUID = 0;
@Override
public boolean contains( Object termine2 ) {
for ( E listaDiTermine1 : this ) {
if ( listaDiTermine1.equals( (E) termine2 ) )
return true;
}
return false;
} // end method equals()
}
this is used to list alphabetically terms in the lists:
import java.util.Comparator;
public class DemoTermAlpha implements Comparator<DemoTerm> {
/**TermAlpha is used to list alphabetically the Terms in the collection.*/
public int compare(DemoTerm t1, DemoTerm t2) {
if ((t1.name.compareTo(t2.name)) < 0)
return -1;
else if ((t1.name.compareTo(t2.name)) > 0)
return 1;
return 0;
}
}
this is simple executable main class.
it creates 3 terms with user input. it adds term 2 to the list (of synonyms) of term one and then prints them all to screen to show the result. even simplifying the code the same errors remained. not compilation-wise, obviously. the same as before. term 1 gets term 3 in his list but term 3 doesn't show to have term 1 in its own.
public class DemoTermMain {
public static void main(String[] args) {
DemoTerm one;
DemoTerm two;
DemoTerm three;
String name1 = Input.readString("Input 1st term name: ");
String desc1 = Input.readString("And its description: ");
String name2 = Input.readString("Input 2nd term name: ");
String desc2 = Input.readString("And its description: ");
String name3 = Input.readString("Input 3rd term name: ");
String desc3 = Input.readString("And its description: ");
one = new DemoTerm (name1,desc1);
two = new DemoTerm (name2,desc2);
three = new DemoTerm (name3,desc3);
one.addSyn(three);
System.out.println(one.toString());
System.out.println(two.toString());
System.out.println(three.toString());
}
}
and this is the output i described:
Input 1st term name: blue
And its description: a cold color
Input 2nd term name: red
And its description: a warm color
Input 3rd term name: intense teal
And its description: actually its blue but got different name
"blue"
a cold color
SYNONYMS or OPPOSITES:
intense teal.
"red"
a warm color
SYNONYMS or OPPOSITES:
"intense teal"
actually its blue but got different name
SYNONYMS or OPPOSITES:
this is a BIG class, the Input class. don't bother looking at it, since i just use its "Input.readString()" method. is a basic keyboard input class provided by the professor (as already told you). just compile it with the rest. for my project i'd probably just need to copy one or two methods but for now i'm using it as it is.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Vector;
/**
Una semplice classe per leggere stringhe e numeri
dallo standard input.
*/
public class Input{
private static BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
/**
Legge una linea di input. Nell'improbabile caso di una
IOException, il programma termina.
@return restituisce la linea di input che l'utente ha battuto.
*/
public static String readString(){
String inputLine = "";
try{
inputLine = reader.readLine();
}
catch(IOException e){
System.out.println(e);
System.exit(1);
}
return inputLine;
}
public static String readString(String msg){
System.out.print(msg);
String inputLine = readString();
return inputLine;
}
/**
Legge una linea di input e la converte in uno short.
Eventuali spazi bianchi prima e dopo l'intero vengono ignorati.
@return l'intero dato in input dall'utente
*/
public static byte readByte(){
String inputString = readString();
inputString = inputString.trim();
byte n = Byte.parseByte(inputString);
return n;
}
public static byte readByte(String msg){
System.out.print(msg);
byte n = readByte();
return n;
}
/**
Legge una linea di input e la converte in uno short.
Eventuali spazi bianchi prima e dopo l'intero vengono ignorati.
@return l'intero dato in input dall'utente
*/
public static short readShort(){
String inputString = readString();
inputString = inputString.trim();
short n = Short.parseShort(inputString);
return n;
}
public static short readShort(String msg){
System.out.print(msg);
short n = readShort();
return n;
}
/**
Legge una linea di input e la converte in un int.
Eventuali spazi bianchi prima e dopo l'intero vengono ignorati.
@return l'intero dato in input dall'utente
*/
public static int readInt(){
String inputString = readString();
inputString = inputString.trim();
int n = Integer.parseInt(inputString);
return n;
}
public static int readInt(String msg){
System.out.print(msg);
int n = readInt();
return n;
}
/**
Legge una linea di input e la converte in un long.
Eventuali spazi bianchi prima e dopo l'intero vengono ignorati.
@return l'intero dato in input dall'utente
*/
public static long readLong(){
String inputString = readString();
inputString = inputString.trim();
long n = Long.parseLong(inputString);
return n;
}
public static long readLong(String msg){
System.out.print(msg);
long n = readLong();
return n;
}
/**
Legge una linea di input e la converte in un numero
in virgola mobile. Eventuali spazi bianchi prima e
dopo il numero vengono ignorati.
@return il numero dato in input dall'utente
*/
public static double readDouble(){
String inputString = readString();
inputString = inputString.trim();
double x = Double.parseDouble(inputString);
return x;
}
public static double readDouble(String msg){
System.out.print(msg);
double x = readDouble();
return x;
}
/**
Legge una linea di input e ne estrae il primo carattere.
@return il primo carattere della riga data in input dall'utente
*/
public static char readChar(){
String inputString = readString();
char c = inputString.charAt(0);
return c;
}
public static char readChar(String msg){
System.out.print(msg);
char c = readChar();
return c;
}
/**
Legge una linea di input e restituisce true se la stringa
e' equals a "true" a meno di maiuscole e minuscole, false altrimenti.
@return il booeano dato in input dall'utente
*/
public static boolean readBool(){
String inputString = readString();
inputString = inputString.trim();
boolean b = Boolean.parseBoolean(inputString);
return b;
}
public static boolean readBool(String msg){
System.out.print(msg);
boolean b = readBool();
return b;
}
/**
Legge una sequenza di stringhe conclusa dalla stringa vuota e
restituisce la sequenza in un nuovo array di stringhe.
@return l'array delle stringhe date in input dal'utente
*/
public static String[] readSeq(){
String[] seq = readSeq("");
return seq;
}
public static String[] readSeq(String prompt){
Vector<String> seqTemp = new Vector<String>();
System.out.print(prompt);
String inputString = readString();
while (inputString.length()>0) {
seqTemp.add(inputString);
System.out.print(prompt);
inputString = readString();
}
String[] seq = new String[seqTemp.size()];
return seqTemp.toArray(seq);
}
public static String[] readSeq(String msg, String prompt){
System.out.println(msg);
String[] seq = readSeq(prompt);
return seq;
}
}