3 Replies - 274 Views - Last Post: 05 June 2014 - 01:17 AM Rate Topic: -----

#1 brittishsnow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 11-October 13

Syntax error when testing code

Posted 04 June 2014 - 08:12 PM

My issue is that when I run the code if I enter anything but 1, 2, or 3 the code breaks. I have spent hours trying to find the error. here is my code
import java.util.Scanner;


public class Tester {

	public static void main(String[] args) {
		/**
		 * constructor
		 * pre: none
		 * post: inherit values of other classes
		 */
	Car Car = new Car(); //inherits the properties of the Car class
	Truck Truck = new Truck(); //inherits the properties of the Truck class
	Minivan Minivan = new Minivan(); //inherits the properties of the Minivan class
	Scanner kbinput = new Scanner(System.in); //initiates the keyboard
	
	System.out.println("Which Vehicle would you like to learn more about?");
	System.out.println("1: Car\n2: Minivan\n3:Truck");
	int vehiclechoice = kbinput.nextInt();
	 
	if (vehiclechoice == 1)
	{
		System.out.println("You have chosen to learn about the car.");
		System.out.println("Please enter one of the following letters:");
		System.out.println("P: for passangers");
		System.out.println("T: for tires and spare tires");
		System.out.println("S: for the max speed");
		System.out.println("D: for the distance per tank");
		System.out.println("Q: to quit");
		String carinfo = kbinput.next();
		char input = carinfo.charAt(0);
		switch(input) //Switches what the input does according to what you enter in the choice
        {
    case 'P':
    case 'p':  
        System.out.println("This Vehicle can hold " + Car.getpassangers() + " passangers.");
        break;
    case 'T':
    case 't':
  	  System.out.println("This Vehicle can hold " + Car.getsparetires() +  " spare tires and has " + Car.gettires() + "tires." );
  	  break;
    case 'S':
    case 's':
  	  System.out.println("This Vehicle can go " + Car.getmaxspeed() +  " km/h" );
  	  break;
    case 'D':
    case 'd':
  	  System.out.println("This  Vehicle can go " + Car.getdistance() + " km on one tank.");
  	  break;
    case 'Q':
    case 'q':
    	System.out.println("Good Bye");
    	System.exit(input); //ends the console
 default: 
	   	 System.out.println("Please enter a 'P', 'S', 'T', or 'D'. Thank you.");
        }}
		else if (vehiclechoice == 2)
	{
			System.out.println("You have chosen to learn about the minivan.");
			System.out.println("Please enter one of the following letters:");
			System.out.println("P: for passangers");
			System.out.println("T: for tires and spare tires");
			System.out.println("S: for the max speed");
			System.out.println("D: for the distance per tank");
			System.out.println("Q: to quit");
			  String carinfo = kbinput.next();
			  char input = carinfo.charAt(0);
			switch(input) //Switches what the input does according to what you enter in the choice
	          {
	      case 'P':
	      case 'p':  
	          System.out.println("This Vehicle can hold " + Minivan.getpassangers() + " passangers.");
	          break;
	      case 'T':
	      case 't':
	    	  System.out.println("This Vehicle can hold " + Minivan.getsparetires() +  " spare tires and has " + Minivan.gettires() + "tires." );
	    	  break;
	      case 'S':
	      case 's':
	    	  System.out.println("This Vehicle can go " + Minivan.getmaxspeed() +  " km/h" );
	    	  break;
	      case 'D':
	      case 'd':
	    	  System.out.println("This  Vehicle can go " + Minivan.getdistance() + " km on one tank.");
	    	  break;
	      case 'Q':
	      case 'q':
	      	System.out.println("Good Bye");
	      	System.exit(input); //ends the console 	  
	   default: 
		   	 System.out.println("Please enter a 'P', 'S', 'T', or 'D'. Thank you.");	
		}
	}
		else if (vehiclechoice == 3)
		{{
			System.out.println("You have chosen to learn about the truck.");
			System.out.println("Please enter one of the following letters:");
			System.out.println("P: for passangers");
			System.out.println("T: for tires and spare tires");
			System.out.println("S: for the max speed");
			System.out.println("D: for the distance per tank");
			System.out.println("Q: to quit");
			  String carinfo = kbinput.next();
			  char input = carinfo.charAt(0);
			switch(input) //Switches what the input does according to what you enter in the choice
	          {
	      case 'P':
	      case 'p':  
	          System.out.println("This Vehicle can hold " + Truck.getpassangers() + " passangers.");
	          break;
	      case 'T':
	      case 't':
	    	  System.out.println("This Vehicle can hold " + Truck.getsparetires() +  " spare tires and has " + Truck.gettires() + "tires." );
	    	  break;
	      case 'S':
	      case 's':
	    	  System.out.println("This Vehicle can go " + Truck.getmaxspeed() +  " km/h" );
	    	  break;
	      case 'D':
	      case 'd':
	    	  System.out.println("This  Vehicle can go " + Truck.getdistance() + " km on one tank.");
	    	  break;
	      case 'Q':
	      case 'q':
	      	System.out.println("Good Bye");
	      	System.exit(input); //ends the console
	   default: 
		   	 System.out.println("Please enter a 'P', 'S', 'T', or 'D'. Thank you.");
	          }}}
		
		else {
				System.out.println("Please enter a '1', '2', or '3'. Thank you.");
		}
	kbinput.close();
	
	}
}


	




Also if the other classes are needed here they are

public class Vehicle {

int tires = 4; //gives a default value of 4 tires for the Vehicle 
int passangers= 2; //gives a default of 2 passengers for the Vehicle
int distance = 100; //gives a default of 100 km for the Vehicle



}				



public class Car extends Vehicle{ //Car extends Vehicle
/** 
 * constructor
 * pre: none
 * post: a car has been born.
 */
private int tires; //declares tires as an int
private int sparetires; //declares spare tires as an int
private int maxspeed; //Declares maxspeed as an int
private int distance;  //Declares distance as an int
private int passangers; //Declares passengers as an int
/**
 * sets the values of each variable
 * pre: received the default values from the vehicle
 * post: the values are set to be appropriate
 */
public Car () {
tires = super.tires; //inherits the default value
sparetires = 1; //declares how many spare tires
maxspeed = 220; //declares how fast the vehicle can go 
distance = super.distance * 5; //declares how far the vehicle can go on one tank
passangers =( super.passangers *2); //declares how many people can fit in the car
}
/**
 * constructor
 * pre: none
 * post: returns tires
 */
public  int gettires() { 
	return(tires);
}
/**
 * constructor
 * pre: none
 * post: returns sparetires
 */
public  int getsparetires()  {// a method that returns sparetires
	return(sparetires);
}
/**
 * constructor
 * pre: none
 * post: returns maxspeed
 */
public  int getmaxspeed (){// a method that returns maxspeed
	return(maxspeed);
}
/**
 * constructor
 * pre: none
 * post: returns distance
 */
public int getdistance (){//a method that returns distance
	return(distance);
}
/**
 * constructor
 * pre: none
 * post: returns passangers
 */
public  int getpassangers (){//a method that returns passengers
	return(passangers);
}
}


public class Minivan extends Vehicle{ //Minivan extends Vehicle
	/** 
	 * constructor
	 * pre: none
	 * post: a minivan has been born.
	 */
private int tires;//declares tires as an int
private int sparetires;//declares spare tires as an int
private int maxspeed; //Declares maxspeed as an int
private int distance;  //Declares distance as an int
private int passangers;//Declares passengers as an int
public Minivan() {
	/**
	 * sets the values of each variable
	 * pre: received the default values from the vehicle
	 * post: the values are set to be appropriate
	 */
tires = super.tires;  //inherits the default value
sparetires = 1;//declares how many spare tires
maxspeed = 180; //declares how fast the vehicle can go 
distance = super.distance *4;//declares how far the vehicle can go on one tank
passangers =( super.passangers *4); //declares how many people can fit in the minivan
}
/**
 * constructor
 * pre: none
 * post: returns tires
 */
public  int gettires() {//a method that returns tires
	return(tires);
}
/**
 * constructor
 * pre: none
 * post: returns sparetires
 */
public  int getsparetires()  {// a method that returns sparetires
	return(sparetires);
}
/**
 * constructor
 * pre: none
 * post: returns maxspeed
 */
public  int getmaxspeed (){// a method that returns maxspeed
	return(maxspeed);
}
/**
 * constructor
 * pre: none
 * post: returns distance
 */
public int getdistance (){//a method that returns distance
	return(distance);
}
/**
 * constructor
 * pre: none
 * post: returns passengers
 */
public  int getpassangers (){//a method that returns passengers
	return(passangers);
}
}


public class Truck extends Vehicle{ //Truck extends Vehicle
	/** 
	 * constructor
	 * pre: none
	 * post: a truck has been born.
	 */
private int tires;//declares tires as an int
private int sparetires;//declares spare tires as an int
private int maxspeed; //Declares maxspeed as an int
private int distance; //Declares distance as an int
private int passangers; //Declares passengers as an int
public Truck() {
	/**
	 * sets the values of each variable
	 * pre: received the default values from the vehicle
	 * post: the values are set to be appropriate
	 */
tires = super.tires; //inherits the default value
sparetires = 2;//declares how many spare tires
maxspeed = 200;//declares how fast the vehicle can go 
distance = super.distance * 6; //declares how far the vehicle can go on one tank
passangers =( super.passangers *3); //declares how many people can fit in the truck
}
/**
 * constructor
 * pre: none
 * post: returns tires
 */
public  int gettires() { //a method that returns tires
	return(tires);
}
/**
 * constructor
 * pre: none
 * post: returns sparetires
 */
public  int getsparetires()  {// a method that returns sparetires
	return(sparetires);
}
/**
 * constructor
 * pre: none
 * post: returns maxspeed
 */
public  int getmaxspeed (){// a method that returns maxspeed
	return(maxspeed);
}
/**
 * constructor
 * pre: none
 * post: returns distance
 */
public int getdistance (){//a method that returns distance
	return(distance);
}
/**
 * constructor
 * pre: none
 * post: returns passangers
 */
public  int getpassangers (){//a method that returns passengers
	return(passangers);
}
}



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax error when testing code

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2725
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Syntax error when testing code

Posted 05 June 2014 - 12:53 AM

Quote

My issue is that when I run the code if I enter anything but 1, 2, or 3 the code breaks.
? Your code will break irrespective of the digit you enter. If you want it otherwise, you need to use a loop
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Syntax error when testing code

Posted 05 June 2014 - 01:02 AM

Quote

My issue is that when I run the code if I enter anything but 1, 2, or 3 the code breaks. I have spent hours trying to find the error. here is my code

What do you mean by break? any error message or something? From what I see in your main() method, you have if condition blocks for 1,2 and 3! and anything else you give a message, so what is the problem exactly.

Checking your other classes, your inheritance doesnt make any sense at all, you will need to check the inheritance again, especially using protected keyword and method overriding.
Here are few tutorials which you can start with:
http://docs.oracle.c...subclasses.html
http://www.dreaminco...to-inheritance/
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2725
  • View blog
  • Posts: 11,457
  • Joined: 20-September 08

Re: Syntax error when testing code

Posted 05 June 2014 - 01:17 AM

Also, you need to get your variables in camel case, and it's passEnger btw

Quote

What do you mean by break?
I think s/he means break as in break ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1