Issame
Issame

Reputation: 1

Java Switch statement going straight to default using arrays

My switch statement coresponds to an array. The only values in the array are 1-6. I am sure of that. But for some reason the default value is always selected no mater what the value in the array is. Can someone tell me why this is happening?

The variables in the switch statement declared.

   //declares arrays
       int[] vehicle= new int[18];
       int[] gate =  new int[18];
       int index = 0;

    //stores the numbers from the dat files into the arrays

    while(inFile.hasNext()){
        vehicle[index] = inFile.nextInt();
        gate[index] = inFile.nextInt();
        index++;
    }

the switch statement

for( int i = 0; i<vehicle.length; i++){

        switch(vehicle[i]){
            case 1: carType[i] = "Compact Car";
                    break;
            case 2: carType[i] = "Small Car";
                    break;
            case 3: carType[i] = "Mid Size Car";
                    break;
            case 4: carType[i] = "Full Size Car";
                    break;
            case 5: carType[i] = "Truck";
                    break;
            case 6: carType[i] = "16 Wheeler";
                    break;
            default: carType[i] = "Invalid Vehicle Type";
                    break;
                }
            }

EDIT FULL CODE

Object CarCharge class

public class CarChargeAP
{
// instance variables - replace the example below with your own
private int[] vehicle = new int[18];
private int[] gateNumber = new int[18];
private double[] factor = new double[18];
private double[] toll = new double[18]; 
private String[] carType = new String[18];
private double[] cost= new double[18];

/**
 * Constructor for objects of class CarChargeAP
 * pre:none
 * post:variables initialized
 */
public CarChargeAP(int[] vt, int[] gn)
{
    // initialise instance variables
    vt=vehicle;
    gateNumber = gn;

}

/**
 * CarType
 * finds the car types of the vehicles and stores it in an array
 * pre: vehicle arrat initializes
 * post: car type array sorted
 */
public void carType()
{

    for( int i = 0; i<vehicle.length; i++){

        switch(vehicle[i]){
            case 1: carType[i] = "Compact Car";
                    break;
            case 2: carType[i] = "Small Car";
                    break;
            case 3: carType[i] = "Mid Size Car";
                    break;
            case 4: carType[i] = "Full Size Car";
                    break;
            case 5: carType[i] = "Truck";
                    break;
            case 6: carType[i] = "16 Wheeler";
                    break;
            default: carType[i] = "Invalid Vehicle Type";
                    break;
                }
            }


}

/**
 * Toll
 * finds the toll of each of the gates and stores it into an array
 * pre: gateNumber initialized
 * post: toll numbers stored
 */
public void toll(){
    for(int index = 0; index< gateNumber.length; index++){
        switch(gateNumber[index]){
            case 1: toll[index] = 1.35;
                    break;
            case 2: toll[index] = 2.00;
                    break;
            case 3: toll[index] = 2.50;
                    break;
            case 4: toll[index] = 3.25;
                    break;
            case 5: toll[index] = 4.10;
                    break;
            case 6: toll[index] = 4.80;
                    break;
            case 7: toll[index] = 5.50;
                    break;  
            case 8: toll[index] = 6.00;
                    break;
            default: toll[index] = 0;
                    break;
        }
    }

    }


 /**
  * factor
  * finds the factor of the coresponding vehicle type and stores it into array
  * pre: vehicle type initialized
  * post: factor numbers stored
  */
 public void factor()
 {
   for( int i = 0; i<vehicle.length; i++){

        switch(vehicle[i]){
            case 1: factor[i] = 1.0;
                    break;
            case 2: factor[i] = 1.3;
                    break;
            case 3: factor[i] = 1.6;
                    break;
            case 4: factor[i] = 2.0;
                    break;
            case 5: factor[i] = 2.4;
                    break;
            case 6: factor[i] = 2.7;
                    break;
            default: factor[i] = 0;
                    break;
                }
            }  
 }



 /**
  * Cost
  * finds the cost of the fee highway
  * pre: factor, toll, and vehicle initialized
  * post: cost numbers stored
  */
public void cost()
{
    for(int i = 0; i<vehicle.length; i++){
        cost[i] = (factor[i] * toll[i]);
    }
}


/*************************************************
 * Getters
 *************************************************/
/**
 * getCarType
 * getter for car type depending on index
 * pre: cartype initialized
 * post: carType returned
 */
public String getCarType(int i){
    return carType[i];
}

/**
 * getVehicle
 *getter for vehicle 
 *Pre: vehicle initialized
 *post: vehicle returned
 */
public int getVehicle(int i){
    return vehicle[i];
}

/**
 *getGateNumber
 *getter for gate number 
 *Pre: gatNumber initialized
 *post: gate number returned
 */
public int getGateNumber(int i ){
    return gateNumber[i];
}


/**
 * getToll
 * getter for toll
 * pre:toll initialized
 * post: toll returned
 */
public double getToll(int i){
    return toll[i];
}

/**
 * getFactor
 * getter for gactor
 * pre: factor initialized
 * post: factor is returned
 */
public double getFactor(int i){
    return factor[i];
}

/**
 * getCost
 * getter for cost
 * pre: cost initialized
 * post: cost returnded
 */
public double getCost(int i){
    return cost[i];
}

/************************************
 * Setters
 **************************************/

