1

So basically I'm creating a betting program where Player1, Player2, Player3 etc. are objects and each have five initialized fields of -1, which means there is no bet, yet.

I'm asking the users to input their bet number, (these numbers are integers inside of an array) and they can have up to a maximum of five bets, explaining the five initialized fields of -1.

The problem here is I can't seem to find a way to have certain players only input 1 or 2 bets, while another inputs 4 and another 5 for example. My program forces each user to enter 5 integers EVEN if they refuse to bet 5 times. Any way I can make this work?

So here's my driver class: (the other parts of it are irrelevant)

System.out.println("Wheel : 0-32-15-19-4-21-2-25-17-34-6-27-13-36-11-30-8-23-10-5-24-16-33-1-20-14-31-9-22-18-29-7-28-12-35-3-26"); 
        System.out.println("Dealer…Place your bets");

        // initializing the six players of the game

        Player player1 = new Player(-1,-1,-1,-1,-1);
        System.out.println("Player 1: ");
            int number1 = keyin.nextInt();
            int number2 = keyin.nextInt();
            int number3 = keyin.nextInt();
            int number4 = keyin.nextInt();
            int number5 = keyin.nextInt();
                System.out.println("");

        Player player2 = new Player(-1, -1, -1, -1, -1);
        System.out.println("Player 2: ");
            number1 = keyin.nextInt();
            number2 = keyin.nextInt();
            number3 = keyin.nextInt();
            number4 = keyin.nextInt();
            number5 = keyin.nextInt();
                System.out.println("");

        Player player3 = new Player(-1, -1, -1, -1, -1);
        System.out.println("Player 3: ");
            number1 = keyin.nextInt();
            number2 = keyin.nextInt();
            number3 = keyin.nextInt();
            number4 = keyin.nextInt();
            number5 = keyin.nextInt();
                System.out.println("");

        Player player4 = new Player(-1, -1, -1, -1, -1);
            System.out.println("Player 4: ");
            number1 = keyin.nextInt();
            number2 = keyin.nextInt();
            number3 = keyin.nextInt();
            number4 = keyin.nextInt();
            number5 = keyin.nextInt();
                System.out.println("");

        Player player5 = new Player(-1, -1, -1, -1, -1);
            System.out.println("Player 5: ");
            number1 = keyin.nextInt();
            number2 = keyin.nextInt();
            number3 = keyin.nextInt();
            number4 = keyin.nextInt();
            number5 = keyin.nextInt();
                System.out.println("");

        Player player6 = new Player(-1, -1, -1, -1, -1);
            System.out.println("Player 6: ");
            number1 = keyin.nextInt();
            number2 = keyin.nextInt();
            number3 = keyin.nextInt();
            number4 = keyin.nextInt();
            number5 = keyin.nextInt();
                System.out.println("");

And here is my other class for players:

