#1, Im tryin to get this bb code right, #2 I need help with finding the problem with me code, it is saying that my for loop is a illegal start type and also a identifer is needed, but I have no clue, this is my first time doing object oriented programming, help please.

import javax.swing.JOptionPane;
public class Cars{
public static void main(String [] args){

carinfo honda = new carinfo();
carinfo bmw = new carinfo();
carinfo lexus = new carinfo();
carinfo chevy = new carinfo();

int honda;
hondat = hondat + honda.price;

}

private class carinfo{


String model;
int year =0;
int numberofdoors = 0;
int horsepower = 0;
int cost =0;


for(int i = 0; i <= 5; i++){

 
String Model = JOptionPane.showInputDialog("Enter car model");
honda.model = String.parseString(Model);
String Years = JOptionPane.showInputDialog("Enter car year");
honda.year = Integer.parseInt(Years);
String Numberofdoors = JOptionPane.showInputDialog("Enter amount of doors");
honda.numberofdoors = Integer.parseInt(Numberofdoors);
String Horsepower =  JOptionPane.showInputDialog("Enter car horsepower");
honda.horsepower = Integer.parseInt(Horsepower);
String Cost =  JOptionPane.showInputDialog("Enter car price");
honda.cost = Integer.parseInt(Cost);
 
}

for(int i=0; i < 5; i++){

lexus.model = JOptionPane.showInputDialog("Enter car model");
String Yearss = JOptionPane.showInputDialog("Enter car year");
lexus.year = Integer.parseInt(Yearss);
String Numberofdoorss = JOptionPane.showInputDialog("Enter amount of doors");
lexus.numberofdoors = Integer.parseInt(Numberofdoorss);
String Horsepowerss =  JOptionPane.showInputDialog("Enter car horsepower");
lexus.horsepower = Integer.parseInt(Horsepowerss);
String Costss =  JOptionPane.showInputDialog("Enter car price");
lexus.cost = Integer.parseInt(Costss);
 
}

for(int i=0; i < 5; i++){
chevy.model = JOptionPane.showInputDialog("Enter car model");
String Yearsss = JOptionPane.showInputDialog("Enter car year");
chevy.year = Integer.parseInt(Yearsss);
String Numberofdoorsss = JOptionPane.showInputDialog("Enter amount of doors");
chevy.numberofdoors = Integer.parseInt(Numberofdoorsss);
String Horsepowersss =  JOptionPane.showInputDialog("Enter car horsepower");
chevy.horsepower = Integer.parseInt(Horsepowersss);
String Costsss =  JOptionPane.showInputDialog("Enter car price");
chevy.cost = Integer.parseInt(Costsss); 

}

for(int i=0; i < 5; i++){
bmw.model = JOptionPane.showInputDialog("Enter car model");
String Yearssss = JOptionPane.showInputDialog("Enter car year");
bmw.year = Integer.parseInt(Yearssss);
String Numberofdoorssss = JOptionPane.showInputDialog("Enter amount of doors");
bmw.numberofdoors = Integer.parseInt(Numberofdoorssss);
String Horsepowerssss =  JOptionPane.showInputDialog("Enter car horsepower");
bmw.horsepower = Integer.parseInt(Horsepowerssss);
String Costssss =  JOptionPane.showInputDialog("Enter car price");
bmw.cost = Integer.parseInt(Costssss); 

}
}
}

#1: You don't need for loops. You have 4 different for-loops and inside each one you have the same code, without using the index i. If you look at one of them you will see that: the first time it takes for example the model and sets it to the honda. Then the second time again ti asks for the model and sets it again in the honda. 5 times you are setting the model to the honda, then 5 times you set it to the lexus, and so on. You should have an array of carinfo and each time you should be setting the model to a different carinfo instance.

#2: The for-loops are wrong. Inside them you use carinfo objects that declared outside the class. You don't set the values of an instance inside the class. You create a class and in the main you create instances and setting their values.

I will use only your code and show how it should be. By the way this is not the best way to do it but I will do it the way you imagined it.
First create a class in a different file: CarInfo.java

