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

Thread: Writing a very simple "Poker" game in Java

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing a very simple "Poker" game in Java

    Hello, everyone.
    I'm writing an extremely simple "Poker" type of game but I keep getting an error.
    The code is supposed to do the following things; Prompt the user to enter 7 integers in the range 1 to 13 (inclusive), display a table of frequencies of the integers from 1 to 13, find the integer that occurs most frequently, and finally check if the 7 integers contain a continuous sequence of 5 or more.

    Where the code messes up is at the boolean and where I try to return i. What's going wrong?


     
    import java.util.Scanner;
    public class Poker
    {
      public static void main(String[] args)
      {
        int[] arrayOfInt1 = new int[7];
        int[] arrayOfInt2 = new int[14];
     
        readCards(arrayOfInt1);
        updateFreq(arrayOfInt1, arrayOfInt2);
        System.out.println("Number\tFrequency ");
        for (int i = 1; i < arrayOfInt2.length; i++) {
          System.out.println(i + "\t" + arrayOfInt2[i]);
        }
        int i = findMax(arrayOfInt2);
        System.out.println("The most frequent number is " + i + " with " + arrayOfInt2[i] + " occurrences.");
     
        int j = findSequence(arrayOfInt2);
        if (j > 0) {
          System.out.println("The sequence of 5 starts at " + j);
        }
        else
          System.out.println("There is no sequence of 5.");
      }
     
      public static void readCards(int[] paramArrayOfInt)
      {
        boolean bool = false;
     
        Scanner localScanner = new Scanner(System.in);
        do {
          System.out.print("Enter 7 integers (1-13): ");
          for (int i = 0; i < paramArrayOfInt.length; i++) {
            paramArrayOfInt[i] = localScanner.nextInt();
          }
          bool = checkRange(paramArrayOfInt);
        }while (!bool);
      }
     
      public static int checkRange(int[] paramArrayOfInt)
      {
        int i = 1;
     
        for (int j = 0; j < paramArrayOfInt.length; j++) {
          if ((paramArrayOfInt[j] < 1) || (paramArrayOfInt[j] > 13)) {
            i = 0;
     
          }
        }
        return i;
      }
     
      public static void updateFreq(int[] paramArrayOfInt1, int[] paramArrayOfInt2) {
        for (int i = 0; i < paramArrayOfInt1.length; i++)
          paramArrayOfInt2[paramArrayOfInt1[i]] += 1;
      }
     
      public static int findMax(int[] paramArrayOfInt) //find integer that occurs most often
      {
        int i = paramArrayOfInt[0];
        int j = 0;
     
        for (int k = 1; k < paramArrayOfInt.length; k++) {
          if (paramArrayOfInt[k] > i) {
            j = k;
            i = paramArrayOfInt[k];
          }
        }
        return j;
      }
     
      public static int findSequence(int[] paramArrayOfInt) { //check if the integers contain a continuous sequence of 5 or more
        int i = 0;
        for (int j = 0; j < paramArrayOfInt.length; j++) {
          if (paramArrayOfInt[j] != 0) {
            i++;
            if (i >= 5)
              return j - i + 1;
          }
          else
          {
            i = 0;
          }
        }
        return -1;
      }
    }

    I forgot to include the error I receive;

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Type mismatch: cannot convert from int to boolean
     
    	at Poker.checkRange(Poker.java:51)
    	at Poker.readCards(Poker.java:38)
    	at Poker.main(Poker.java:11)
    Last edited by omgar; April 12th, 2011 at 12:35 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Writing a very simple "Poker" game in Java

    boolean bool = false;
    bool = checkRange(paramArrayOfInt);

    bool is a boolean value, but checkRange(int[] i) returns an int.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing a very simple "Poker" game in Java

    So I convert the int into a boolean, correct?

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Writing a very simple "Poker" game in Java

    You can make checkRange() return a boolean instead of an int.. or you can change bool to an int depending on what you're trying to do.
    Remember to take while (!bool); into consideration when you make your decision.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

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

    Default Re: Writing a very simple "Poker" game in Java

    Can I return a boolean by replacing "return i;" with the following?
    boolean bool = (i != 0);
        return bool;

  6. #6
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Writing a very simple "Poker" game in Java

    Yeah, you can simply just say.. return (i != 0); aslong as the method type is set to return a boolean ofcourse.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing a very simple "Poker" game in Java

    Ah, okay, it works wonderfully now! Thanks!

  8. #8
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Writing a very simple "Poker" game in Java

    can you sent me correct all code for poker?

  9. #9
    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: Writing a very simple "Poker" game in Java

    No, no one here will send you code. Thread closed.

Similar Threads

  1. WHY THE CMD DOES NOT ABLE TO FIND "A" IN THIS SIMPLE PROGRM
    By rahul dixit in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 21st, 2011, 02:39 PM
  2. Replies: 13
    Last Post: October 13th, 2010, 11:20 AM
  3. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  4. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM