How do I stop my program from crashing when a non-numeric input is entered by the user? [duplicate]

I am making a simple program for class where the user starts with a 100 dollars and they make bets, and they either lose or win that amount based on whether they roll the same number on two dice. All of my code works and meets the requirements, but I can’t seem to handle invalid input for the bets. For example, if a word is entered, it still gives me the Input Mismatch Exception error. I have tried to do a try and catch, as well as the hasNextDouble as shown in my current code. Please help me figure this out!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import java.util.Scanner;
public class DoubleDice {
public static void main(String[] args) {
double playerMoney;
double playerBet;
boolean word = false;
Scanner scnr = new Scanner(System.in);
//player is given $100 to start
playerMoney = 100.00;
Die die1 = new Die();
Die die2 = new Die();
while (!word) {
//program loops until player is out of money
while ((int)playerMoney > 0) {
//player is told their current amount and asked to bet
System.out.print("You have $");
System.out.printf("%.2f", playerMoney);
System.out.print("nHow much would you like to bet (Enter 0 to quit)? ");
//this is where the issue is
if (scnr.hasNextDouble()) {
playerBet = scnr.nextDouble();
word = true;
} else {
System.out.println("Invalid input. Please enter a valid number.");
scnr.nextLine(); // Consume the invalid input
continue; // Go back to the beginning of the loop
}
//quits the game if player enters 0
if ((int)playerBet == 0) {
System.out.print("See you around, winner!");
break;
}
//prompts for a valid bet if negative bet is placed
else if ((int)playerBet < 0) {
System.out.println("Please enter a positive amount to bet!");
System.out.println("n");
continue;
}
//prompts for a valid bet if player bets more money than they have
else if (playerBet > playerMoney) {
System.out.println("Please enter a bet that is less than or equal to your current amount!");
System.out.println("n");
continue;
}
else {
//both dice are rolled and printed
die1.roll();
die2.roll();
System.out.println("You rolled a " + die1.toString() + " and " + die2.toString());
//checks whether dice are equal and prints amount won or lost
if (die1.equals(die2)) {
System.out.print("You win $");
System.out.printf("%.2f", playerBet);
playerMoney += playerBet;
System.out.println("n");
}
else {
System.out.print("You lose $");
System.out.printf("%.2f", playerBet);
playerMoney -= playerBet;
System.out.println("n");
}
}
}
}
//losing message printed if player runs out of money
if ((int)playerMoney <= 0) {
System.out.println("You are out of money!");
System.out.println("Better luck next time");
}
}
}
</code>
<code>import java.util.Scanner; public class DoubleDice { public static void main(String[] args) { double playerMoney; double playerBet; boolean word = false; Scanner scnr = new Scanner(System.in); //player is given $100 to start playerMoney = 100.00; Die die1 = new Die(); Die die2 = new Die(); while (!word) { //program loops until player is out of money while ((int)playerMoney > 0) { //player is told their current amount and asked to bet System.out.print("You have $"); System.out.printf("%.2f", playerMoney); System.out.print("nHow much would you like to bet (Enter 0 to quit)? "); //this is where the issue is if (scnr.hasNextDouble()) { playerBet = scnr.nextDouble(); word = true; } else { System.out.println("Invalid input. Please enter a valid number."); scnr.nextLine(); // Consume the invalid input continue; // Go back to the beginning of the loop } //quits the game if player enters 0 if ((int)playerBet == 0) { System.out.print("See you around, winner!"); break; } //prompts for a valid bet if negative bet is placed else if ((int)playerBet < 0) { System.out.println("Please enter a positive amount to bet!"); System.out.println("n"); continue; } //prompts for a valid bet if player bets more money than they have else if (playerBet > playerMoney) { System.out.println("Please enter a bet that is less than or equal to your current amount!"); System.out.println("n"); continue; } else { //both dice are rolled and printed die1.roll(); die2.roll(); System.out.println("You rolled a " + die1.toString() + " and " + die2.toString()); //checks whether dice are equal and prints amount won or lost if (die1.equals(die2)) { System.out.print("You win $"); System.out.printf("%.2f", playerBet); playerMoney += playerBet; System.out.println("n"); } else { System.out.print("You lose $"); System.out.printf("%.2f", playerBet); playerMoney -= playerBet; System.out.println("n"); } } } } //losing message printed if player runs out of money if ((int)playerMoney <= 0) { System.out.println("You are out of money!"); System.out.println("Better luck next time"); } } } </code>
import java.util.Scanner;

public class DoubleDice {
  public static void main(String[] args) {
    double playerMoney;
    double playerBet;
    boolean word = false;

    Scanner scnr = new Scanner(System.in);
    
    //player is given $100 to start
    playerMoney = 100.00;
    Die die1 = new Die();
    Die die2 = new Die();

    while (!word) {
      //program loops until player is out of money
      while ((int)playerMoney > 0) {
        
        //player is told their current amount and asked to bet
        System.out.print("You have $");
        System.out.printf("%.2f", playerMoney);
  
        System.out.print("nHow much would you like to bet (Enter 0 to quit)? ");
      
//this is where the issue is
        if (scnr.hasNextDouble()) {
          playerBet = scnr.nextDouble();
          word = true;
        } else {
          System.out.println("Invalid input. Please enter a valid number.");
          scnr.nextLine(); // Consume the invalid input
          continue; // Go back to the beginning of the loop
        }
    
        //quits the game if player enters 0
        if ((int)playerBet == 0) {
          System.out.print("See you around, winner!");
            break;
        }
  
        //prompts for a valid bet if negative bet is placed
        else if ((int)playerBet < 0) {
          System.out.println("Please enter a positive amount to bet!");
          System.out.println("n");
            continue;
        } 
          
        //prompts for a valid bet if player bets more money than they have
        else if (playerBet > playerMoney) {
          System.out.println("Please enter a bet that is less than or equal to your current amount!");
          System.out.println("n");
            continue;
        }
          
        else {
          //both dice are rolled and printed
          die1.roll();
          die2.roll();
          System.out.println("You rolled a " + die1.toString() + " and " + die2.toString());
          
          //checks whether dice are equal and prints amount won or lost
          if (die1.equals(die2)) {
            System.out.print("You win $");
            System.out.printf("%.2f", playerBet);
            playerMoney += playerBet;
            System.out.println("n");
          }
          else {
            System.out.print("You lose $");
            System.out.printf("%.2f", playerBet);
            playerMoney -= playerBet;
            System.out.println("n");
          }
        }
      }
    }

    //losing message printed if player runs out of money
    if ((int)playerMoney <= 0) {
      System.out.println("You are out of money!");
      System.out.println("Better luck next time");
    }
  }  
}

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật