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.

Jessi Kruger
  • 29
  • 1
  • 3

2 Answers2

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);
Ratish Bansal
  • 1,197
  • 5
  • 17
  • 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') – Jessi Kruger Feb 09 '19 at 19:35
  • 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 – Jessi Kruger Feb 09 '19 at 19:37
  • You should only give R as input, not 'R' or "R" – Ratish Bansal Feb 09 '19 at 19:46
  • When I do that it says cannot find symbol – Jessi Kruger Feb 09 '19 at 19:50
  • you should take char input as svcType = scan.next ().charAt(0); – Ratish Bansal Feb 09 '19 at 19:50
  • If I declare the R and P as a string variable it says String cannot convert to boolean. – Jessi Kruger Feb 09 '19 at 19:52
  • Don't do anything just change it to svcType = scan.next ().charAt(0); – Ratish Bansal Feb 09 '19 at 19:53
  • So I did what you said and now it gives me this: PhoneBill3.java:20: error: incompatible types: boolean cannot be converted to char if (svcType=R) ^ PhoneBill3.java:20: error: incompatible types: char cannot be converted to boolean if (svcType=R) ^ PhoneBill3.java:35: error: incompatible types: boolean cannot be converted to char if (svcType=P); ^ PhoneBill3.java:35: error: incompatible types: char cannot be converted to boolean if (svcType=P); – Jessi Kruger Feb 09 '19 at 19:54
  • its running property in my machine.What all have you changed? Just replace svcType=scan.nextInt() to svcType = scan.next ().charAt(0); – Ratish Bansal Feb 09 '19 at 19:55
  • please kindly upvote and mark the answer as verified, if this worked for you – Ratish Bansal Feb 09 '19 at 20:08
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);
pczeus
  • 7,195
  • 4
  • 34
  • 50