1

My java code isnt working properly. I keep getting this error message right after it asks the user to enter a R or P. What does it mean? 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.

3
  • What line of code does the error message point you towards? Usually InputMismatchExceptions are a problem with your Scanner, see this other question for a hint maybe! stackoverflow.com/questions/21143028/… Commented Feb 9, 2019 at 19:23
  • svcType = scan.nextChar (); Commented Feb 9, 2019 at 19:32
  • @ian any ideas? Commented Feb 9, 2019 at 19:42

2 Answers 2

0

This is happening because you are reading your R & P input as int.You should read it as String Please change the

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

to

System.out.println ("Service type (R/P): ");
svcType = scan.next ().charAt(0);
Sign up to request clarification or add additional context in comments.

10 Comments

for some reason if I do that I get these error msgs , ----jGRASP exec: javac -g PhoneBill3.java PhoneBill3.java:21: error: incompatible types: char cannot be converted to String if (svcType='R') ^ PhoneBill3.java:21: error: incompatible types: String cannot be converted to boolean if (svcType='R')
and if I make it a char i get this: PhoneBill3.java:17: error: cannot find symbol svcType = scan.nextChar (); ^ symbol: method nextChar() location: variable scan of type Scanner
You should only give R as input, not 'R' or "R"
When I do that it says cannot find symbol
you should take char input as svcType = scan.next ().charAt(0);
|
0

You are asking the user to input a String value (of length 1) and attempting to assign it to an int variable (svcType). You need to make 2 changes:

1) Change the type of the svcType variable to type char:

char svcType = '?';

2) Extract only the first character of input from the users input:

scan.next().charAt(0);

You could also toUpperCase() the input to allow for lower case 'r' or s':

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.