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));
}
}
}
0.System.out.println(Arrays.toString(vehicle));carTypearray is not zero.int[] vehicle= new int[18];fills it with0. You assumed your file reading code worked. Based on your problem description, I think you are mistaken. Ergo, the array is filled with18zeros.