简体   繁体   English

此错误代码是什么意思? 线程“ main”中的异常java.util.InputMismatchException

[英]What does this error code mean? Exception in thread “main” java.util.InputMismatchException

My java code isnt working properly. 我的Java代码无法正常工作。 I keep getting this error message right after it asks the user to enter a R or P. What does it mean? 在要求用户输入R或P之后,我一直收到此错误消息。这是什么意思? How can I fix it? 我该如何解决?

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at PhoneBill3.main(PhoneBill3.java:17)

import java.util.Scanner;
public class PhoneBill3
{
   public static void main (String [] args)
   {
      double acctNum;
      int svcType=0;
      int dtmin=0;
      int ntmin=0;
      int dtFree=50;
      int ntFree=100;
      int minUsed=0;
      Scanner scan= new Scanner (System.in);
      System.out.print ("Please enter your account number: ");
      acctNum=scan.nextDouble();
      System.out.println ("Service type (R/P): ");
      svcType = scan.nextInt ();
      System.out.print("You entered " +svcType);

      //using switch to decide what to do with user input
      switch (svcType)
      {
         case 'R': 
         //if case R is entered, this should prompt the user to enter          
total minutes used and determin the cost of the Regular bill
            System.out.println ("Total minutes used: ");
            minUsed=scan.nextInt ();
            if (minUsed<50){
               System.out.println ("Account number: " + acctNum);
               System.out.println ("Account type: Regular");
               System.out.println ("Total minutes: " + minUsed);
               System.out.println ("Amount due: $15.00");}
            else{
               System.out.println ("Account number: " + acctNum);
               System.out.println ("Account type: Regular");
               System.out.println ("Total minutes: " + minUsed);
               System.out.println ("Amount due: $"+ 15 + (minUsed-    
50)*.2);}
            break;
         case 'P':
         //if case P is entered, this should prompt the user to enter     
day time and night time minutes used
            System.out.println ("Number of daytime minutes used: ");
            dtmin=scan.nextInt ();
            double dtTtlDue=0.00;
            System.out.println ("Number of nighttime minutes used: ");
            ntmin=scan.nextInt ();
            double ntTtlDue=0.00;
            dtTtlDue= (dtmin-dtFree)*.2;
            ntTtlDue= ((ntmin-ntFree)*.1);
            System.out.println ("Account number: " + acctNum);
            System.out.println ("Account type: Premium");
            System.out.println ("Daytime Min: "+ dtmin);
            System.out.println ("Nighttime Minutes: " + ntmin);
            System.out.println ("Amount due: $" + 25.00+ dtTtlDue + 
ntTtlDue);
            break;
       default:
            System.out.println ("That is not a valid service type. 
Enter R for regular or P for premium.");
            break;

       }

    }
}

I need for the final product to print the account number, service type, and depending on the service type, the number of minutes used or daytime and nighttime minutes. 我需要最终产品打印帐号,服务类型,并根据服务类型,使用的分钟数或白天和晚上的分钟数来打印。 Then based on the answers print what the total bill would be. 然后根据答案打印总账单。

This is happening because you are reading your R & P input as int .You should read it as String Please change the 发生这种情况是因为您正在将R & P输入读为int 。您应该将其读为String请更改

System.out.println ("Service type (R/P): ");
svcType = scan.nextInt ();

to

System.out.println ("Service type (R/P): ");
svcType = scan.next ().charAt(0);

You are asking the user to input a String value (of length 1) and attempting to assign it to an int variable (svcType). 您正在要求用户输入一个String值(长度为1),并试图将其分配给一个int变量(svcType)。 You need to make 2 changes: 您需要进行2项更改:

1) Change the type of the svcType variable to type char: 1)将svcType变量的类型更改为char类型:

char svcType = '?';

2) Extract only the first character of input from the users input: 2)仅从用户输入中提取输入的第一个字符:

scan.next().charAt(0);

You could also toUpperCase() the input to allow for lower case 'r' or s': 您也可以toUpperCase()输入以允许小写的“ r”或“ s”:

scan.next().toUpperCase().charAt(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 什么导致“线程中的异常”主“java.util.InputMismatchException”错误? - What causes the "Exception in thread "main" java.util.InputMismatchException" error? 线程“ main”中的异常java.util.InputMismatchException: - Exception in thread “main” java.util.InputMismatchException: 线程“主”java.util.InputMismatchException 中的异常 - Exception in thread "main" java.util.InputMismatchException 主线程中的异常-java.util.InputMismatchException - exception in thread main - java.util.InputMismatchException “线程“ main” java.util.InputMismatchException中的异常” ** - “Exception in thread ”main“ java.util.InputMismatchException”** Java 错误:“线程“main”java.util.InputMismatchException 中的异常” - Java error : "Exception in thread "main" java.util.InputMismatchException" 获取此错误 -&gt; Java 中线程“main”java.util.InputMismatchException 中的异常 - getting this error -> Exception in thread "main" java.util.InputMismatchException in Java 线程主java.util.InputMismatchException中的异常 - Exception in thread main java.util.InputMismatchException 线程* main *中的异常java.util.InputMismatchException - Exception in thread *main* java.util.InputMismatchException 线程主异常” java.util.InputMismatchException - Exception in thread main" java.util.InputMismatchException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM