vendredi 26 mars 2021

How do you do if statement with words and it says my char cannot be deferenced

so I'm making this program where the objective is: Create a program that will compute a fare of a passenger. The passenger has 3 types namely ordinary, student and senior. Based on the type of passenger it has an equivalent discount for the student has 20% discount, for the senior it has 30% discount and for the ordinary passenger no discount at all. The formula for computing the distance is the same as the previous problem before. The first 10 kilometers is minimum fare of 20 pesos and for the distance greater than 10 will be 2.50 pesos per kilometer plus the minimum fare of 20 pesos.

    public static void main(String[] args) 
{
    char Type;
    int dist, f0r;
    double price, dcprice, dscnt;
    Scanner input = new Scanner (System.in);
    
    System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
    Type = input.next().charAt(0);
    
    if (Type.equals("Ordinary"))
    {
        System.out.println("Enter Distance Travelled: ");
        dist = input.nextInt();
        
        f0r = dist - 10;
        price = f0r * 2.50 + 20;
        
        System.out.println("the total price is; "+ price);
    }
    else
    {
        
    }
}    

} I'm having this issue where im imputting the word Ordinary but its just stops and did not continue to the if statement where the rest of the code is.

I have also tried it with

    {
        char Type, Ordinary = 0, Student = 0, Senior = 0;
        int dist, f0r;
        double price, dcprice, dscnt;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
        Type = input.next().charAt(0);
        ```
        if (Type == Ordinary)
and it still did the same thing
What am I doing Wrong?


update I fixed the thing and heres my take

    public static void main(String[] args) 
    {
        String Ordinary = "Ordinary";
        String Student = "Student";
        String Senior = "Senior";
        int dist, f0r;
        double price, dcprice, dscnt;
        Scanner input = new Scanner (System.in);
        
        System.out.println("Enter Type of Passenger (Ordinary/Student/Senior): ");
        String Type = input.nextLine();
        
        if (Type.equals(Ordinary))
        {
            System.out.println("Enter Distance Travelled: ");
            dist = input.nextInt();
            
            f0r = dist - 10;
            price = f0r * 2.50 + 20;
            
            System.out.println("the total price is; "+ price);
        }
        else if (Type.equals(Student))
        {
            System.out.println("Enter Distance Travelled: ");
            dist = input.nextInt();
            
            f0r = dist - 10;
            price = f0r * 2.50 + 20;
            dscnt = price * .20;
            dcprice = price - dscnt;
            
             System.out.println("the total price is; "+ dcprice);
        }
    }    
}
please Review and is there still any error?

Aucun commentaire:

Enregistrer un commentaire