0

hi i just want to ask how to initialize a variable properly when you're gonna use it in a condition? here's the code i made so far..

import java.io.*;

public class Bwiset{
    public static void main(String[]args){
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int t=0;
        double v=0;


        String vt,tt;
        double tc;

try{

        System.out.print("==================================\nVehicle Type \tCharge per km \n Car \t\t Php0.50\n Light Truck \t Php0.75");
        System.out.print("\n Bus \t\t Php1.00");
        System.out.print("\n Heavy Truck \t Php1.25\n");
        System.out.print("==================================\n");
        System.out.print("Km Travelled \t Ticket Color\n");
        System.out.print("   15 \t \t Yellow\n");
        System.out.print("   25 \t \t Blue\n");
        System.out.print("   50 \t \t Red\n");
        System.out.print("   75 \t \t Orange\n");




        System.out.println("Welcome! Please enter your vehicle type: ");
        vt=br.readLine();
        System.out.println("Enter ticket type: ");
        tt=br.readLine();





        if (vt=="car"||vt=="Car"||vt=="CAR"||vt=="cAr"||vt=="caR"||vt=="CAr"||vt=="cAR"){
            v=0.50;
        }else if (vt=="light truck"||vt=="Light Truck"||vt=="LIGHT TRUCK"){
            v=0.75;
        }else if (vt=="bus"||vt=="Bus"||vt=="BUS"){
            v=1.00;
        }else if (vt=="heavy truck"||vt=="Heavy Truck"||vt=="HEAVY TRUCK"){
            v=1.25;
        }



        if (tt=="Yellow"||tt=="yellow"||tt=="YELLOW"){
            t=15;
        }else if (tt=="BLUE"||tt=="blue"||tt=="Blue"){
            t=25;
        }else if (tt=="red"||tt=="RED"||tt=="Red"){
            t=50;
        }else if (tt=="orange"||tt=="ORANGE"||tt=="Orange"){
            t=75;
        }




        tc=v*t;



        System.out.println("Vehicle Type: " + vt);
        System.out.println("Ticket Type:" + tt);
        System.out.println("Charge According to Vehicle: " + v);
        System.out.println("Kilometers traveled according to ticket: " + t);
        System.out.print("Total Toll Charge: " + tc);



}catch(IOException e){
}

    }
}

to make it short, the program runs but when you input a vehicle type and a ticket type, it still returns 0 which isn't supposed to happen-- because i used conditions that assigns a certain value for each vehicle type and ticket types (also shown in the table that comes with the program). i've tried to search for proper initializations related to this but i can't find anything and i get so confused. please help. sorry for bein such a noob

1 Answers1

4

Don't use == to compare String values; in Java, with objects, == compares object references to determine if they refer to the same object.

Use String#equals to compare String values.

if (vt.equals("car") || vt.equals("Car"))

In fact, String#equalsIgnoreCase is better in your case, to cut down on the number of conditions in each if statement:

if (vt.equalsIgnoreCase("car"))
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • Just a note on this solution: while `equalsIgnoreCase` is probably what you want, it is not exactly equal to the original code (though perhaps preferable), since it would accept other "abnormal" capitalization forms of some inputs, such as "rEd", "reD', and "rED". – Kevin Welker May 13 '13 at 19:49