Anonymous
Несовместимые типы: байт невозможно преобразовать в строку [закрыто]
Сообщение
Anonymous » 01 июн 2024, 21:22
Я новичок в Java, и у меня постоянно возникает эта ошибка, в которой говорится о несовместимых типах: байт не может быть преобразован в строку. Я посмотрел на свой конструктор, и все выглядело нормально, есть ли что-то, что мне не хватает? это также мой первый пост здесь, я не знаю, правильно ли я его разместил.
вот мой код
Код: Выделить всё
package dt;
public class Suv extends Vehicle{
private byte capacity;
private byte childSeatCount;
private byte airbagCount;
public Suv( String ownerName, String ownerAddress, String brand,
String model, String licencePlate, float value,byte capacity,
byte childSeatCount, byte airbagCount)throws VehicleException{
super(ownerName, ownerAddress, brand, model, licencePlate, value);
this.capacity = capacity;
this.childSeatCount = childSeatCount;
this.airbagCount = airbagCount;
}
public byte getCapacity() {
return capacity;
}
public byte getChildSeatCount() {
return childSeatCount;
}
public byte getAirbagCount() {
return airbagCount;
}
public void setCapacity(byte capacity) {
this.capacity = capacity;
}
public void setChildSeatCount(byte childSeatCount) {
this.childSeatCount = childSeatCount;
}
public void setAirbagCount(byte airbagCount) {
this.airbagCount = airbagCount;
}
@Override
public String toString() {
return super.getOwnerName() + ", " + super.getOwnerAddress() + ", " + brand + ", " + model + ", " +
licencePlate + ", " + value + ", " + capacity + ", " + childSeatCount + ", " + airbagCount;
}
}
import dt.*;
public class Driver {
public static void testTruck() throws VehicleException{
Truck cityTruck = new Truck(
new String[]{"sand", "traffic cones"},
1001,
"Ville de Montreal",
"Valerie Plante",
"City Hall",
"Chevrolet",
"Silverado",
"MTL 001",
40000.f
);
Truck westmountTruck = new Truck(
new String[]{"sand", "traffic cones", "concrete", "trees", "flowers"},
2001,
"Westmount",
"Michelle Desjardins",
"75 Belvedere",
"Ford",
"XLT",
"WMT 100",
27000.f
);
Truck expensiveTruck = new Truck(
new String[]{"traffic cones", "sand", "wood", "furniture", "food", "ice"},
3003,
"Griffintown",
"Jean Montagne",
"1122 Peel",
"Toyota",
"Tundra",
"RST 002",
45000.f
);
System.out.println(cityTruck.toString());
System.out.println(westmountTruck.toString());
System.out.print(expensiveTruck.toString());
}
public static void testSuv() throws VehicleException{
Suv cheapSuv = new Suv(
"Jeanne Tremblay",
"1100 Notre-Dame",
"Fiat",
"500X",
"ABC 123",
25000.f,
(byte) 12,
(byte) 4,
(byte) 2
);
Suv mediumSuv = new Suv(
"Pierre Maisonneuve",
"",
"Honda",
"Odyssey",
"FHG 789",
36000.f,
(byte) 7,
(byte) 2,
(byte) 2
);
Suv expensiveSuv = new Suv(
"Justin Levesque",
"1400 McGill",
"BMW",
"X6",
"",
74000.f,
(byte) 8,
(byte) 2,
(byte) 2
);
System.out.println(cheapSuv.toString());
System.out.println(mediumSuv.toString());
System.out.print(expensiveSuv.toString());
}
public static void main(String[] args) throws VehicleException {
try {
if (args.length > 0 && args[0].equalsIgnoreCase("truck")) {
testTruck();
} else {
testSuv();
}
} catch (VehicleException e) {
System.out.println("Error with Vehicle constructor parameter " + e.getParameter());
}
}
}
Я проверил свой конструктор, чтобы убедиться, что я не ошибся в параметрах, но вроде все в порядке.
Подробнее здесь:
https://stackoverflow.com/questions/785 ... -to-string
1717266123
Anonymous
Я новичок в Java, и у меня постоянно возникает эта ошибка, в которой говорится о несовместимых типах: байт не может быть преобразован в строку. Я посмотрел на свой конструктор, и все выглядело нормально, есть ли что-то, что мне не хватает? это также мой первый пост здесь, я не знаю, правильно ли я его разместил. вот мой код [code]package dt; public class Suv extends Vehicle{ private byte capacity; private byte childSeatCount; private byte airbagCount; public Suv( String ownerName, String ownerAddress, String brand, String model, String licencePlate, float value,byte capacity, byte childSeatCount, byte airbagCount)throws VehicleException{ super(ownerName, ownerAddress, brand, model, licencePlate, value); this.capacity = capacity; this.childSeatCount = childSeatCount; this.airbagCount = airbagCount; } public byte getCapacity() { return capacity; } public byte getChildSeatCount() { return childSeatCount; } public byte getAirbagCount() { return airbagCount; } public void setCapacity(byte capacity) { this.capacity = capacity; } public void setChildSeatCount(byte childSeatCount) { this.childSeatCount = childSeatCount; } public void setAirbagCount(byte airbagCount) { this.airbagCount = airbagCount; } @Override public String toString() { return super.getOwnerName() + ", " + super.getOwnerAddress() + ", " + brand + ", " + model + ", " + licencePlate + ", " + value + ", " + capacity + ", " + childSeatCount + ", " + airbagCount; } } import dt.*; public class Driver { public static void testTruck() throws VehicleException{ Truck cityTruck = new Truck( new String[]{"sand", "traffic cones"}, 1001, "Ville de Montreal", "Valerie Plante", "City Hall", "Chevrolet", "Silverado", "MTL 001", 40000.f ); Truck westmountTruck = new Truck( new String[]{"sand", "traffic cones", "concrete", "trees", "flowers"}, 2001, "Westmount", "Michelle Desjardins", "75 Belvedere", "Ford", "XLT", "WMT 100", 27000.f ); Truck expensiveTruck = new Truck( new String[]{"traffic cones", "sand", "wood", "furniture", "food", "ice"}, 3003, "Griffintown", "Jean Montagne", "1122 Peel", "Toyota", "Tundra", "RST 002", 45000.f ); System.out.println(cityTruck.toString()); System.out.println(westmountTruck.toString()); System.out.print(expensiveTruck.toString()); } public static void testSuv() throws VehicleException{ Suv cheapSuv = new Suv( "Jeanne Tremblay", "1100 Notre-Dame", "Fiat", "500X", "ABC 123", 25000.f, (byte) 12, (byte) 4, (byte) 2 ); Suv mediumSuv = new Suv( "Pierre Maisonneuve", "", "Honda", "Odyssey", "FHG 789", 36000.f, (byte) 7, (byte) 2, (byte) 2 ); Suv expensiveSuv = new Suv( "Justin Levesque", "1400 McGill", "BMW", "X6", "", 74000.f, (byte) 8, (byte) 2, (byte) 2 ); System.out.println(cheapSuv.toString()); System.out.println(mediumSuv.toString()); System.out.print(expensiveSuv.toString()); } public static void main(String[] args) throws VehicleException { try { if (args.length > 0 && args[0].equalsIgnoreCase("truck")) { testTruck(); } else { testSuv(); } } catch (VehicleException e) { System.out.println("Error with Vehicle constructor parameter " + e.getParameter()); } } } [/code] Я проверил свой конструктор, чтобы убедиться, что я не ошибся в параметрах, но вроде все в порядке. Подробнее здесь: [url]https://stackoverflow.com/questions/78564600/incompatible-types-byte-cannot-be-converted-to-string[/url]