public class Player {

int number1;
int number2;
int number3;
int number4;
int number5;
// constructor for each player at the table
public Player(int number1, int number2, int number3, int number4, int number5) {

    this.number1 = number1;
    this.number2 = number2;
    this.number3 = number3;
    this.number4 = number4;
    this.number5 = number5;

The number1, number2 ... represent the five bets.

Also to clarify, the user can't be asked to input a certain String, int, char whatsoever to let the program know he/she's done inputting their bets. It has to look like this

Player 1: 12 13 15
Player 2: 0 5 4
X1XX
  • 141
  • 2
  • 13
  • 2
    Have you learned about lists yet? (or arrays) – user253751 Feb 11 '15 at 02:43
  • Assuming keyin is a `java.util.Scanner Object`, the program is always asking for five numbers because you're making the program ask for five numbers. Instead of `nextInt()` do a `nextLine()` and parse the string into integer using `Integer.parseInt()` if the input to `nextLine()` is only `\n` (return key) assume it to be no bet and go to next player. Or establish you're own convention like, if the user types in "no more" or something. Then match you're input against it at each input. – anu Feb 11 '15 at 02:58
  • @anu How exactly would I use this interger.parseInt()? Quite a newbie at Java! Thanks for helping out – X1XX Feb 11 '15 at 03:16

4 Answers4

2

Follow up of your comment Patrick. This is how you'd use Integer.parseInt

...    
Player player2 = new Player(-1, -1, -1, -1, -1);
System.out.println("Player 2: ");
 try {
                number1 = Integer.parseInt(keyin.nextLine());
                number2 = Integer.parseInt(keyin.nextLine());
                number3 = Integer.parseInt(keyin.nextLine());
                number4 = Integer.parseInt(keyin.nextLine());
                number5 = Integer.parseInt(keyin.nextLine());
                System.out.println("");
    }
    //Code will go to this block when user entered something other than integers
    catch(NumberFormatException e) {
        System.out.println("Done accepting bets from player2");
    }
    ...

Alternatively, could store the output of keyin.nextLine() into a string and apply parseInt or evaluate it for some "keyword" you wanted like so.

String keyedinString = keyin.nextLine();
if(keyinString.equalsIgnoreCase("done betting") {
    throw new Exception();
}

You'll soon realize how repetitive this who process is. Therefore, as others have told suggested consider using arrays and for-loops for your task. Like so.

int[] player1Bets = new int[5];
for(int i=0;i<5;i++) {
   try {
      player1Bets[i] = Integer.parseInt(keyin.nextLine());
   }
   catch(NumberFormatException e) {
       System.out.println("Player1 took" + i + "bets. he is done betting");
   }
}
...

So that, instead of number1, number2, number3, number4... etc you'll have the first bet in player1Bets[0], second bet in player1Bets[1] ... Much easier to type out, and code.

Hope things are clear.

anu
  • 1,017
  • 1
  • 19
  • 36
  • Thank you! Btw I implemented your code above, with the array and the try-catch, but for some reason the same SOP keeps appearing although my catch is different for each player...? – X1XX Feb 11 '15 at 04:21
  • SOP ? Please explain ? – anu Feb 11 '15 at 07:05
  • System.out.println @anu – X1XX Feb 11 '15 at 07:25
  • Did you change the string inside sysout (SOP) for each catch block ? Sounds to me that you didn't – anu Feb 11 '15 at 07:27
  • I did, somehow i figured out it was the for loop causing the problem, which number should i switch the i for in player1Bets[i]? – X1XX Feb 11 '15 at 07:32
  • I think you should edit your original post append your updated code. That way we both will be clear about what you did. What problem you're facing. – anu Feb 11 '15 at 22:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70753/discussion-between-patrick-bui-and-anu). – X1XX Feb 12 '15 at 02:54
0

There are many ways you can do that:

  • Use 1,2,3,4,5 parameters constructors instead if just one with 5 parameter.
  • The better way is to make use of BUilder pattern because later you might need to place more than 5 bets so instead of keep on adding constructors this is a better option.
  • You can also make use of variable arguments. See When do you use varargs in Java?
  • Like @immibs said you can also have like a member variable list which you can then add to as many as you want
Community
  • 1
  • 1
Vivin
  • 1,327
  • 2
  • 9
  • 28
0

You can always loop your input and designate a certain number for the loop to terminate. For example; the bets are 1, 2 and 3 and to stop it from there, the user can input 0 then exit the loop or the program can ask the user everytime whether they wanted to place another bet or not. For example,

Place Bet: 1

Do you want to place another bet? Press 1 for yes and 0 for no

If this question is more on the logic part, then this may help you. I do hope I am of help, I am new here and I tried to answer questions to improve my skills and experience. :)

MAC
  • 676
  • 3
  • 13
  • 32
0

You may need "Builder Pattern" if you want to pass multiple parameters and the count of parameters is not fixed. Please see https://jlordiales.wordpress.com/2012/12/13/the-builder-pattern-in-practice/ for more info about "Builder Pattern".

Here is an example:

If you have a User class with multiple parameters,

public class User {
  private final String firstName; // required
  private final String lastName; // required
  private final int age; // optional
  private final String phone; // optional
  private final String address; // optional

  public static class UserBuilder {
    private final String firstName;
    private final String lastName;
    private int age;
    private String phone;
    private String address;
    public UserBuilder(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
    public UserBuilder withAge(int age) {
      this.age = age;
      return this;
    }
    public UserBuilder withPhone(String phone) {
      this.phone = phone;
      return this;
    }
    public UserBuilder withAddress(String address) {
      this.address = address;
      return this;
    }
    public User build() {
      // if(age < 0) { ... } return new User(this);
      // not thread-safe, a second thread may modify the value of age
      User user = new User(this);
      if (user.getAge() < 0) {
        throw new IllegalStateException("Age out of range!"); // thread-safe
      }
      return user;
    }
  }

  private User(UserBuilder builder) {
    this.firstName = builder.firstName;
    this.lastName = builder.lastName;
    this.age = builder.age;
    this.phone = builder.phone;
    this.address = builder.address;
  }

  public String getFirstName() {
    return firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public int getAge() {
    return age;
  }
  public String getPhone() {
    return phone;
  }
  public String getAddress() {
    return address;
  }
}

Use following way to create a new User instance:

User user1 = new User.UserBuilder("Jhon", "Doe")
                    .withAge(30)
                    .withPhone("1234567")
                    .withAddress("Fake address 1234")
                    // maybe more parameters
                    .build();
User user2 = new User.UserBuilder("Tom", "Jerry")
                    .withAge(28)
                    // maybe more parameters
                    .build();
coderz
  • 4,847
  • 11
  • 47
  • 70