Reputation: 3
I am new to Java and was following a book with the following code...
class Vehicle {
int passengers;
int fuelcap;
int mpg;
int range() {
return mpg * fuelcap;
}
double fuelneeded(int miles) {
return (double) miles / mpg;
}
}
class TwoVehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
double gallons;
int dist = 252;
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
gallons = minivan.fuelneeded(dist);
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
gallons = sportscar.fuelneeded(dist);
System.out.println("To go ", + dist + " miles sportscar needs " + gallons + " gallons of fuel.");
}
}
However, upon running this code I get an error saying 'error: no suitable method found for println(String,String)'. Why is this happening?
Upvotes: 0
Views: 118
Reputation: 2601
You can not use System.out.println
with 2 arguments, pass it only 1 String.
The comma that is not inside the String (After the String "To go" ,
), is telling the compiler to treat the Strings as 2 different arguments.
Change this line:
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
to this:
System.out.println("To go " + dist + " miles minivan needs " + gallons + " gallons of fuel.");
Upvotes: 2
Reputation: 1
Due to the presence of comma(,) outside the string your code is showing error
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
Your code should be
System.out.println("To go ," + dist + " miles minivan needs " + gallons + " gallons of fuel.");
Upvotes: 0