Error in my code: String cannot be converted to int / String cannot be converted to double | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Error in my code: String cannot be converted to int / String cannot be converted to double

Here is my code: class Car{ private String color; private int engineSize; private double carLength; private String model; private String carInfo; public String getColor(){ return color; } public int getEngineSize(){ return engineSize; } public double getCarLength(){ return carLength; } public String getModel(){ return model; } public void printCarInfo(){ System.out.println("Color: " + getColor()); System.out.println("Engine Size: " + getEngineSize()); System.out.println("Length of Car: " + getCarLength()); System.out.println("Model: " + getModel()); } public void setColor(String color){ this.color = color; } public void setEngineSize(int engineSize){ this.engineSize = engineSize; } public void setCarlength(double carLength){ this.carLength = carLength; } public void setModel(String model){ this.model = model; } } class Program{ public static void main(String[] args){ Car lamborghini = new Car(); lamborghini.setColor("red"); lamborghini.setEngineSize("14"); lamborghini.setCarlength("50.36"); lamborghini.setModel("A48J1"); lamborghini.printCarInfo(); } } I keep getting the error code: ..\Playground\:8: error: incompatible types: String cannot be converted to int lamborghini.setEngineSize("14"); ^ ..\Playground\:9: error: incompatible types: String cannot be converted to double lamborghini.setCarlength("50.36"); ^ Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output 2 errors Please help me fix this. Thanks

22nd Dec 2017, 10:10 PM
ota.234
4 Answers
+ 15
Numbers are like this: setEngineSize(14); No " " needed.
22nd Dec 2017, 10:12 PM
jay
jay - avatar
+ 6
The error messages are telling you what's happening - setEngineSize takes a parameter of type 'int' but you are passing a string in, anything surrounded by " " will be interpreted as a string.
22nd Dec 2017, 10:16 PM
Dan Walker
Dan Walker - avatar
+ 2
thank you!!
23rd Dec 2017, 12:00 AM
ota.234
0
/** This program demonstrates an array of String and integer objects. It should print out the years and the number of shoppers in the last 4 days before Christmas. It should print out the day with the most shoppers It should print out the average number of shoppers over the 4 day period This is what should display when the program runs as it should Dec 21 had 10,000 shoppers. Dec 22 had 12,000 shoppers. Dec 23 had 20,000 shoppers. Dec 24 had 16,000 shoppers. Highest Day is Dec 23 with 20,000 shoppers. Average shoppers in the last 4 days before Christmas is 14,500.00 shoppers. */ public class Shoppers { public static void main(String[] args) { String[] days = { "Dec 21", "Dec 22", "Dec 23", "Dec 24"}; int[] shoppers = {10000, 12000, 20000, 16,000}; double average = 0.0; int highest = 0; String highestDay = " "; int sum = 0; for (int index = 0; index < shoppers.length; index++) { System.out.printf("%s had %,8d shoppers.\n", days[index], shoppers[index]); } int i = Interger.parseInt( highest = days[0]); for (int index = 0; index < shoppers.length; index++) { if (shoppers[index] >= highest) { highest = shoppers[index]; highestDay = days[index]; sum = sum + shoppers[index]; average = (double)sum / shoppers.length; } } System.out.printf("Highest Day is %s with %,10d shoppers.\n", highest, highestDay); System.out.printf("Average shoppers in the last 4 days before Christmas is %,10.2f shoppers.\n", average); } }
29th Apr 2021, 3:13 PM
Nikita Upadhye
Nikita Upadhye - avatar