Making string insensitive to upper or lower case
I have two questions. I finally realized that if I just use ignore case under if parentheses it does work. But is it possible to acquire the same results using the switch statement?
My second question is for variables s1 and s4 . if i kept s1 = "South Bend". and when I entered in whatever case, it wouldn't work because of the space between south and bend. How do I fix that portion. Thank you for time and effort in resolving my issue.
import java.util.*;
import java.text.DecimalFormat;
public class TicketCalc {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
//Declare Stations
String s1 = new String("South"); //variable is actually South Bend
String s2 = new String("Dunes");
String s3 = new String("Gary");
String s4 = new String("East"); //variable is actually East Chicago
String s5 = new String("Hammond");
System.out.println("Enter one of the stops <South Bend, Dunes, Gary, East Chicago, or Hammond>: ");
//Input
String stop = s.next();
//arguements
double price = 0;
double single = 0;
double ten = 0;
double monthly = 0;
/*** switch (stop){
case "south":
single = 8.10;
ten = single*10*0.98;
monthly = single*30*0.95;
break;
case "dunes":
single = 7.30;
ten = single*0.98*10;
monthly = single*30*0.95;
break;
case "gary":
single = 4.60;
ten = single*0.98*10;
monthly = single*30*0.95;
break;
case "east":
single = 4.00;
ten = single*0.98*10;
monthly = single*30*0.95;
break;
case "hammond":
single = 3.75;
ten = single*0.98*10;
monthly = single*30*0.95;
break;
} ***/
//output
if (stop.equalsIgnoreCase(s1)) {
single = 8.10;
ten = single*10*0.98;
monthly = single*30*0.95;
System.out.println("\n\nFares from South Bend to Chicago are: ");
System.out.println("\nSingle - Ride Ticket: " +"$" +df.format(single));
System.out.println("Ten - Ride Ticket: " +"$" +df.format(ten));
System.out.println("Monthly pass: " +"$" +df.format(monthly));
System.out.println("\n");
}//end if
else if (stop.equalsIgnoreCase(s2)) {
single = 7.30;
ten = single*0.98*10;
monthly = single*30*0.95;
System.out.println("\n\nFares from South Bend to Chicago are: ");
System.out.println("\nSingle - Ride Ticket: " +"$" +df.format(single));
System.out.println("Ten - Ride Ticket: " +"$" +df.format(ten));
System.out.println("Monthly pass: " +"$" +df.format(monthly));
System.out.println("\n");
}//end if
else if (stop.equalsIgnoreCase(s3)) {
single = 4.60;
ten = single*0.98*10;
monthly = single*30*0.95;
System.out.println("\n\nFares from South Bend to Chicago are: ");
System.out.println("\nSingle - Ride Ticket: " +"$" +df.format(single));
System.out.println("Ten - Ride Ticket: " +"$" +df.format(ten));
System.out.println("Monthly pass: " +"$" +df.format(monthly));
System.out.println("\n");
}//end if
else if (stop.equalsIgnoreCase(s4)) {
single = 4.00;
ten = single*0.98*10;
monthly = single*30*0.95;
System.out.println("\n\nFares from South Bend to Chicago are: ");
System.out.println("\nSingle - Ride Ticket: " +"$" +df.format(single));
System.out.println("Ten - Ride Ticket: " +"$" +df.format(ten));
System.out.println("Monthly pass: " +"$" +df.format(monthly));
System.out.println("\n");
}//end if
else if (stop.equalsIgnoreCase(s5)) {
single = 3.75;
ten = single*0.98*10;
monthly = single*30*0.95;
System.out.println("\n\nFares from South Bend to Chicago are: ");
System.out.println("\nSingle - Ride Ticket: " +"$" +df.format(single));
System.out.println("Ten - Ride Ticket: " +"$" +df.format(ten));
System.out.println("Monthly pass: " +"$" +df.format(monthly));
System.out.println("\n");
}//end if
else { System.out.println("Incorrect Input. Try again");}//end else
}//end main
}//end class