• Post Reply Bookmark Topic Watch Topic
  • New Topic

array and setmethod  RSS feed

 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Im getting compiler errors for this top part im not sure what im doing wrong


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing I see is that setMake returns no value (void). How can you assign it to CarList[0] ?
 
Muhammad usman shad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Look at your mistake Abhradeep....

Car is your class and myCar is your object to access it,
Now, you are making an array of objects which is a false approach.

Write this code and get required output...

{
Car myCar = new Car();

String carList[];
carList = new String[9];

carList[0] = myCar.setMake("Toyota","Altis", "SJC2456 X", "100", "60");

and so on upto carList[8];

}

[Removed. Please UseTheForumNotEmail]


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, consider naming your array simply "cars". The name "carList" is misleading, as it's not really a list.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general tip, if you say "I get a compiler error with this code", it helps if you post the exact text of the message. It really, truly does tell you a lot about what the problem(s) is/are.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
database.java


Car.java



im putting the main for it to run it the main supposed to be in another file

heres the attachment from imageshack



sorry if I didnt reply earlier didnt see the msges in my email

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your code to include only the necessary bits--commented-out code doesn't help.

You're trying to run code outside of any method. You're also trying to set a reference expected to be a Car to a string, via a method that's defined to return a string, but doesn't return anything at all.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
er what should i set it as??
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, sorry; I mis-read the code--you're right, it's a String array.

The code still needs to be in a legal place, though, and the method you're calling actually needs to return a string.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, rather than posting screenshots, copy-and-paste the text. It's easier to deal with.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
err so what do i do? im stuck at this for weeks lol

o the text is from command prompt
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know where the text is from--cut and paste from the command prompt.

Since I'm unsure what you're trying to accomplish, it's hard to advise how to move forward. The first step would be to move the code that's not in a method into a method. It *looks* like you meant it to be in the main method, but that's just a guess. The second step would be to actually return a string from the method declared to return a string.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically in the main method when i type in the model of the car example honda it should compare with the array then all the details should display if any

in the class Database the methods would be
findCarmodel
showdetails return type void
calculatecost
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your new code look like?
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still have code outside a method (line 12, but I suspect 10-12 aren't where you want them), and now you're trying to set a carList entry to the results of a void method.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what do at this part => set a carList entry to the results of a void method.

DATABASE java file

CAR java file
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setCar(...) needs to return a value.
 
Abhradeep Banerjee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
err then whats the point of having a get method???
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The traditional setter doesnt return any values. But in your code, you have assigned it to carList[0].
So a value must be returned from setCar. David was referring to that in his posts.

By the way, the setCar() actually can be converted to a constructor. You would also have compile error as you are trying to pass "100" instead of 100 (int).

hth
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!