can someone pls tell me what is the damn error here?!!

package t6q6;

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
       Scanner input = new Scanner (System.in);
       System.out.println("Please enter your account number.");
       int account = input.nextInt();
       System.out.println("Please enter P or R for Premium or Regular Service.");
       String temp = input.nextLine();
       char type = temp.charAt(0);
       double total_price;
       if (type == 'r' || type == 'R')
       {
           
       System.out.println("Please enter number of minutes you were using the system.");
       double minutes;
       minutes = input.nextDouble();
       
       if (minutes>50)
       {
           total_price = (minutes-50)*0.20 - 10;
       }
       else
       {
           total_price = 0;

       }
       if (total_price > 10)
       {
           total_price = total_price - 10;
       }
           else
       {
           total_price = 0;
       }
       
       System.out.println("Service Type : Regular");
       System.out.println("Account number : " + account);
       System.out.println("Number of minutes service was used : " + minutes);
       System.out.println("Total amount : " + total_price);
       }
       else if (type == 'p' || type == 'P')
       {
           System.out.println("Please enter the minutes you used the service from 6.00 A.M to 6.00 P.M");
           double minute_day = input.nextDouble();
           System.out.println("Please enter the minutes you used the service from 6.00 P.M to 6.00 A.M");
           double minute_night = input.nextDouble();
           if (minute_day > 75)
               total_price = (minute_day-75)*0.10 - 25;
           else
               total_price = 0;

           if (minute_night > 100)
               total_price = total_price + (minute_night-100)*0.05 - 25;

       System.out.println("Service Type : Premium");
       System.out.println("Account number : " + account);
       System.out.println("Number of minutes service was used : " + (minute_night+minute_day));
       System.out.println("Total amount : " + total_price);




       }
       else
           System.out.println("Please enter a valid character.");




    }
}

Recommended Answers

All 3 Replies

At first glance you need to make this change:

System.out.println("Please enter P or R for Premium or Regular Service.");
       String temp = input.next();

Instead of:

String temp = input.nextLine();

This could be other solutions

Hope it helps.

In addition to what moutanna said, you should be wary of bad user input. For example, before you call nextDouble() you should verify that there is actually a double present in the input by calling hasNextDouble(), which returns true if there is a double that can be read in. Oh, and the user could also enter something like "a whatever" and your program would read in the letter, then the nextDouble() call would choke on the next token of input, which is 'whatever' and you would get an InputMismatchException.

In addition to what moutanna said, you should be wary of bad user input. For example, before you call nextDouble() you should verify that there is actually a double present in the input by calling hasNextDouble(), which returns true if there is a double that can be read in. Oh, and the user could also enter something like "a whatever" and your program would read in the letter, then the nextDouble() call would choke on the next token of input, which is 'whatever' and you would get an InputMismatchException.

thank u, my problem is solved according to moutanna's post. but actually i didnt get your point about 'a whatever' , and i doont know how to use hasNextDouble.. should i use it after the line 10?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.