0

This is just a part of my code. The same structure goes for all 6 variables. (except level). Once read in, this prints back the input calculating the bonus. Bonus should be 0 at 10, cumulative +1 for each even number above 10 and -1 for each odd number below 10. How can i terminate if invalid input is entered. Do i have to use an if condition for all the inputs..

import java.util.Scanner;
 public class Game{
  public static void main(String[]args){
    Scanner sc = new Scanner(System.in);

    int bonus,x;
    double HP;

    System.out.print("Enter Str : ");
    int Str = sc.nextInt();

    System.out.print("Enter Dex : ");
    int Dex = sc.nextInt();

    System.out.print("Enter Con : ");
    int Con = sc.nextInt();

    System.out.print("Enter Int : ");
    int Int = sc.nextInt();

    System.out.print("Enter Wis : ");
    int Wis = sc.nextInt();

    System.out.print("Enter Cha : ");
    int Cha = sc.nextInt();

    System.out.print("Enter Level : ");
    int Level = sc.nextInt();

    System.out.println("\nLevel : "+Level);

    if ( Str == 10 ) {
        bonus = 0;
        System.out.println("Str : "+Str+"["+bonus+"]");
    }
        else if ( Str < 10 && Str % 2 == 0 ) {
            bonus = 0 ;
            for ( x = Str; x <= 10; x++ ) {
            if ( x % 2 != 0 ) {
                bonus = bonus + 1 ;             
            }
        }
         System.out.println("Str : "+Str+"[+"+bonus+"]");
        }
         else if ( Str < 10 && Str % 2 != 0 ) {
            bonus = 0 ;
            for ( x = Str; x <= 10; x++ ) {
            if ( x % 2 == 0 ) {
                bonus = bonus + 1 ;             
            }
        }
         System.out.println("Str : "+Str+"[-"+bonus+"]");
        }
        else if ( Str > 10 && Str % 2 == 0 ) {
            bonus = 0 ;
            for ( x = 10; x <= Str; x++ ) {
            if ( x % 2 != 0 ) {
                bonus = bonus + 1 ;             
            }
        }
         System.out.println("Str : "+Str+"[+"+bonus+"]");
        }
        else if ( Str > 10 && Str % 2 != 0 ) {
            bonus = 0 ;
            for ( x = 10; x <= Str; x++ ) {
            if ( x % 2 == 0 ) {
                bonus = bonus + 1 ;             
            }
        }
         System.out.println("Str : "+Str+"[-"+bonus+"]");
        }
Salem
  • 13,516
  • 4
  • 51
  • 70
DDSasikala
  • 43
  • 1
  • 7

3 Answers3

0

What you are looking for is:

System.exit();

Here is a good link

0
int Str = sc.nextInt();

This line will throw an exception if the user inputs a string that cannot be parsed as an integer. If you do not catch this exception, the program will print the traceback and abort.

The same thing will happen for each of your other attempts to read an integer. If you do successfully read the integers, then you just need to test for the correct values, and, if not correct, as others have said, call System.exit() to quit

FredK
  • 4,094
  • 1
  • 9
  • 11
  • I'm actually still learning java. So it's still a little bit hard for me to understand..Any way, i keep getting compilation errors when adding System.exit() to quit. – DDSasikala Feb 22 '18 at 20:04
0

There are some things you can improve in your code.

You must know that scanner has to be closed after execution, but happily it implements Closeable interface, so it will be automatically closed if you open it in try, as described here: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

sc.nextInt() will throw InputMismatchException when input is not a valid integer, so you can capture it and exit.

Basically you can write your code within try-catch clause as follows:

    try (Scanner sc = new Scanner(System.in)){

        int bonus,x;
        double HP;

        System.out.print("Enter Str : ");
        int Str = sc.nextInt();

        // here the rest of your code ...

    } catch (InputMismatchException e) {
        System.err.println("Error processing inputs, exiting... ");
        System.exit(-1);
    } 
    // after try/catch block scanner is closed
carlosvin
  • 979
  • 9
  • 22
  • oook.... i'm getting 100 compilation errors after typing this code..Is it possible to do this from using a while loop? – DDSasikala Feb 22 '18 at 20:48
  • You get compilation errors because I posted a fragment of code that goes within main method. You don't have to copy and paste it, you can use it as guide and modify your code adding this try catch structure – carlosvin Feb 22 '18 at 21:33