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));
}
}
}
}
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!
...
}