Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: adding elements on two mutual ArrayList's

  1. #1
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default adding elements on two mutual ArrayList's

    hello everyone.
    i'm working on a piece of code which is driving me crazy these days...
    this is a project for an exam, and it is about creating a synonims/opposites dictionary.
    there have to be many different functions, but here i will focus only on the "updating" of a list of synonims, and that has to be done calling a method on an object "Term" that adds another Term to his synonims list.

    what i was trying to do - since if Y is synonim of X, X should also be added to Y's synonims list - is to call a method on Term1 that adds Term2 to an ArrayList stored inside Term1, and at the same time, adding Term1 to Term2's list.
    it is a bit complicated to read, but very easy from a logical standpoint. i'm gonna show you my code to be clearer.


    this is the basic structure of a term (object "Termine"):

    public class Termine implements Serializable{
    	private String lemma; // this is the term name
     
    	private String desc; // this is its description
     
    	private ArrayList<Termine> sinonimi = new ArrayList<Termine>(); // this is the synonims list
     
    	private ArrayList<Termine> contrari = new ArrayList<Termine>(); // this is the sunonims list
     
    	static final long serialVersionUID = 0;
     
    }


    so, when i want to "flag" one term as a synonim to another one, for later use with the program, i have to use a method on the second one that adds the first to its list, but that same method HAS TO add to the first's list the second term too, probably, recursively.
    this is the method i'm using right now, it works only to the extent to add one term to another's list, for the second - more complex part - it does nothing:

    public void addSin (Termine t) {
    		// aggiunge un Termine alla lista di sinonimi
     
    		this.sinonimi.add(t);
    		// works: "this" gets updated with a new term in the vector
     
    		ArrayList<Termine> listaSinT = t.getSin();
    		// i use another method to copy inside a new vector ("listaSinT") the contents of the synonims of "t"
     
    		for (int i = 0; i < listaSinT.size(); i++) {
    		// i look through the elements of the vector
     
    			String nextLem = listaSinT.get(i).getLemma();
    		// i use another method to obtain EACH term's name (a string, which is also a primary key, as the description above)	
     
    			if (!(nextLem.equals(this.getLemma()))) {
    		// if the string found is NOT equal to the name of the calling object ("this") do as follows
     
    				Termine newT = new Termine (this.getLemma(), this.getDesc(), this.getSin(), this.getCon());
    		// create a new term identical to "this"
     
    				t.addSin(newT);
    		// call this same method on "t" to add "this" to its list
     
    			} else 
     
    				System.out.println("\"" + this.getLemma() + "\"" + "è già nella lista di sinonimi di \"" + t.getLemma() + "\"");
    // send an error message
    		};		
     
    	}


    as i've already told you, the method adds Term2 to the list of Term1, but it doesnt go farthest from there, this is a piece of a main class to test:

                            one = new Termine (name1,desc1);
     
    			two = new Termine (name2,desc2);
     
    			three = new Termine (name3,desc3);
     
                            // these 3 terms are created with input from the user
     
    			one.addSin(three); // adds "three" to "one"'s synonim list
    			one.addCon(two); // adds "two" to "one"'s opposite list
     
                            System.out.println(one.toString());
    			System.out.println(two.toString());
    			System.out.println(three.toString());


    the last 3 lines are used to check the outcome, and it prompts a toString() of all the terms: "one" carries correctly "three" as synonim and "two" as opposite, but the other two are shown to have both their vectors empty.

    what am i missing? i tried many different solutions, trying to make them more and more complex, then more and more simple, but nothing worked.
    i'm a bit frustrated -__-'

    i hope the code is readable enough, i tried to change colors but it doesn't let me do it inside the code windows
    any kind of help would be immensely appreciated.
    thank you very much to anybody that will try and give their help.

    see you next time, have a good one.
    Last edited by Stormbringer; October 11th, 2013 at 06:27 AM. Reason: making my noobish code more readable


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    I'm going to oversimplify, because I don't understand why it's so complicated, and I can't imagine how/why recursion would ever be involved.

    Let's agree that term1 and term2 are synonyms. Both term1 and term2 are Term instances and Term instances all have an ArrayList instance variable, 'synonyms'. When one wants to *pair* Term instances as synonyms, the method might look something like:
    public void addSynonym( Term term2 )
    {
        // adds term2 to term1's list of synonyms
        synonyms.add( term2 );
     
        // add term1 to term2's list of synonyms
        term2.addSynonym( term1 );
    }
    The above is not the final answer, because it will result in an infinite loop, continuing to add to the other's list until a stack overflow occurs. To resolve the infinite loop, a check to avoid adding duplicates to a Term object's list of synonyms will have to be added.

    Since I've oversimplified, let me know where I have gone too far because I didn't understand the problem.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Stormbringer (October 11th, 2013)

  4. #3
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    first of all, thank you very much for your support.
    and thank you for letting me correct myself in adding the second "y" to "synonym" i was failing at that so much.

    anyway, the code of the method does the 2 things you cited.
    the first command is the same as my this.sinonimi.add(t) and the second one t.addSin (newT) where "newT" is basically a copy of "this" from which we are calling the method "addSin".

    i've put the second inside an if-else command which is the control structure i've used: it checks if the "lemma" (stands for term name) of term2 is already present in the list of synonyms of term1, which i cycle through with the for.

    point is it doesn't work. probably i'm fucking up the cycle or the conditional command. still can't get it.

    p.s. how did you get the code so well formatted and colored?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    I suspect you'll have to write your own equals() method to ensure it returns the desired results. You're probably wholeheartedly excluding List objects rather than their contents when you check for duplicates.

    As for code formatting in posts, the [ highlight=java] code here [ /highlight] tags (without the leading spaces) adds the extra stuff.

    Edit: And no worries about the spelling. Your English is excellent.

  6. #5
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    thanks for the hunches and infos about the spelling (and for your kindess too )...

    as for the code, i didn't get what was my mistake, as in "excluding List object...". do you mean i should work on the strings in the object in a different way?

    i'll try and edit the first post so it is more readble.
    Last edited by Stormbringer; October 11th, 2013 at 06:28 AM. Reason: minor corrections

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    Back to my simplified example, if I add duplicate protection,
    public void addSynonym( Term term2 )
    {
        // prevent adding duplicates
        if ( !synonyms.contains( term2 ) )
        {
            // adds term2 to term1's list of synonyms
            synonyms.add( term2 );
        }
     
        // add term1 to term2's list of synonyms
        term2.addSynonym( term1 );
     
    }
    But the contains() method uses the default equals() method - the one provided by the Object class - to compare existing elements of synonyms to term2 to determine if term2 is already present. Since synonyms elements are all Term objects, the result of the equals() method will always return true.

    I don't think you're using the contains() method, but you're using the equals() method directly which will provide the same undesirable results. To improve the results, write your own Term.equals() method that provides an appropriate comparison. I don't know how Term is setup, but I imagine there is an instance variable for the actual word, like String term, so a possible equals() method to compare Term instances might be:
    // method equals() returns true if the instance variables 'term' are equal, false if not
    public boolean equals( Term term2 )
    {
        // compares the two String term instance variables using String's equals() method
        if ( term.equals( term2.getTerm() )
        {
            return true;
        }
     
        return false;
     
    }
    This equals() method is (again) oversimplified (and may be sufficient for your needs), but you can learn how to write a good Java equals() method by searching. There are several decent articles written on the topic.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    Stormbringer (October 11th, 2013)

  9. #7
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    i think i get what you mean. the rationale behind my duplicate protection block was exactly this one, but seems that i have implemented it wrong, since your explaination sounds right, even if the equals() you wrote seems similar to my solution.
    i will try it out right away and tell you about the results.
    for now thanks for your help, i really appreciate it. a second pair of eyes (with a brain behind them) is REALLY useful in these matters.

    --- Update ---

    as Vic Mackey would say... "Not there yet."
    turns out your idea was the same as mine, just moved into an independent method to call inside the one to add synonyms.

    this is the situation, with your implementation.

    this is the method to add a new synonym (addSin(Termine t)): the part in which the calling object (the term "this") is to be added to EACH SYNONYM in the list of the parameter object (the term "t").

    public void addSin (Termine t) {
     
    		this.sinonimi.add(t);
    // adds term "t" to the list of synonyms (ArrayList "this.sinonimi") of the calling object ("this")
     
    		ArrayList<Termine> listaSinT = t.getSin();
    // i use method get.Sin() to store inside a variable a copy of the list of synonyms of "t"
     
    // HERE COMES THE IMPORTANT PART
     
                    for (int i = 0; i < listaSinT.size(); i++) {
     
    /* "listaSinT" is a vector (ArrayList) that stores every synonym inside the object "t", the cycle goes through all its elements */
     
    		if (this.checkDup(t))
    // this is the method to check for duplicates in a form similar to what you suggested (see below)
     
    			t.addSin(this);
    // this is the simple method to add a synonym, it's called by object "t" and gets "this" as a parameter
     
    		}
     
    }

    down here, i put the <handmade> equals() method you inspired:

    public boolean checkDup (Termine t) {
     
    		if (this.lemma.equals(t.getLemma()))
    /* "lemma" is the plain name of the term, and it's a String. we compare it to "t"'s name using the method getLemma() to return it*/
     
    			return true;
    // duplicate found: returns true
     
    		else
     
    			return false;
    // not found: returns false
     
    }

    to clarify even more, i put right down here the istance variables of the objects we are handling all the time here, the class "Termine" (which is simply italian for "Term"). I'll copy just variables and not al the constructors and methods, i'll translate comments to make it understandable, but if you followed me 'till now, you already got basically all that these things mean.

    public class Termine implements Serializable{
     
    	private String lemma;
    // this is the word itself, the name of the term
     
    	private String desc;
     
    // this is the description of the term
     
    	private ArrayList<Termine> sinonimi = new ArrayList<Termine>();
     
    // this is the vector (left empty as default condition) of all the synonyms of the term
     
    	private ArrayList<Termine> contrari = new ArrayList<Termine>();
     
    // this is exactly identical to the list right above it, just that stores opposites instead of synonyms
     
    	static final long serialVersionUID = 0;
     
    // this is here just for the sake of binary save/load
     
    }

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    One can only talk abstract design for so long. I put the following demo together as an example of what I'm thinking which has the elements of what I described above but applied slightly differently or in different places. I hope it helps.
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
     
    // class MultpleListsDemo demonstrates how to manage adding the same item
    // to two lists which happen to be instances of the same object
    public class MultipleListsDemo
    {
        // a list of contents which belong to both lists with duplicates
        private final String[] addList = { "one", "two", "three", "four", "one",
                "five", "six", "two", "seven", "eight", "nine", "ten" };
     
        // two Item instances that will contain the same items
        Item item1;
        Item item2;
     
        // default constructor creates the two Item instances in which
        // the similar items will be held and then runs the simple demo
        public MultipleListsDemo()
        {
            item1 = new Item( "numbers" );
            item2 = new Item( "numerals" );
     
            // runs the demo
            runDemo();
        }
     
        // method runDemo() adds the list of common terms to the first item,
        // adding them also to the second item without adding duplicates
        // to either one. adding the same list of terms is then added to the
        // second list to ensure duplicates will not be added to either Item
        // instance
        private void runDemo()
        {
            System.out.println( "Adding the terms to Item1 . . ." );
     
            for ( String itemToAdd : addList )
            {
                item1.addTerm( itemToAdd, item2 );
            }
     
            System.out.println( "\nAnd the results are:" );
            // printing out the resulting contents
            System.out.println( "Item1 contains: " + item1 );
            System.out.println( "Item2 contains: " + item2 );
     
            System.out.println();
     
            System.out.println( "Adding the same terms to Item2 . . ." );
     
            // try adding the list again to item2 (which will add to item1)
            for ( String itemToAdd : addList )
            {
                item2.addTerm( itemToAdd, item1 );
            }
     
            System.out.println( "\nAnd the results are:" );
            // printing out the resulting contents
            System.out.println( "Item1 contains: " + item1 );
            System.out.println( "Item2 contains: " + item2 );
     
        }
     
        // the main method calls the demo class which initializes the necessary
        // variables and then presents the demo
        public static void main( String[] args )
        {
            new MultipleListsDemo();
     
        } // end method main()
     
    } // end class MultipleListsDemo
     
    // how i imagine the Item class might look but without any frills
    class Item
    {
        String itemName;
        List<String> terms;
     
        // single-parameter constructor that gives the item a name
        public Item( String itemName )
        {
            this.itemName = itemName;
            terms = new ItemList<String>();        
     
        } // end constructor Item()
     
        // adds a term provided by the first parameter to this list and
        // then to the second list passed as the second parameter 
        public void addTerm( String itemToAdd, Item item2 )
        {
            // checks for duplicates and returns if found
            if ( !terms.contains( itemToAdd ) )
            {
                terms.add( itemToAdd );
            }
            else
            {
                return;
            }
     
            // otherwise the item is added to the second list
            item2.addTerm( itemToAdd, this );
        }
     
        // a toString() method that may be more complicated than it needs to be
        // but . . . 
        public String toString()
        {
            return terms.toString();
        }
     
        private final class ItemList<E> extends ArrayList<E>
        {
            @Override
            public boolean contains( Object item2 )
            {
                    for ( E list1Item : this )
                    {
                        if ( list1Item.equals( (E)item2 ) )
                        {
                            return true;
                        }
                    }
     
                    return false;
     
            } // end method equals()
     
            @Override
            public String toString()
            {
                return ( Arrays.toString( this.toArray() ) );
     
            } // end method toString()
     
        } // end class ItemList
     
    } // end class Item

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    Stormbringer (October 12th, 2013)

  12. #9
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    for starters, i really feel to thank you again because you are - actually - staying with me and trying your best to let me come through this one, and for that i must appreciate and reiterate how grateful i am.

    as for seconds, i'm now going through your last code piece, to try and understand it well and launching it on my eclipse and see how it can actually help (for now im just about at the start trying to understand it and i thought to write this first).

    i'll let you know what i come up with.

  13. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    You're welcome, and good luck. Let me know if you have any questions.

  14. #11
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    i've bashed my head on this same fucking method for almost 5 hours today.
    those were not straight because i managed to have a shower and eat lunch, but they were 1.5 hrs ---> brief return to life of a human being ---> 3+ hrs

    you pointed me to the right direction and i think i got close to my goal, but it still doesn't work well: i'm testing vectors with just one element, so that hints that MAYBE my redundant commands are sometimes inserting/erasing the same thing over and over until SOMETIMES it stays, and SOMETIMES it gets deleted...
    but anyway this has become my personal hell, lately...

    i'll try and post something soon, but i feel im close: even if my code is unsecure - not being so messy - it has some logic in it and maybe i could come up with what im looking for. still im growing a bit more desperate by each passing moment...

    the point is that i don't think it's very clever and polite to paste here all of my tries, i'd like it best to show you something that works, even if sloppy :-(
    Last edited by Stormbringer; October 12th, 2013 at 11:37 AM. Reason: some grammar

  15. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    Whenever you're ready. If posts were limited to elegant code that worked exactly as intended, there wouldn't be much point to the forum, but I understand your point.

    I read a related blog post recently that I thought made a good point. The blogger was wishing he'd been more open, sharing and collaborative from his earliest days as a developer, "putting himself out there," so to speak. He learned the value of it as a more senior developer, and figured it had taken him a lot longer to get there than if he'd have been more open to learning from others by sharing his ideas with those who could have given constructive criticism and giving the same to those who needed it when he could.

  16. #13
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    i'm adding something because reading again the conversation makes me think that maybe my poor explaination and the following confusion may have cast some misunderstanding on what i want my two (similar) methods to do.

    i'll use some schematics instead of java:

    class Term:
     
    has a String "name".
    has a String "description".
     
    has a vector "synonyms" type Term:
    that means he carries a collection of complex objects
    (which have their own vectors).
     
    has a vector "opposites" type Term:
    that means he carries a collection of complex objects
    (which have their own vectors).
     
     
    method addSynonym:
     
    it can be called by any Object type "Term", has to have another Object type "Term" as parameter,
    which has to be added to the calling one.
     
     
    method addOpposite:
     
    it can be called by any Object type "Term", has to have another Object type "Term" as parameter,
    which has to be added to the calling one.

    what is ruining my life are the two methods, one is easier (synonyms) one is a bit harder (opposites), even if at start i thought them to be roughly the same, they aren't.
    i've used very similar loops, with a duplicate check like yours, to do a series of 5 numbered procedures using the various fileds of each Term.
    i'll explain what my code TRIES TO DO up until now, without delivering IN THE RIGHT WAY:

    method addSynonym(Term t):
     
    1)   adds EACH Term in the SYNONYMS vector of the calling obj ("this"), to the SYNONYMS vector of "t" (see: if X == Y and Z == X, Z == Y)
    2)   adds EACH term in the OPPOSITES vector of the calling obj ("this"), to the OPPOSITES vector of "t" (same identical thing, different vector)
     
    3) and 4) repeat but backwards (trying to get the best filtering from the if block), synonyms from "t" get imported inside the calling obj synonym vector; opposites of "t" to opposites of calling obj.
     
    5) FINALLY Term "t" gets imported inside the calling object's synonyms vector.

    the second method is not the same because it crosses the references:

    method addOpposite(Term t):
     
    1)   adds EACH Term in the OPPOSITES vector of the calling obj ("this"), to the SYNONYMS vector of "t" (see: if X != Y and Z != X, Z == Y)
    2)   adds EACH term in the SYNONYMS vector of the calling obj ("this"), to the OPPOSITES vector of "t" (same identical thing, different vector)
     
    3) and 4) repeat but backwards (trying to get the best filtering from the if block), opposites from "t" get imported inside the calling obj synonyms vector; synonyms of "t" to opposites of calling obj.
     
    5) FINALLY Term "t" gets imported inside the calling object's opposites vector.

    i've made 100 x2 lines of code that repeat the same construct with roughly (or every) combination, to get the job done. but the output of my testing main class its showing me i've failed. here's the output of my adding a single synonym(3) to a first term(1) and a single opposite(2) to the same term ("..." means an empty vector):

    "good"
    what is good
     
    SYNONYMS:
    right; right.
    OPPOSITES:
    bad.
     
    "bad"
    what is not good
     
    SYNONYMS:
    ...
    OPPOSITES:
    right.
     
    "right"
    something that is not bad
     
    SYNONYMS:
    ...
    OPPOSITES:
    ...


    --- Update ---

    (i didnt read your last reply because i was compiling as precisely as i could my last post, but thinking it over: i'm really seeing now that blogger's point. but that's obvious now, since it has been 2 days we are chatting on this matter ^^)

    --- Update ---

    (deleting the double post)

  17. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    Yes, you've changed the problem. You've certainly made it more complex, at least in your approach to it, but I haven't thought about it enough to see why it has to be so much more complex. I'll stew on it while I relax away this day that isn't much good for anything else. (Sorry, that may be insensitive to your frustration.)

  18. #15
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    sure, for today i've also broke myself on it enough for a day xD

    here in italy it's 19:46, so since i didn't do anything else for now i'll rest too.

    thanks for your invaluable help up until now. i hope to come up with this. due time approaches...

  19. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    I've read your last description of the problem several times, and I probably get lost along the way, but I don't see or don't understand the problem. It's time to give a short bit of code that demonstrates what you're trying to do with the problem clearly shown.

  20. #17
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    okay. i paste here the exact method as it is in front of me now when i'm working on the code for the project. i put on top of it the istance variables and the constructor used in the method (which is the most overloaded).

    public class Termine implements Serializable{
     
    	// INSTANCE VARIABLES
     
    	private String lemma;
     
    	private String desc;
     
    	private ArrayList<Termine> sinonimi = new TermList<Termine>(); // the term's collection of synonyms
     
    	private ArrayList<Termine> contrari = new TermList<Termine>();// the term's collection of opposites (italian:"contrari" --> english:"opposites")
     
    	static final long serialVersionUID = 0; // just for the sake of binary saving-loading
     
     
     
    	// OVERLOADED CONSTRUCTOR
     
    public Termine (String l, String d, ArrayList<Termine> s, ArrayList<Termine> c) {
     
    		if (!l.equals(null) || !l.equals("") || !checkChar(l))
     
    			this.lemma = l;
     
    		else
     
    			throw new IllegalArgumentException ();
     
    		if (!d.equals(null) || !d.equals("") || !checkChar(d))
     
    			this.desc = d;
     
    		else
     
    			throw new IllegalArgumentException ();
    		/* here, we do a very important control on string formatting:
    		 * "lemma" (Term name) and "desc" (Term description) are provided
    		 * by user keyboard input, the static method "checkChar(String s)"
    		 * checks that the String passed as argument doesnt contain anything
    		 * but upper or lower case letter (no other numbers or characters).
    		 * i'll paste this method's body after everything else*/
     
     
    		this.sinonimi = s;
     
    		this.contrari = c;
     
    		/* cambio il riferimento dei vettori di Termine vuoti dell'oggetto predefinto
    		 * con quelli passati come parametro al costruttore*/
     
    	}
     
     
     
    	// THE "ADD SYNONYM" METHOD
     
    public void addSin(Termine t) {
     
    		for (int i = 0; i < this.sinonimi.size(); i++) {
     
            		String tempLemma = this.sinonimi.get(i).getLemma();
     
            		String tempDesc = this.sinonimi.get(i).getDesc();
     
            		ArrayList<Termine> tempSin = this.sinonimi.get(i).getSin();
     
            		ArrayList<Termine> tempCon = this.sinonimi.get(i).getCon();
     
            		Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
    			// here i'm using some temp-variables to create a tempTerm that is a copy
    			// of each single synonym of "this"
     
            		if (!t.sinonimi.contains(tempTerm))
            		// checking if it's already in the synonym list of "t"
     
                    		t.sinonimi.add(tempTerm);
     
            		else
     
            			return;
     
            }
            // (1) - each syn. from the list of the calling obj. ("this") gets added to the syn. list of "t"
     
     
            for (int i = 0; i < this.contrari.size(); i++) {
     
            	String tempLemma = this.contrari.get(i).getLemma();
     
            	String tempDesc = this.contrari.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = this.contrari.get(i).getSin();
     
            	ArrayList<Termine> tempCon = this.contrari.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!t.contrari.contains(tempTerm))
     
                    	t.contrari.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (2) - exact same thing for opposites of the calling obj. ("this")
     
            for (int i = 0; i < t.sinonimi.size(); i++) {
     
            	String tempLemma = t.sinonimi.get(i).getLemma();
     
            	String tempDesc = t.sinonimi.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = t.sinonimi.get(i).getSin();
     
            	ArrayList<Termine> tempCon = t.sinonimi.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!this.sinonimi.contains(tempTerm))
     
                    	this.sinonimi.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (3) - now, every syn. of "t"'s list is added to the syn. list of "this"
     
     
            for (int i = 0; i < t.contrari.size(); i++) {
     
            	String tempLemma = t.contrari.get(i).getLemma();
     
            	String tempDesc = t.contrari.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = t.contrari.get(i).getSin();
     
            	ArrayList<Termine> tempCon = t.contrari.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!this.contrari.contains(tempTerm))
     
                    	this.contrari.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (4) - opposites from "t"'s opp. list are added to opp. list of "this"
     
     
            if (!this.sinonimi.contains(t))
     
                this.sinonimi.add(t);
     
        	else
     
        		return;
            // (5) - at the very end, we add "t" to the syn. list of "this"
     
    	}
     
    }
     
     
     
    	// THE "ADD OPPOSITE" METHOD (SIMILAR BUT DIFFERENT)
     
    public void addCon(Termine t) {
     
    		for (int i = 0; i < this.contrari.size(); i++) {
     
            		String tempLemma = this.contrari.get(i).getLemma();
     
            		String tempDesc = this.contrari.get(i).getDesc();
     
            		ArrayList<Termine> tempSin = this.contrari.get(i).getSin();
     
            		ArrayList<Termine> tempCon = this.contrari.get(i).getCon();
     
            		Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            		if (!t.sinonimi.contains(tempTerm))
            		// controllo sui lemmi con il metodo "contains()" riscritto per il vettore
     
    		                t.sinonimi.add(tempTerm);
     
            		else
     
            			return;
     
    	        }
            // (1) - every OPP. of "this" is added to the SYN. list of "t"
     
     
            for (int i = 0; i < this.sinonimi.size(); i++) {
     
            	String tempLemma = this.sinonimi.get(i).getLemma();
     
            	String tempDesc = this.sinonimi.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = this.sinonimi.get(i).getSin();
     
            	ArrayList<Termine> tempCon = this.sinonimi.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!t.contrari.contains(tempTerm))
     
              	      t.contrari.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (2) - every SYN. of "this" is added to OPP. list of "t"
     
            for (int i = 0; i < t.contrari.size(); i++) {
     
            	String tempLemma = t.contrari.get(i).getLemma();
     
            	String tempDesc = t.contrari.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = t.contrari.get(i).getSin();
     
            	ArrayList<Termine> tempCon = t.contrari.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!this.sinonimi.contains(tempTerm))
     
            	        this.sinonimi.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (3) - every OPP. of "t" is added the SYN. list of "this"
     
     
            for (int i = 0; i < t.sinonimi.size(); i++) {
     
            	String tempLemma = t.sinonimi.get(i).getLemma();
     
            	String tempDesc = t.sinonimi.get(i).getDesc();
     
            	ArrayList<Termine> tempSin = t.sinonimi.get(i).getSin();
     
            	ArrayList<Termine> tempCon = t.sinonimi.get(i).getCon();
     
            	Termine tempTerm = new Termine (tempLemma, tempDesc, tempSin, tempCon);
     
            	if (!this.contrari.contains(tempTerm))
     
                    	this.contrari.add(tempTerm);
     
            	else
     
            		return;
     
            }
            // (4) - every SYN. of "t" is added to the OPP. list of "this"
     
     
            if (!this.contrari.contains(t))
     
                this.contrari.add(t);
     
        	else
     
        		return;
            // (5) - at the very end, we add "t" to the opposite list of "this"
     
        }
     
     
     
    public static boolean checkChar (String str) {
    		//Checks if an input string has different characters from simple letters
     
    		boolean detected = false;
     
    		char [] car = new char[6];
     
    		car[0] = '['; car[1] = ']'; car[2] = '\\'; car[3] = '^'; car[4] = '_'; car[5] = '`';
     
    		//the simple array carries the only 6 unicode chars that sit between
    		// letters upper case and letters lower case
     
    		char start = 'A';
     
    		char end = 'z';
     
    		for (int i = 0; i < str.length(); i++) {
     
    			char c = str.charAt(i);
     
    			for (int j = 0; j < car.length; j++) {
     
    				if ((c == car[j]) || (c > end) || (c < start))
     
    					detected = true;
     
    			};
     
    		};
     
    		/* using method String.charAt() we parse each char of the input string and see if
    		* any of those falls out of the desired range in the unicode chart. if one does, 
    		* the boolean is set to "true", if not it stays "false"*/
     
    		return detected;
    		// in the end, the method returns the boolean as it is
    	}
     
     
     
    }

    here - again - some lines from the main class used to test the method:

    public class TestDiz01 {
     
    	public static void main(String[] args) {
     
    		Dizionario d = new Dizionario("dizionario prova", "ita");
     
    		// this is the object dictionary", for now its used just as a simple container
    		// don't think it over more than for what is its purpose
     
    		Termine one;
    		Termine two;
    		Termine three;
     
    		// i define 3 Term variables without initializing them
     
    		String name1 = Input.readString("dammi il nome del I termine");
     
    		String desc1 = Input.readString("dammi la desc del I termine");
     
    		String name2 = Input.readString("dammi il nome del II termine");
     
    		String desc2 = Input.readString("dammi la desc del II termine");
     
    		String name3 = Input.readString("dammi il nome del III termine");
     
    		String desc3 = Input.readString("dammi la desc del III termine");
     
    		// i ask the user to type in from keyboard name and description for 3 
    		// terms using a class "Input" provided by the professor
     
    		try {
     
    			one = new Termine (name1,desc1);
     
    			two = new Termine (name2,desc2);
     
    			three = new Termine (name3,desc3);
     
    			// i inizialize the past variables calling the Term constructor without
    			// vectors as parameteres
     
    			one.addSin(three);
    			one.addCon(two);
     
    			// i add Term "three" as a synonym of Term "one", and "two"
    			// as its opposite
     
    		} catch (IllegalArgumentException e) {
     
    			System.out.println("Il campo non può essere uguale a 'null': " + e);
     
    		}
    		// all of my simple exceptions don't seem to work, but let's not focusing
    		// on them right now
     
    	}	
     
    }

    and lastly, the incorrect output. it has something of desperately and noobishly funny (in caps lock, the user's input):

    dammi il nome del I termine RIGHT
     
    dammi la desc del I termine SOMETHING NOT WRONG
     
    dammi il nome del II termine WRONG
     
    dammi la desc del II termine SOMETHING NOT RIGHT
     
    dammi il nome del III termine JUST
     
    dammi la desc del III termine SOMETHING THAT'S RIGHT
     
    "right"
    something not wrong
     
    SINONIMI:
    just; just.
    CONTRARI:
    wrong.
     
    "wrong"
    something not right
     
    SINONIMI:
     
    CONTRARI:
    just.
     
    "just"
    something that's right
     
    SINONIMI:
     
    CONTRARI:

    so... this is it for now. no blinding flash of light with the gift of newfound wisdom, even if i hoped so :-( sometimes, i thought to have found the solution, but after a lot of time spent on understanding and then implementing, the behaviour remained faulty.
    but for now the code is resting too similar to itself, because since 2 days ago i couldn't make it to change much of the key aspects, because had no new ideas

    anyway, i really hope that my code and my comments are understandable even if maybe too insecure and unskilled in your view (i know they are ^^').
    but i spent a lot of time trying to present it and comment it in english in the best way possible, so that at least the understanding isnt frustrating.

    thanks for your precious help up until now, because even if i still didn't resolve the matter i feel that you are pointing me to the right direction, inspiring me and correcting some of my logical messes, and thanks again for your good will and politeness ^^

  21. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    Seeing many errors:

    undefined types/symbols TermList, Input, Dizionario;
    undefined methods Termine.getLemma(), getDesc(), getSin(), getCon();
    undefined constructor Termine( String, String ).

  22. #19
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

    "Input" is a class provided by the professor and used in all the course, it's for user's keyboard input.
    "Dizionario" is another class in my project (which is a collection of Termine's).
    i didn't finduseful to paste them too.

    as for TermList, sorry i forgot to include it:

    private final class TermList<E> extends ArrayList<E> {
     
    		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()
     
    	}

    i thought you wanted to see just the addSin() method, so i deliberately avoided to report every single part of the Term class. but anyway here they are the other 5 things you mentioned:

    public Termine (String l, String d) {
     
    		if (!l.equals(null) || !l.equals("") || !checkChar(l))
     
    			this.lemma = l;
     
    		else
     
    			throw new IllegalArgumentException ();
     
    		if (!d.equals(null) || !d.equals("") || !checkChar(d))
     
    			this.desc = d;
     
    		else
     
    			throw new IllegalArgumentException ();
     
    	}
     
    public String getLemma() {
     
    		return this.lemma;
     
    	}
     
    	public String getDesc() {
     
    		return this.desc;
     
    	}
     
    	public ArrayList<Termine> getSin() {
     
    		Collections.sort(this.sinonimi, new TermAlpha());
     
    		return this.sinonimi;
     
    	}
     
    	public ArrayList<Termine> getCon() {
     
    		Collections.sort(this.contrari, new TermAlpha());
     
    		return this.contrari;
     
    	}

  23. #20
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: adding elements on two mutual ArrayList's

    As I said in post #16, "It's time to give a short bit of code that demonstrates what you're trying to do with the problem clearly shown." This isn't a short bit, and it doesn't demonstrate anything, because I can't run it to get the results, errors, whatever it is that you've been tearing your hair out about. I can read the code to go to sleep each night, but I can't reliably see what it does or doesn't do without running it. The Java compiler in my biologic computer is limited to a few lines of simple code at a time, and even then it sometimes makes mistakes.

    Sorry I can't be more help. I'll bet you figure it out eventually. Simplify where you can, breaking the complex parts into smaller, less complex - even easy - parts.

  24. #21
    Junior Member Stormbringer's Avatar
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: adding elements on two mutual ArrayList's

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

Similar Threads

  1. LinkedList : Adding elements in constructor
    By Anusha870 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 8th, 2012, 12:45 PM
  2. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 06:30 AM
  3. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  4. [SOLVED] ArrayList object's elements confusing??? doesnt replace the elements? set() method?
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 21st, 2011, 01:20 PM
  5. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 09:48 PM