Re: Arraylist
- From: "John W. Kennedy" <jwkenne@xxxxxxxxxxxxx>
- Date: Sun, 28 Jan 2007 18:51:06 -0500
chris1980 wrote:
Hi. i am creating a program in which a user can add a car, view the list of car, delete a car and search for a specific car. i can't go any further because i am having problems in creating a method of adding a car using Arraylist. i know i can't return the whole arraylist but then what am i suppose to return? i might have questions later on about deleting. here is my code.
[code]
import java.util.*;
import java.util.Arrays;
import java.util.Scanner;
class Car
{
public String addCar()
{
String make="";
String model="";
String year="";
ArrayList<String> cars = new ArrayList<String>();
Scanner s = new Scanner(System.in);
System.out.print("Enter make ");
cars.add(make);
System.out.print("Enter model ");
cars.add(model);
System.out.print("Enter the year ");
cars.add(year);
return cars;
}
}
class AddEntry
{
public static void main(String[] args)
{
Car car = new Car();
Scanner s = new Scanner(System.in);
String name;
String phone;
String address;
int choice;
System.out.println("*******************************************");
System.out.println("\t Wellcome to Mike DealerShip ");
System.out.println("*******************************************");
do {
System.out.println("Make a selection ");
System.out.println("1. Add a Car ");
System.out.println("2. View car list ");
System.out.println("3. Delete a car ");
System.out.println("4. Quit ");
System.out.print("Enter your choice plz: ");
choice = s.nextInt();
if(choice==1)
{
car.addCar();
}
} while(choice!=4);
}
}
[/code]
error:
C:\Java programs\AddEntry.java:20: incompatible types
found : java.util.ArrayList<java.lang.String>
required: java.lang.String
return cars;
Thanks for the help. and yes for searching i am thinking of using linear search but i read the api and i saw this iterator very useful in arraylist. any comments ?
This code is hopelessly, desperately confused. In all seriousness, you might as well be asking why it doesn't work when you try to drive in screws by hitting them with a saw.
Get a book about Java. If you already have one, get another. I, myself, am partial to Core Java2 (2 volumes) by Horstmann and Cornell.
For whatever it's worth, here's a version of your code that works, as far as it goes. I'm not saying it's a good design -- just that it will compile and run. There are still many questions to ask.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Car {
String make;
String model;
String year;
public Car(Scanner s) {
System.out.println("Enter make ");
make = s.nextLine();
System.out.println("Enter model ");
model = s.nextLine();
System.out.println("Enter the year ");
year = s.nextLine();
}
}
class CarList {
private List<Car> list = new ArrayList<Car>();
public void addCar(Scanner s) {
list.add(new Car(s));
}
}
class AddEntry {
public static void main(String[] args) {
CarList carlist = new CarList();
Scanner s = new Scanner(System.in);
int choice;
System.out.println("*******************************************");
System.out.println("\t Wellcome to Mike DealerShip ");
System.out.println("*******************************************");
do {
System.out.println("Make a selection ");
System.out.println("1. Add a Car ");
System.out.println("2. View car list ");
System.out.println("3. Delete a car ");
System.out.println("4. Quit ");
System.out.print("Enter your choice plz: ");
choice = s.nextInt();
s.nextLine(); // Discard the rest of the line
if (choice == 1) {
carlist.addCar(s);
}
} while (choice != 4);
}
}
--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
.
- Follow-Ups:
- Re: Arraylist
- From: chris1980
- Re: Arraylist
- References:
- Arraylist
- From: chris1980
- Arraylist
- Prev by Date: Arraylist
- Next by Date: Re: New line in a JTextArea (student learning here)
- Previous by thread: Arraylist
- Next by thread: Re: Arraylist
- Index(es):
Relevant Pages
|
|