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: Help camparing array elements

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help camparing array elements

    Okay this is my first question on here. but essentially I have 2 codes (one written by someone else) and mine is supposed to implement their code. and in this code i am supposed to deal out 2 hands of 5 cards. compare each card of 1 hand to the other cards of the same hand.<-- thats where my problem is.

    In my code i am only comparing the 1st element of the array to the rest. i need to compare each element to the remainder of the array

    (MY CODE)
    public class cardTester
    {
    public static void main(String[] args)
    {
    Card[] hand1 = new Card[5];
    Card[] hand2 = new Card[5];
    int pair1;
    pair1 =0;

    System.out.println("Player1 hand");
    for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
    {
    Card card1 = new Card();
    card1.printCard();
    System.out.println();
    hand1[x] = card1;

    for(int y=1; y<5; y++)
    {
    Card card2 = new Card();
    card2.printCard();
    System.out.println();
    hand1[y] = card2;

    if(hand1[x].face() == hand1[y].face())
    pair1++;
    }
    System.out.println(pair1);
    }


    System.out.println("Player2 hand");
    for(int i=0; i<hand2.length; i++)
    {
    Card card1 = new Card();
    card1.printCard();
    System.out.println();
    hand2[i] = card1;
    }
    }
    }
    -----------------------------------------------------------------------------------------------

    I am going to see my TA tomorrow reguardless, but i was wondering if anyone here could help me.


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: Help camparing array elements

    Quote Originally Posted by Richmond View Post
    Okay this is my first question on here. but essentially I have 2 codes (one written by someone else) and mine is supposed to implement their code. and in this code i am supposed to deal out 2 hands of 5 cards. compare each card of 1 hand to the other cards of the same hand.<-- thats where my problem is.

    In my code i am only comparing the 1st element of the array to the rest. i need to compare each element to the remainder of the array

    (MY CODE)
    public class cardTester
    {
    public static void main(String[] args)
    {
    Card[] hand1 = new Card[5];
    Card[] hand2 = new Card[5];
    int pair1;
    pair1 =0;

    System.out.println("Player1 hand");
    for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
    {
    Card card1 = new Card();
    card1.printCard();
    System.out.println();
    hand1[x] = card1;

    for(int y=1; y<5; y++)
    {
    Card card2 = new Card();
    card2.printCard();
    System.out.println();
    hand1[y] = card2;

    if(hand1[x].face() == hand1[y].face())
    pair1++;
    }
    System.out.println(pair1);
    }


    System.out.println("Player2 hand");
    for(int i=0; i<hand2.length; i++)
    {
    Card card1 = new Card();
    card1.printCard();
    System.out.println();
    hand2[i] = card1;
    }
    }
    }
    -----------------------------------------------------------------------------------------------

    I am going to see my TA tomorrow reguardless, but i was wondering if anyone here could help me.
    To make your code easy to read and find help, please enclose it with [highlight = java] code here[/java]

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help camparing array elements

    i know what you mean, but as you can see i am new to posting on this website and i am unsure on how to make my code look neat the the website.. it looks fine on Dr. Java, but i dont know how to make it look like that here. I can add comment lines to tell which } ends what

     
    public class cardTester
    {
      public static void main(String[] args)
      {
        Card[] hand1 = new Card[5];
        Card[] hand2 = new Card[5];
        int pair1;
        pair1 =0;
     
        System.out.println("Player1 hand");
        for(int x=0; x<5; x++)
        {
          Card card1 = new Card();
          card1.printCard(); 
          System.out.println();
          hand1[x] = card1;
     
          for(int y=1; y<5; y++)
          {
            Card card2 = new Card();
            card2.printCard(); 
            System.out.println();
            hand1[y] = card2;
     
            if(hand1[x].face() == hand1[y].face())
              pair1++;
        }//end for loop 2
          System.out.println(pair1);
        }//end for loop 1 aka nested
     
     
        System.out.println("Player2 hand");
        for(int i=0; i<hand2.length; i++)
        {
          Card card1 = new Card();
          card1.printCard(); 
          System.out.println();
          hand2[i] = card1;
        }//end for loop player2 hand
      }//end main
    }// end class
    Last edited by Richmond; February 8th, 2012 at 12:08 AM.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Help camparing array elements

    The highlighting was all that was necessary, thanks!

    Here are a list, in order, of things that I noted about your code.

    1: You naming for your class should start with a capitol letter. (EG String, ArrayList, Card, System)

    2: You spacing is incorrect, this should help with most of those problems (Also your variable names)

    3. Your logic here is... a bit flawed. How wouold you do this on a piece of paper? (I'd recommend grid paper because you are using arrays). It looks like you are using this as a 2d array, but I can't tell.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help camparing array elements

    1)This is just a Tester that i will later convert to my EMACS java code. that has a capital letter.

    2) okay.

    3) on a piece of paper i read 5 random cards compare card 1 to card 2 card 1 to 3.. card 1 to 5.. card 2 to 3-5 card 3-5 card 4-5. its NOT a 2d array. its 1d because i only care about the number valued with that card.

    and if you think its flawed because of the for(int x=0; x<1; x++)

    in my first post i had it as
    for(int x=0; x<1; x++)//<--if i use 5 or hand1.length here it will give me 5 different hands.
    telling you why i had it at 1

Similar Threads

  1. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  2. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 06:30 AM
  3. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  4. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  5. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM