1

I am trying my hand at Java and have not been formally trained at all. I picked up a assignment from a course somewhere and thought I would just try it. Everything is going well so far except I need to have a user make an input which is a selection from a given list. I have no issues dealing with the String values but they also have the option of choosing a number from 0-39 which I would like to be stored as an int.

Any help would be appreciated.

import java.util.Scanner;

public class RouletteGame {

    static Player p1;
    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);
        int num = (int)(Math.random()*37);
        int winnings = 0;
        int x = 0;
        int counter = 1;
        int p  = 1;
        String name;
        String choice;
        int iAmount = 100;
        int betNum;

        System.out.println("Welcome to Cache Creek style Rouylette..bet an amount and type");
        System.out.println("    if you select the correct colour, you win double your bet");
        System.out.println("    if you select the correct odd/even, you win double your bet");
        System.out.println("    if you select the correct number, you win 40 times your bet");
        System.out.println("    otherwise you lose your bet");
        System.out.println("If you lose all your money, the game is over");
        System.out.println();
        System.out.print("How much do you want to bet? $");
        int bet=scan.nextInt();

        while (x==0){
            Scanner scann = new Scanner(System.in);
            System.out.println("What is your bet? (odd/even/red/black/exact number)");
            choice=scann.nextLine();
            }

            if  (choice.equals("odd")){
                System.out.println("You have selected odd.");
                x=1;
                }
                else if (choice.equals("even")){
                        System.out.println("You have selected even.");
                        x=1;
                }
                else if (choice.equals("red")){
                        System.out.println("You have selected red.");
                        x=1;
                }
                else if (choice.equals("black")){
                        System.out.println("You have selected black.");
                        x=1;
                }
                else if (choice.equals(int)){
                    betNum = Integer.parseInt(choice);
                    System.out.println("You have selected " +betNum);
                    x=1;
            }
                else{
                    System.out.println("Invalid value: Must be odd, even, red, black or a number between 0 and 39");
                    x=0;
            }
        }
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • possible duplicate of [Determine if a String is an Integer in Java](http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java) – vefthym Nov 05 '14 at 13:44
  • use Integer.parseInt(String_input); to convert String to int – Chaitanya Gudala Nov 05 '14 at 13:45
  • @KrsnaChaitanya he already does that. The problem is in the line `else if (choice.equals(int)) {` – vefthym Nov 05 '14 at 13:46
  • yes vefthym, I know that is wrong.. just no idea what to put there – Scholarmate Nov 05 '14 at 13:47
  • I already commented on that. Follow this link to see the answer: http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java – vefthym Nov 05 '14 at 13:48
  • Again, I have read that and tried that but where do I put the public static boolean isInteger(String s) { try { Integer.parseInt(s); } catch(NumberFormatException e) { return false; } // only got here if we didn't return false return true; } – Scholarmate Nov 05 '14 at 13:50
  • OK. I'm writing an answer to make it more clear. Wait a few minutes – vefthym Nov 05 '14 at 13:50

1 Answers1

0

The problem is that you don't know if the given answer is a String or an Integer.

This line won't compile:

else if (choice.equals(int)) {

Instead, create a new method isInteger(String s), like this one and call it in the place of the line above, like that:

else if (isInteger(choice)) {
    betNum = Integer.parseInt(choice);
    if (betNum >= 0 && betNum <=39) {
       System.out.println("You have selected " +betNum);
       x = 1; //by the way, you can set x to be a boolean variable
    } else { //betNum not a valid Roulete number
       System.out.println("Invalid value: Must be odd, even, red, black or a number between 0 and 39");
       x=0;
    }
}
Community
  • 1
  • 1
vefthym
  • 7,422
  • 6
  • 32
  • 58