Unclosed character class near index 525

2.6k views Asked by At

I'm a new programmer in general, so forgive me if this issue is a fair bit base.

This program's goal is a simple calculation of "optimal body weight," and continually throws an exception during runtime over the a and b string comparisons in Line 35. I've attempted removing the logic operators and that still does not seem to be the issue. Where am I wrong?

import java.util.*;

public class WeightCalc {

  //Initialize variables
  static int feet = 0, inches = 0, totalWeight = 0;
  static boolean isMale;

  public static void optimalWeight(){
    // Calculate optimal weight
    if (isMale == true){
      if (feet >= 5){
        totalWeight = 106 + 6*(inches);
      } else{
        System.out.print("Error, you're a midget.");
      }
    }
    if (isMale == false){  
      if (feet >= 5){
        totalWeight = 100 + 5*(inches);
      } else{
        System.out.print("Error, you're a midget.");
      }
    }
  }

  public static void main(String[] args){

    String a = "Male", b = "male";

    // Initialize kboard Scanner
    Scanner kboard = new Scanner(System.in);

    // Ask for gender and assign isMale
    System.out.println("What is your gender? ");
    String gender = kboard.nextLine();
    if (gender.equals(a) || gender.equals(b)){
      isMale = true;
    }else {
      isMale = false;
    }

    // Ask for height, first feet, then inches
    System.out.println("What is your height in regards to feet? ");
    kboard.nextInt(feet);
    System.out.println("What is your remaining h eight in inches? ");
    kboard.nextInt(inches);

    //Call optimalWeight method and run
    optimalWeight();

    // Print the output
    System.out.println("Your optimal weight should be " + totalWeight + ".");

    // Set isMale opposite to what it was before and calculate opposite sex's potential weight
    isMale = !isMale;
    optimalWeight();

    // Print the output of the second run
    System.out.println("If you were of the opposite sex, your weight would be " + totalWeight + ".");

    // Close the Scanner variable
    kboard.close();
  }
}
2

There are 2 answers

2
Andy Turner On

This line:

kboard.nextInt(feet);

should be

feet = kboard.nextInt();

When you provide an int parameter to Scanner.nextInt, it is considered the radix of the number you're going to enter. feet's value is zero, and you can't really have a radix zero number, hence the error.


Note that the error doesn't occur on the line you claim:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 525
(([-+]?(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))))|(((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))|(\Q-\E((((?i)[]|\p{javaDigit})++)|([\p{javaDigit}&&[^0]]((?i)[]|\p{javaDigit})?((?i)[]|\p{javaDigit})?(\,((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit})((?i)[]|\p{javaDigit}))+)))
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ^
    at java.util.regex.Pattern.error(Pattern.java:1955)
    at java.util.regex.Pattern.clazz(Pattern.java:2548)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.clazz(Pattern.java:2504)
    at java.util.regex.Pattern.sequence(Pattern.java:2063)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.group0(Pattern.java:2905)
    at java.util.regex.Pattern.sequence(Pattern.java:2051)
    at java.util.regex.Pattern.expr(Pattern.java:1996)
    at java.util.regex.Pattern.compile(Pattern.java:1696)
    at java.util.regex.Pattern.<init>(Pattern.java:1351)
    at java.util.regex.Pattern.compile(Pattern.java:1028)
    at java.util.Scanner$1.create(Scanner.java:367)
    at java.util.Scanner$1.create(Scanner.java:365)
    at sun.misc.LRUCache.forName(LRUCache.java:72)
    at java.util.Scanner.integerPattern(Scanner.java:443)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at WeightCalc.main(Main.java:45)

The penultimate line here:

java.util.Scanner.nextInt(Scanner.java:2117)

shows that it's in Scanner.nextInt.

0
Pineechio On

You're calling the wrong Scanner.readInt. You want to call the empty args one that returns an int.

The one you calling takes an int as an input that's supposed to be the number base (i.e. the number is in octal or binary, etc). Since feet and inches are both initialized to 0, it attempts to read an int in base 0, which is impossible.

What you want is something more like:

// Ask for height, first feet, then inches 
System.out.println("What is your height in regards to feet? ");
feet = kboard.nextInt();
System.out.println("What is your remaining h eight in inches? ");
inches = kboard.nextInt();