Results 1 to 5 of 5
  1. #1
    liQuorice is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default [SOLVED]Adding elements before and after array

    Hi all,

    I'm new to Java and I'm having this problem with adding elements alternately in front and behind an array.

    The methods to add the elements I have already figured out.

    I also came up with a method to add the elements alternately to the array, but my instructor told me it could be done with a boolean instead of what I have now.

    My method now is to keep up count when the data that needs to be added is read and whenever the count odd it adds data BEHIND and whenever it's even it adds data IN FRONT of the array.

    Java Code:
    in.useDelimiter("=");
    		
    		int count = 0;
    		
    		while (in.hasNext()) {
    			String array = in.next();
    			Scanner arrayScanner = new Scanner (array);
    			if (count%2==0) {
    				coordinateArrayInput.addInFrontArray(readArray(arrayScanner));
    			} else {
    				coordinateArrayInput.addBehindArray(readArray(arrayScanner));
    			}
    			count+=1;
    Could anyone please tell me how to fix this problem with the boolean my instructor is talking about?

    Thank you.

    Yours,

    liQuorice
    Last edited by liQuorice; 03-04-2011 at 12:53 AM. Reason: Solved

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose you have a boolean flag; it can be either true or false; if it is true you add a value, say, to the front of the array, otherwise you append it to the end. You 'toggle' the value of that flag like this:

    Java Code:
    flag= !flag;
    You have to do that as the last statement of the body of your loop and you have to initialize its value to true or false, depending on if you want to insert or append your first element.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    liQuorice is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Dear JosAH,

    Thank you for your reply.

    Unfortunately it was not the answer I was looking I for (I think...?).

    My instructor told me you can do it with a boolean AND without the count to check if it's even or odd.

    Still a mystery to me...anyone care to share their knowledge :)?

    Thank you.

    Yours,

    liQuorice

  4. #4
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    i think you need to look at Jos' answer again. Unless I'm misunderstanding you're question, Jos' answer should do it.
    Basically, he is suggesting this:
    Java Code:
    boolean flag = true;
    while (in.hasNext()) {
        if (flag) {
            coordinateArrayInput.addInFrontArray(readArray(arrayScanner));
        } else {
            coordinateArrayInput.addBehindArray(readArray(arrayScanner));
        }
        flag = !flag;
    }
    This uses a boolean and doesnt use a counter.

    Hope this explains it better!

  5. #5
    liQuorice is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you berkeley and Jos!

    This solves my problem....:D!

    Yours,

    liQuorice

Similar Threads

  1. Adding elements to an ArrayList
    By ArcherSam in forum Advanced Java
    Replies: 7
    Last Post: 01-28-2011, 03:05 PM
  2. Problem in adding arrayList elements
    By cool in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:27 PM
  3. Adding the Elements of a 2D Array
    By RMcLuckie45 in forum New To Java
    Replies: 0
    Last Post: 11-07-2010, 11:04 PM
  4. Adding elements in array list and vectors using threads
    By hina.yousuf@seecs.edu.pk in forum Advanced Java
    Replies: 2
    Last Post: 10-10-2010, 03:07 AM
  5. Adding elements to an Object Array
    By aneesahamedaa in forum New To Java
    Replies: 4
    Last Post: 09-07-2008, 03:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •