0

I'm trying to call in methods to do different things as you can see. When I select choice 1, I have no problems but when I choose choice 2, ("2. Calculate consulting charges"); in the debugger after the code is ran it gives me an error at userInput = input.nextLine().charAt(0);. When I choose ("1. Compare Strings"); it will run and allow me to make another choice which is what I want until choice 3 is entered. So if user enters 3 I want the program to end. If you look at my while loop I put (cLoop == '3'). Is that also a correct way of running this. Thanks for all your input in advance. It really does help someone further there knowledge.

    public class main {
        public static void main(String[] args) {

        // Create Scanner object
        Scanner input = new Scanner(System.in);

        char userInput;
        String intro;
        double income = 0;
        double time = 0;
        double rate= 0;
        char cLoop = '3';

        while (cLoop == '3') {

            getChoice();
            System.out.print("Number: ");
            userInput = input.nextLine().charAt(0);

            if (userInput == '1') {
                int value = 0;
                System.out.print("Enter your first string: ");
                String s1 = input.nextLine();
                System.out.print("Enter your second string: ");
                String s2 = input.nextLine();
                value = compareStrings(s1, s2);
                if (value == -9){
                    System.out.println("These strings are equal");
                } else if (value == 9) {
                    System.out.println("The first string is GREATER than the second string");
                } else {
                    System.out.println("The first string is LESS than the second string");
                }

            } else if (userInput == '2') {
                System.out.println("Enter income: ");
                income = input.nextInt();
                System.out.println("Enter time in minutes: ");
                time = input.nextInt();
                System.out.println("Enter hourly rate: ");
                rate = input.nextInt();
                int x = findFees(income, time, rate);
                System.out.println(x);

            } else if (userInput == '3') {
                System.out.println("Thank you. Goodbye!");
                return;

            } else {
                System.out.println("Invalid choice\n");
            }
        }
    }

    public static void getChoice() {

        System.out.println("1. Compare Strings");
        System.out.println("2. Calculate consulting charges");
        System.out.println("3. End the program");
    }

    public static int compareStrings(String s1, String s2) {
        int b = 0;
        if (s1.equalsIgnoreCase(s2)) {
            b = -9;
        } else if (s1.compareToIgnoreCase(s2) > 0) {
            b = 9;
        } else {
            b = 10;
        }
        return b;
    }

    public static int findFees(double income, double time, double rate) {
        double charges = 0;
        if (income <= 25000 && time < 30) {
            System.out.println("No Charge.");
            }
        else {
            charges = (double) ((time - 30) / 60 * .4 * rate);

        }

        return (int) charges;
    }
}
Gaurav Jeswani
  • 4,134
  • 6
  • 23
  • 41
ripsly25
  • 19
  • 3

0 Answers0