Struggling on IF conditions

65 Views Asked by At

What the problem I want to ask is I cannot make the E part, but the V one is working and I tried all I could to figure it out what is going on in my logic... Please help me out to make the 'E' works.

import java.util.Scanner;

public class Service {

    public static void main(String args[]) {
        String service;
        float monthV, usaV, ausV, rusV, callV;
        float monthE, usaE, ausE, rusE, callE;

        Scanner input = new Scanner(System.in);

        System.out.print("Which service did you use for the calls?<V - Vartec, E - Eircom> :   ");
        service = input.nextLine();

        if (service.charAt(0) != 'E')
            if (service.charAt(0) != 'V') {
                System.out.println("Thank you for your time...good bye.");
            } else {
                if (service.charAt(0) == 'V') {
                    System.out.print("\nPlease enter the total number of calls made in the month:   ");
                    monthV = input.nextFloat();

                    System.out.print("\nPlease enter the number of minutes spent calling the USA:   ");
                    usaV = input.nextFloat();
                    System.out.print("\nPlease enter the number of minutes spent calling the Australia:   ");
                    ausV = input.nextFloat();
                    System.out.print("\nPlease enter the number of minutes spent calling the Russia:   ");
                    rusV = input.nextFloat();
                    callV = ((usaV * 0.06f) + (ausV * 0.08f) + (rusV * 0.24f));
                    System.out.println("The total cost of using the Vartec service for the month is"
                            + String.format("%.2f", callV));
                } else {
                    if (service.charAt(0) == 'E') {
                        System.out.print("\nPlease enter the total number of calls made in the month:   ");
                        monthE = input.nextFloat();

                        System.out.print("\nPlease enter the number of minutes spent calling the USA:   ");
                        usaE = input.nextFloat();
                        System.out.print("\nPlease enter the number of minutes spent calling the Australia:   ");
                        ausE = input.nextFloat();
                        System.out.print("\nPlease enter the number of minutes spent calling the Russia:   ");
                        rusE = input.nextFloat();
                        callE = ((usaE * 0.19f) + (ausE * 0.85f) + (rusE * 0.92f));
                        System.out.println("The total cost of using the Vartec service for the month is"
                                + String.format("%.2f", callE));
                    }
                }
            }
    }
4

There are 4 best solutions below

1
user85421 On BEST ANSWER

simple way

char ch = service.charAt(0);

if (ch == 'E') {
   // do E
} else if (ch == 'V') {
    // do V
} else {
    // do neither E nor V - that is, BYE
}

a bit more advanced (easier to understand with more options):

char ch = service.charAt(0);

switch (ch) {
    case 'E':
    case 'e':  // also handle lowercase
        // do E
        break;
    case 'V':
    case 'v':
        // do V
        break;
    // more options if needed
    default:
        // do neither of the above
        break;
}

Your problem is doing something like

if (ch != 'E') {
    // something else
    if (ch == 'E') {
        // will never enter here
     }
}

Note: you can also use a switch with strings (unless using very old java version):

// changed to uppercase, we don't mind the case of input
switch (service.toUpperCase()) {
    case "VARTEC":
        ....
    case "EIRCOM":
        ...
    default:
        // I don't understand the user!
        ...
}
0
AudioBubble On

I assume you refer to this if

if (service.charAt(0)=='E')

if yes, this one is never true because is nested in this one

if (service.charAt(0)!='E')
0
AudioBubble On

There is no need for nested if statements in your code at all.

A simple

if(service.charAt(0)=='V') {
     // First char is V
} else if (service.charAt(0)=='E') {
     // First char is E
} else {
     // First char is neither V nor E
}

will work just fine.

0
mohd shoaib On

So switch and else-if approach would work and would be simple to debug and read. But to know the issue in the question asked is that: if (service.charAt(0) != 'E') does not have a matching else.

So suppose the cases referring to the condition if (service.charAt(0) != 'E'):

case 1. When you input V the above condition becomes true and the flow goes in this if and the execution flows as expected for V.

case 2. When you input E , then above condition becomes false as the input is indeed E - so the flow does not goes in this if and the code written for E never gets executed(since its written in if inside which the flow does not goes), And since we do not have any else part also hence the program would terminate.

So if you are inclined to use if only and not switch then to correct the implementation you should replace if (service.charAt(0) != 'E') with

if (service.charAt(0) != 'E' && service.charAt(0) != 'V'){
            System.out.println("Thank you for your time...good bye.");
        } else {
             ... rest of the code as usual  
        }