Alright Here is how my output looks Here is what I need it to look like I have t
ID: 3539700 • Letter: A
Question
Alright Here is how my output looks
Here is what I need it to look like
I have tried String.format() along with various print modifiers and no matter what I do the columns are all still misalligned
I have a toString method that puts everything together
and a print statement in my main method that prints
I dont know which one to edit to format like the above
Here is my code, if anyone could help! Im sure im just missing something obvious
///////////////////////////////////Print Method///////////////////////////////////////////////
private static void printCars(){
System.out.println(" Imported Car List : ");
int number1 = 1;
for (int count = 0; count<iImport; count++ )
{
System.out.println (number1++ +". " +importCar[count].toString());
}
System.out.println(" Domestic Car List : ");
int number2 = 1;
for (int count = 0; count<iDomestic; count++ )
{
System.out.println(number2++ +". " +domesticCar[count].toString());
}
System.out.println(" Number of imported cars : "+iImport);
System.out.println("Number of Domestic cars : "+iDomestic);
int total = (iImport+iDomestic);
System.out.println("Total : "+ total);
}
and then my toString Methods
fyi i must use toString methods for my assignment
////////////////////////////////////////Import/////////////////////////////////////////
@Override
public String toString() {
return company
+ " " + model +" " +year + " " + formatP.format(price)
+ " " + category + " " + countryImportedFrom +" " +tax+"%" ;
}
//////////////////////////////////////Domestic/////////////////////////////////////////////
@Override
public String toString() {
return company +" " + model +" " + year + " " +formatP.format(price)
+ " " + category +" " + state ;
}
Also Company is a String, Model is a String, Year is a int, price is a double, Category is a String, state is a String, Tax is a double
Explanation / Answer
Here is the entire code with some changes in it..
Use this new code.
public class DomesticCar {
private char Identifier;
private String company;
private String model;
private int year;
private double price;
private String category;
private String city;
public DomesticCar() {
super();
}
public DomesticCar(char identifier, String company, String model, int year,
double price, String category, String city) {
super();
Identifier = identifier;
this.company = company;
this.model = model;
this.year = year;
this.price = price;
this.category = category;
this.city = city;
}
public char getIdentifier() {
return Identifier;
}
public void setIdentifier(char identifier) {
Identifier = identifier;
}
public String getCompany() {
StringBuilder sb = new StringBuilder();
int rest = 12 - company.length();
sb.append(company);
for(int i = 1; i < rest; i++)
{
sb.append(" ");
}
return sb.toString();
}
public void setCompany(String company) {
this.company = company;
}
public String getModel() {
StringBuilder sb = new StringBuilder();
int rest = 12 - model.length();
sb.append(model);
for(int i = 1; i < rest; i++)
{
sb.append(" ");
}
return sb.toString();
}
public void setModel(String model) {
this.model = model;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getCategory() {
StringBuilder sb = new StringBuilder();
int rest = 12 - category.length();
sb.append(category);
for(int i = 1; i < rest; i++)
{
sb.append(" ");
}
return sb.toString();
}
public void setCategory(String category) {
this.category = category;
}
public String getCity() {
StringBuilder sb = new StringBuilder();
int rest = 12 - city.length();
sb.append(city);
for(int i = 1; i < rest; i++)
{
sb.append(" ");
}
return sb.toString();
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return getCompany()
+" " + getModel() +" " + getYear() + " " + formatPrice(getPrice())
+ " " + getCategory() +" " + getCity() ;
}
public String formatPrice(double price)
{
String priceString = Double.toString(price);
StringBuilder sb = new StringBuilder();
int rest = 12 - priceString.length();
for(int i = 1; i < rest; i++)
{
sb.append(" ");
}
sb.append(priceString);
return sb.toString();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.