Re: Arraylist



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"
.



Relevant Pages

  • Re: About absolute reference frame......
    ... The centripetal force is real, ... door of a car pushing sideways on you as the car turns a corner. ... One person might say there is a force backwards on the voodoo head. ... The only agent available for such a force is the string. ...
    (sci.physics.relativity)
  • Re: hmmm ... a solution but not a high order procedure.
    ... Since ones that go together are likely to be adjacent in a sorted list, if you compare each pair of adjacent filenames with some sort of string difference function, and limit it to some magic number for "these two things are really different", then you can use my partition function to sort them for you. ... you stayed with car cdr and a couple of minor procedures ... ... (define (odd-numbers message liste) ...
    (comp.lang.scheme)
  • Re: hmmm ... a solution but not a high order procedure.
    ... you stayed with car cdr and a couple of minor procedures ... ... (define (position char string) ... Yes, you're right, higher order function for higher order ... (define (odd-numbers message liste) ...
    (comp.lang.scheme)
  • Re: non-lexicographic string comparison
    ... (define str-num-str car) ... (define str-num-num cdr) ... ((and (string? ...
    (comp.lang.scheme)
  • Re: Newbie - need a small push on recursion / higher order procedures
    ... seems that first referred to the first 'word' of the string sentence defined ... 'rest are the equivalent of first and rest (car and cdr) for lists??? ... (define (odd-numbers message liste) ...
    (comp.lang.scheme)