public class CarInfo{

public String model;
public int year =0;
public int numberofdoors = 0;
public int horsepower = 0;
public int cost =0;
}

Then in the main you will create the instances and set their values:
file: Cars.java

import javax.swing.JOptionPane;
public class Cars{
public static void main(String [] args){

CarInfo honda = new CarInfo();
CarInfo bmw = new CarInfo();
CarInfo lexus = new CarInfo();
CarInfo chevy = new CarInfo();

//int hondat;
//honda = hondat + honda.price;
//the above is logically wrong because honda you haven't set the price to honda yet.

String Model = JOptionPane.showInputDialog("Enter car model");
honda.model = String.parseString(Model);
String Years = JOptionPane.showInputDialog("Enter car year");
honda.year = Integer.parseInt(Years);
String Numberofdoors = JOptionPane.showInputDialog("Enter amount of doors");
honda.numberofdoors = Integer.parseInt(Numberofdoors);
String Horsepower =  JOptionPane.showInputDialog("Enter car horsepower");
honda.horsepower = Integer.parseInt(Horsepower);
String Cost =  JOptionPane.showInputDialog("Enter car price");
honda.cost = Integer.parseInt(Cost);

}
}

Inside the main you create 5 CarInfo objects and you are setting the value of the honda object. You don't need a for-loop.
Then repeat the code for the others, in the main:

bmw.model = JOptionPane.showInputDialog("Enter car model");
String Yearssss = JOptionPane.showInputDialog("Enter car year");
bmw.year = Integer.parseInt(Yearssss);
String Numberofdoorssss = JOptionPane.showInputDialog("Enter amount of doors");
bmw.numberofdoors = Integer.parseInt(Numberofdoorssss);
String Horsepowerssss =  JOptionPane.showInputDialog("Enter car horsepower");
bmw.horsepower = Integer.parseInt(Horsepowerssss);
String Costssss =  JOptionPane.showInputDialog("Enter car price");
bmw.cost = Integer.parseInt(Costssss);

If you want for-loop do something like this although since you are new I wouldn't recommended:

CarInfo [] carInfo =new CarInfo[5]; //carInfo is an array

for (int i=0;i<carInfo .length;i++) {
  carInfo[i] = new CarInfo(); //carInfo[i] is a CarInfo object. You must instantiate it using new

carInfo[i].model = JOptionPane.showInputDialog("Enter car model for car No: "+i);
String Yearssss = JOptionPane.showInputDialog("Enter car year for car No: "+i);
carInfo[i].year = Integer.parseInt(Yearssss);
String Numberofdoorssss = JOptionPane.showInputDialog("Enter amount of doors for car No: "+i);
carInfo[i].numberofdoors = Integer.parseInt(Numberofdoorssss);
String Horsepowerssss =  JOptionPane.showInputDialog("Enter car horsepower for car No: "+i);
carInfo[i].horsepower = Integer.parseInt(Horsepowerssss);
String Costssss =  JOptionPane.showInputDialog("Enter car price for car No: "+i);
carInfo[i].cost = Integer.parseInt(Costssss);
}

A good book that helped me learn java was Cadenhead Java 2. Try to search this forum for a thread about java books. Also from what I have seen in your code, there certain basic elements you don't understand about Object Oriented Prog.
You create classes, and these classes can be used to programs to create different instances. If it helps it is like when you declare int variables:
int i=0;
int j=0;
Both are int but have different values.
CarInfo car1=new CarInfo();
CarInfo car2=new CarInfo();
Both car1,car2 are CarInfo, but they are different. Note that you must use new to create a new object.
Then you can use car1, car2 to access the variables they have inside them.
But you don't instantiate the object itself inside it. You only declare the variables-attributes that the object should have. Then you can use instances of the object anywhere in your programs.
Try to put your classes in different files. For example if you wanted to use CarInfo in another program, you couldn't the way you wrote your program.
In the way I showed you, you have the CarInfo object in a separate file and you can use it in any class you want

This article has been dead for over six months. Start a new discussion instead.