Java' java.util.InputMismatchException'编译时出错

时间:2018-06-01 06:17:33

标签: java debugging

我对Java很陌生。我有这个任务,我应该编写一个计算客户每月账单的程序。它应该要求用户输入客户购买的包裹的字母(A,B或C)以及使用的分钟数。然后它应显示总费用。

规格如下:

  

互联网服务提供商为其客户提供三种不同的订阅套餐:

     

套餐A:每月39.99美元,提供450分钟的访问权限。额外的分钟是每分钟0.45美元。套餐B:每月59.99美元,提供900分钟的访问时间。额外的时间是每分钟0.40美元。套餐C:每月69.99美元,无限制使用。

这是我的代码:

 import java.util.Scanner;

    public class assignment2
    {

        public static void main(String[] args)
        {
    //declaration section 

    double AddTime;
    double Charges;
    double Saving;
    double Bill;
    int Subscription;
    int TimeMin;

    Scanner input = new Scanner (System.in);
    //ask user subscription
    System.out.println("Please enter you subscription (A,B, or C) ");
    Subscription = input.nextInt ();

    //if user choses subscription A
     switch (Subscription)
    {
            case 'a':
                    case 'A':
    System.out.println("Enter amount of minutes spent this month ");
    TimeMin = input.nextInt ();
        if (TimeMin < 450)
        {
        AddTime = TimeMin - 450 ;
        Charges = AddTime * 0.45;
        System.out.println("The charges are: $" + Charges);
        Bill = 39.99 + Charges;
        System.out.println("Your bill this month will be: $" + Bill);
        System.out.println(" ");
    //mention the user the savings with other subscriptions
    Saving = Bill - 59.99;
    System.out.println("With package B you would have saved: $" + Saving);
    Saving = Bill - 69.99;
    System.out.println("With package C you would have saved: $" + Saving);

        }
        else
        {System.out.println("Your bill is $39.99 this month ");}
                              break;
                    case 'b':
                    case 'B':
    // ask user for the amount of minutes used
    System.out.println("Enter amount of minutes spent this month ");
    TimeMin = input.nextInt();
        if (TimeMin < 900)
        {
        AddTime = TimeMin - 900;
        Charges = AddTime * 0.40;
        System.out.println("The charges are: $" + Charges);
        Bill = 59.99 + Charges;
        System.out.println("Your bill this month will be: $" + Bill);
        System.out.println(" ");
    //mention the user the savings with other subscriptions
    Saving = Bill - 69.99;
    System.out.println("With package C you would have saved: $" + Saving);

        }
        else
        {System.out.println("Your bill is $59.99 this month ");}
                break; 

                    case 'c':
                    case 'C':   
    // ask user for the amount of minutes used
        System.out.println("Enter amount of minutes spent this month ");
        TimeMin = input.nextInt();
        System.out.println("Your bill is $69.99 this month you have unlimited access to our service!");

    break;
    default:
    System.out.println("That is not A, B, or C!");  
    }

    System.out.println("Thanks for using our service!");
    }

    }

一旦我尝试编译它就给了我这个错误:

Exception in thread "main" java.util.InputMismatchException
        at java.util.Scanner.throwFor(Scanner.java:864)
        at java.util.Scanner.next(Scanner.java:1485)
        at java.util.Scanner.nextInt(Scanner.java:2117)
        at java.util.Scanner.nextInt(Scanner.java:2076)
        at assignment2.main(assignment2.java:21)

我还没有解决它。知道如何修复我的代码吗?

2 个答案:

答案 0 :(得分:0)

您应该将input.nextInt();更改为input.nextLine();,将int Subscription更改为String Subscription,将'更改为"中的switch case

答案 1 :(得分:0)

抛出Exception的主要原因是您尝试从 InputStream (在您的情况下为stdin / System.in)中读取整数值
但是,由于您提示用户输入A,B或C,因此他很可能会输入其中一个字符

根据文档Scanner#nextInt()尝试将给定的 String 解析为整数。这意味着像#&#34; 526&#34;将返回一个整数526.在这种情况下,A,B或C应该代表什么?答案很简单。这些值不在0-9之间,因此在运行时抛出InputMismatchException表示无法解析输入。

解决此类问题的可能性很多。要保持当前的开关盒结构,您可以使用以下内容:

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

    System.out.println( "Enter A,B or C" );

    // read from console
    String rawInput = sc.nextLine();
    // remove unneccessary whitespaces.
    String trimmedInput = rawInput.trim();
    // assure that our string was not only containing whitespaces.
    if ( trimmedInput.length() > 0 )
    {
      // assign the value
      char value = trimmedInput.charAt( 0 );
      switchInputValue( value );
    }
  }

  private static void switchInputValue( char value )
  {
    switch ( value )
    {

      case 'a':
      case 'A':
        doSomethingForA();
        break;

      case 'b':
      case 'B':
        doSomethingForB();
        break;
      default:
        // get creative here
        // example:
        System.out.println("Something went wrong. - Wong input value.");
        break;

    }
  }
}

注意:从Java 7开始,我们可以直接切换字符串,但如果你不能在你的情况下做出很大的改变。为了完善,下面的代码片段与上面完全相同但没有获得单个 char值

public class Main
{
  public static void main( String[] args )
  {
    Scanner sc = new Scanner( System.in );
    System.out.println( "Enter A,B or C" );
    String rawInput = sc.nextLine();
    String trimmedInput = rawInput.trim();
    if ( trimmedInput.length() > 0 )
    {
      // switching our String
      switchInputValue( trimmedInput );
    }
  }

  private static void switchInputValue( String value )
  {
    switch ( value )
    {

      case "a":
      case "A":
        doSomethingForA();
        break;

      case "b":
      case "B":
        doSomethingForB();
        break;
      default:
        doSomethingIfEverythingElseFails();
        break;
    }
  }
}

正如您所看到的,除了参数类型od之外没有太大差异,方法已更改为String value amd,案例标有" "而不是' ' 。仍然因为你将一个字符排除在iput之外,我会考虑第一种方法而不是第二种方法。

希望这对您的问题有帮助。