1

I wasn't sure how to give a title for this problem, but basically this is part of my blackjack program. Also, since I did not know how to title this, I wasn't sure how to look it up, which is why I am asking here. So I am saying that when the user enters either 1 or 11 for the ace value, if they enter something other than 1 or 11, it asks the user again to put in 1 or 11. In my program everything works fine except when the user enters 1, then it just asks the question again. The program should only asks again if the input is not equal to 1 or 11. Here is my code as I made sure it always gives an ace for testing purposes:

String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1=="A"){
    System.out.println("Do you want a 1 or 11 for the Ace?: ");
    int player_ace_selection=input_var.nextInt();
    if ((1|11)!=(player_ace_selection)){
        System.out.println("Please enter a 1 or 11: ");
        int new_selection=input_var.nextInt();
        total=total + new_selection;
    }
    else {
        total=total + player_ace_selection;
    }
}
System.out.println(total);

Thanks in advance.

dat3450
  • 954
  • 3
  • 13
  • 27
Bob G.
  • 143
  • 3
  • 14
  • Hint: `.nextInt()` vs `.nextLine` -> the first doesn't read the new line `\n` – Yahya May 19 '17 at 00:57
  • There is no short form `OR`, you are doing a bitwise or on `1` and `11` at `(1|11)!=(player_ace_selection)` – Elliott Frisch May 19 '17 at 00:57
  • `card1 == "A"` is not the only problem here, and it's not the main one. Voting to reopen. – Sergey Kalinichenko May 19 '17 at 00:58
  • Also use `.equals()` instead of `==` for comparing Strings i.e. `card1` and `"A"` – Sash Sinha May 19 '17 at 00:58
  • See [best way to format multiple 'or' conditions in an if statement (Java)](http://stackoverflow.com/questions/7604814/best-way-to-format-multiple-or-conditions-in-an-if-statement-java) and [how do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Elliott Frisch May 19 '17 at 01:12

3 Answers3

3

The expression (1|11) uses binary OR, which produces 11:

    11 = 01001
     1 = 00001
(11|1) = 01001

Hence, the comparison is the same as 11!=player_ace_selection

You should change the code to use logical OR, i.e.

if (1!=player_ace_selection && 11!=player_ace_selection) {
    ...
}

In addition, you need to fix card1 == "A" comparison for card1.equals("A")

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

Instead of an If statement, try a while loop. A while loop ensures that your program waits for your user to pick the right answer. You also made a mistake with your logical operations. The correct way to use "OR" in this context is to compare your user input to both '1' and '11' separately using '||'.

    String card1="A";
    int total=0;
    Scanner input_var=new Scanner(System.in);
    if (card1.equals("A")){
        System.out.println("Do you want a 1 or 11 for the Ace?: ");
        int player_ace_selection=input_var.nextInt();

        while(player_ace_selection != 1 && player_ace_selection != 11){
            System.out.println("Do you want a 1 or 11 for the Ace?: ");
            player_ace_selection = input_var.nextInt();                
        }
        total += player_ace_selection;
    }

    System.out.println(total);
Rahul Chowdhury
  • 641
  • 1
  • 7
  • 16
0

There are some problems in your code, please consider this example and compare it with yours.

String card1="A";
int total=0;
Scanner input_var=new Scanner(System.in);
if (card1.equals("A")){ // compare the content not the reference (==)
   System.out.println("Do you want a 1 or 11 for the Ace?: ");
   try{ // wrap with try-catch block
       int player_ace_selection = Integer.parseInt(input_var.nextLine()); //read the entire line and parse the input
       if ((player_ace_selection!=1)&&(player_ace_selection!=11)){
             System.out.println("Please enter a 1 or 11: ");
             try{
                int new_selection = Integer.parseInt(input_var.nextLine()); //again read the entire line and parse the input
                total=total + new_selection;
             }catch(NumberFormatException e){
                   // do something to catch the error
             }
        }
        else {
             total=total + player_ace_selection;

        }

    }catch(NumberFormatException e){
             // do something to catch the error
    }
    System.out.println(total);

}
Yahya
  • 13,349
  • 6
  • 30
  • 42