-2

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();
  }
}
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
Under_Scored
  • 1
  • 1
  • 2
  • Which is line 35? `if (gender.equals(a) || gender.equals(b)){`? And what is the exception? – Andy Turner Sep 16 '16 at 18:43
  • 1
    Don't compare booleans using `a == true` or `a == false`. Just use `a` or `!a`. – Andy Turner Sep 16 '16 at 18:45
  • http://stackoverflow.com/questions/21816788/unclosed-character-class-error – Wiktor Stribiżew Sep 16 '16 at 18:45
  • Try initializing a and b on two separate lines. `String a = "Male"; String b = "male";` – Hypnic Jerk Sep 16 '16 at 18:46
  • and why test for two different strings? Why not just have `male` and to a `toLower()` on the input string? Otherwise you'll simply have `mAlE` become isMale = false – Marc B Sep 16 '16 at 18:47
  • @AndyTurner Yes, that'd be the line. The exception is as follows: Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 525 – Under_Scored Sep 16 '16 at 18:49
  • 1
    Check your line number again, because there is **no** regex processing in the line `if (gender.equals(a) || gender.equals(b))`. More likely, it's in the `kboard.nextInt(feet)` line, which should be `feet = kboard.nextInt()`. – Andreas Sep 16 '16 at 18:55
  • 1
    I think this is actually a bug in OpenJDK - not so much the behaviour, but the cryptic error. It seems like the [`setRadix`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Scanner.java#Scanner.setRadix%28int%29) method could have a check for `radix < 2`. But then, it's probably not the common a problem. – Andy Turner Sep 16 '16 at 19:15
  • 1
    @AndyTurner `Integer.parseInt(String s, int radix)` defines that it will throw `NumberFormatException` for a bad radix value, and `nextInt(int radix)` defines that it will call `parseInt()`, so they were probably relying on its validation, not realizing that the regex is going bad for a `0` radix *before* it gets to call `parseInt()`. – Andreas Sep 16 '16 at 19:22
  • I've submitted a bug report. – Andy Turner Sep 16 '16 at 19:30
  • This issue has been accepted by Oracle as [bug JDK-8166261](http://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8166261). – Andy Turner Sep 19 '16 at 10:08

2 Answers2

2

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.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Wouldn't the important line be `WeightCalc.main(Main.java:45)`, indicating that it is line **45** that's the culprit? – Andreas Sep 16 '16 at 19:04
  • @Andreas well, sure, but you can see that it's a problem with the call to `nextInt` without looking at the line numbers. – Andy Turner Sep 16 '16 at 19:07
0

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();
Pineechio
  • 371
  • 1
  • 9