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

Thread: How to convert two elements into one

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to convert two elements into one

    Is it possible to convert one element (string) from one linkedlist with another element (string) from another linkedlist to have an output of one element that joined the two strings?
    I am trying to show that a card is facing up so I need to add the string UP in the card name without producing two elements (card, up) -- should be cardUP.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to convert two elements into one

    It sounds like the upness is a property of a card. Consider modelling this by writing a Card class whose toString() or some other method produces the desired string.

    One consequence of this approach is that the "parallel" lists of String become a single list of Card which can be easier to keep track of.

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

    titowinky (May 8th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to convert two elements into one

    This is a solitaire game (without graphics) that I am trying to write. I can't figure out how to write a class with that property because the cards are not always facing down. By default they are facing down. So the status changes.

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to convert two elements into one

    So the status changes
    That's what setter methods are for. Your Card class would have an instance variable like isUp or whatever, and a setter method:

    public class Card {
       private boolean isUp = false;
     
          /** Sets the the card's upness to a given value. */
       public void setUp(boolean b) {
          isUp = b;
       }
     
       public String toString() {
          String str = /* whatever */
          if(isUp) {
             str += "(UP)";
          }
          return str;
       }
    }

    As well as isUp you might have other instance variables like suit and rank and a constructor to set up a card with a particular suit and rank.

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to convert two elements into one

    So what will be the output of that code if I do sys.out.println(str) if the string is KD?
    I have the code below and the corresponding output.
    void openTopCards(){
    		LinkedList<String> fliper = new LinkedList <String>();
    		face.add("Up");
    		face.add("Dn");
    		for (int i=0; i < m; i++){
    			if (maneuver[i].size() != 0){
    				fliper.add(maneuver[i].pollLast());
    				fliper.add(face.peekFirst());
    				maneuver[i].addAll(fliper);
    			}
    			fliper.clear();
    		System.out.println("maneuver[" + i + "]" + maneuver[i]);
    		}
    	}
    The output is:
    maneuver[0][AD, Up]
    maneuver[1][KD, 7D, Up]
    maneuver[2][QD, 6D, AH, Up]
    maneuver[3][JD, 5D, KH, 9H, Up]
    maneuver[4][10D, 4D, QH, 8H, 5H, Up]
    maneuver[5][9D, 3D, JH, 7H, 4H, 2H, Up]
    maneuver[6][8D, 2D, 10H, 6H, 3H, AS, KS, Up]
    I want the output to be like this:
    maneuver[6][8D, 2D, 10H, 6H, 3H, AS, KSUp or Up KS] because there will other cards going on top of KS in the future.
    I am thinking of putting all the open cards in another linkedlist then adding them all to the maneuver linkedlist.
    Last edited by titowinky; May 8th, 2012 at 07:46 PM.

Similar Threads

  1. [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
  2. [SOLVED] Gap between GUI elements
    By petemyster in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2011, 11:44 AM
  3. Duplicating Elements through Recursion
    By vk999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 12th, 2011, 01:22 AM
  4. Transferring elements between arraylists
    By KipTheFury in forum Collections and Generics
    Replies: 6
    Last Post: August 23rd, 2010, 02:13 PM
  5. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM