Web Hosting Talk







View Full Version : java - how to ask for a manual input in a mutator method call


metronome
02-23-2005, 10:13 AM
This is the super class vehicle


public class Vehicle
{
private String reg ;
private String type ;
private int maxLoad ;


public Vehicle(String registration)
{
reg = registration ;
}


public String getRegistration()
{
return reg;
}


public String getType()
{
return type;
}


public int getMaxLoad()
{
return maxLoad;
}


public void setType(String vehicleType)
{
if (vehicleType == "van") {maxLoad = 3500;}
if (vehicleType == "small van") {maxLoad = 500;}
if (vehicleType == "lorry") {setLoad(3000);}
type = vehicleType;
}

private void setLoad(int maximumLoad)
{
maxLoad = maximumLoad;
}




}




This is the lorry class that inherits from vehicle



public class Lorry extends Vehicle
{
private boolean artic ;


public Lorry(String registration, boolean articulated)

{
super(registration);
setType("lorry");
artic = articulated ;
}


public boolean getArticulated()
{
return artic;
}
}


I'm trying to get it to ask for a manual input when it detects the string "lorry" for vehicle type as lorries can have individual maximum capacities where as vans have known capacities. The problem is that I can get it to setup for the known types but since a lorry has a determined for individual loads, I need to know how to call a method and ask for an input

the method im looking at is the setLoad method which is called from the setType method. Currently it is happy to accpet 3000 as an input but i want it to ask for an input instead.

if i just have it as

if (vehicleType == "lorry") {setLoad();}

then it gives me the following error

setLoad(int) in Vehicle cannot be applied to ()

note: I don't even know if this is the right way of doing it, I thought that you'd have to call a method to ask for an input rather then being able to request in the if statement

anyway many thanks for anyone who is able to help out

ilyash
02-25-2005, 12:54 PM
you cant do (vehicleType == "lorry")

you must use the equals method

if(vehicleType.equals("lorry")){
// do stuff..
}

MilesToGo
02-26-2005, 03:13 AM
The way things work in Java is you need to call an existing method signature, or it won't compile. A method signature is the combination of the method name and the parameters it takes.

Java isn't going to automatically prompt the user for input when a method isn't passed in arguments.

To dynamically read input from the command line, you can use System.in, but it's not so simple as just calling it. Read this article (http://www.devx.com/gethelpon/10MinuteSolution/16715/0/page/3) for an example.