 /**
 * setCarType
 * setter for car type depending on index
 * pre: cartype initialized
 * post: carType set
 */
public void setCarType(int i, String str){
    carType[i] = str;
}

/**
 * setVehicle
 *setter for vehicle 
 *Pre: vehicle initialized
 *post: vehicle set
 */
public void setVehicle(int i, int set){
    vehicle[i] = set;
}

/**
 *setGateNumber
 *seetter for gate number 
 *Pre: gatNumber initialized
 *post: gate number set
 */
public void setGateNumber(int i, int set ){
    gateNumber[i] = set;
}


/**
 * setToll
 * setter for toll
 * pre:toll initialized
 * post: toll set
 */
public void setToll(int i, double set){
    toll[i] = set;
}

/**
 * setFactor
 * setter for gactor
 * pre: factor initialized
 * post: factor is set
 */
public void setFactor(int i, double set){
    factor[i] = set;
}

/**
 * setCost
 * setter for cost
 * pre: cost initialized
 * post: cost set
 */
public void setCost(int i,double set){
    cost[i] = set;
}
}

Tester Class

import java.util.Scanner;
import java.io.*;
public class ChargeTesterAlyiahP
{
public static void main(String[] args){
    //sets up scanner 
    Scanner inFile = null;
    try
    { 
        inFile = new Scanner(new File("prog435a.dat")); 
    }   
    catch(FileNotFoundException e) 
    { 
        System.out.println("File not found!!"); 
        System.exit(0); 
    } 

    //declares arrays
    int[] vehicle= new int[18];
    int[] gate =  new int[18];
    int index = 0;

    //stores the numbers from the dat files into the arrays

    while(inFile.hasNext()){
        vehicle[index] = inFile.nextInt();
        gate[index] = inFile.nextInt();
        index++;
    }

    //creating a carcharge object
    CarChargeAP object = new CarChargeAP(vehicle, gate);

    //calling upon other class to find the values
    object.carType();
    object.toll();
    object.factor();
    object.cost();

    System.out.printf("%5s %5s %5s $5s","Car Type", "Base Toll", "Factor", "Cost");
    for(int i = 0; i<vehicle.length; i++){

        System.out.printf("\n%5s $%5s %5s $%5s",object.getCarType(i), object.getToll(i), object.getFactor(i), object.getCost(i));

    }

}


}

Upvotes: 0

Views: 163

Answers (2)

Maarten Bodewes
Maarten Bodewes

Reputation: 94018

Your constructor is incorrect:

public CarChargeAP(int[] vt, int[] gn)
{
    // initialise instance variables
    vt=vehicle;
    gateNumber = gn;

}

here vehicle, your field is assigned to vt, which is an input parameter. This would have caused an error if you had not assigned an unused, empty array to the vehicle field:

private int[] vehicle = new int[18];

However, this should not be resolved by just changing the order of the variable assignment: vehicle = vt. Instead you should be copying the values from arrays:

System.arraycopy(vt, 0, vehicle, 0, vehicle.length);

That way changes to the vt aren't reflected in the class, i.e. this should not be possible:

CarChangeAP ap = new CarChangeAP(vt, gn);
// changes the field within ap
vt[0] = -1;

as it violates the encapsulation of the data within the fields of the class instance. Data within the class should only be updated using the methods of the class. Now the field is private but the contents can still be changed, as arrays in Java are always mutable.

Upvotes: 1

Ezell Buoee
Ezell Buoee

Reputation: 35

i don't what's happening but this works for me

        int[] vehicle = new int[6];
    String[] carType = new String[6];
    for (int a = 0; a < vehicle.length; a++) {
        vehicle[a] = a + 1;
    }
    for (int i = 0; i < vehicle.length; i++) {

        switch (vehicle[i]) {
            case 1:
                carType[i] = "Compact Car";
                break;
            case 2:
                carType[i] = "Small Car";
                break;
            case 3:
                carType[i] = "Mid Size Car";
                break;
            case 4:
                carType[i] = "Full Size Car";
                break;
            case 5:
                carType[i] = "Truck";
                break;
            case 6:
                carType[i] = "16 Wheeler";
                break;
            default:
                carType[i] = "Invalid Vehicle Type";
                break;
        }
    }
    for (int a = 0; a < carType.length; a++) {
        System.out.println(carType[a]);
    }

which means something else is happening to the file you are reading from, can you post your full code including the file reader

Upvotes: 0

Related Questions