线程“main”中的异常java.util.regex.PatternSyntaxException:

时间:2016-03-03 18:43:05

标签: java

刚进入java,绝对是初学者。使用netbeans IDE8.1

这是一个简单的程序,我从文本中逐字输入,构建了'ok':

package volume2;

import java.util.Scanner;


public class Volume2 {

  public static void main(String[] args) 
  {
    final double BOTTLE_VOLUME = 2;
    final double LITER_PER_OUNCE = 0.0296;
    final double CAN_VOLUME = 12 * LITER_PER_OUNCE;

    System.out.print("Please enter the number of bottles: ");

    Scanner in = new Scanner(System.in);
    int bottles = in.nextInt(0);

    double totalVolume = bottles * BOTTLE_VOLUME;

    System.out.print("Please enter the number of cans: ");
    int cans = in.nextInt(0);

    double additionalVolume = cans * CAN_VOLUME;

    totalVolume = totalVolume + additionalVolume;

    System.out.print("Total volume: ");
    System.out.println(totalVolume);

    // TODO code application logic here
}

}

但是,这是输出:

运行:

  

线程“main”中的异常java.util.regex.PatternSyntaxException:   索引525附近的未闭合字符类(等...)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ^在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)       等....

(我知道我没有正确格式化运行文本和代码,所以如果在这些论坛上有关于如何正确分步的任何链接,请告诉我。“我如何发布。 ..“没有指定如何发布运行输出。”

1 个答案:

答案 0 :(得分:0)

https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt()

方法nextInt()是一个重载方法,一个不带参数的形式,一个形式采用整数参数,该参数是正在读取的数字的基数。您在代码中使用后者并为其提供基数值0,这是无效的。我假设您在基数10中输入数字,所以您也可以使用无参数形式。

    int bottles = in.nextInt();
    ...
    int cans = in.nextInt();