Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

Arraylist

Thread view: 
chris1980 - 28 Jan 2007 23:06 GMT
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.println("Enter make ");
        cars.add(make);
        System.out.println("Enter model ");
        cars.add(model);
        System.out.println("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 ?
John W. Kennedy - 28 Jan 2007 23:51 GMT
> 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
[quoted text clipped - 67 lines]
> 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);

    }
}

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

chris1980 - 29 Jan 2007 00:19 GMT
ok thanks for the code. i know now what you did and what i was doing
wrong. i was getting confused in how to store the car added by the
user?. though arraylist looks good to me but have never used one. so
still i am going to have problems in creating a method for deleting a
car and viewing the car. any good explanation on how should i acheieve
this goal? i am just writing this program to test my java skills. yes
i got through in reading the first book for java.
Thanks
John W. Kennedy - 29 Jan 2007 01:54 GMT
> ok thanks for the code. i know now what you did and what i was doing
> wrong. i was getting confused in how to store the car added by the
[quoted text clipped - 3 lines]
> this goal? i am just writing this program to test my java skills. yes
> i got through in reading the first book for java.

You want to add additional methods to the CarList class, one to delete,
and one to list.

Before doing this, you have to think about how you want to do the
delete. Without a significant redesign, about the only thing you can do
is A) list everything, including a number, and B) delete based on the
number listed. (In a GUI application, you would use a checkbox instead.)
Or, of course, you could ask for make, model, and year, and then scan
through everything -- but in a real application, it's rather peculiar to
ask for the user to enter the entire content of a record in order to
designate it for deletion. In order to do it otherwise, you'll have to
think of some way to pick one Car from another other than by place in
the list.

Also, the way it is now, with the dialog in the Car constructor, is
very, very unusual. It would be more usual to read the data as part of
the main, and then call a constructor with profile:

  Car (String make, String model, String year) { ... }

To go further, the actual variables in the Car object should be private,
and new getMake, getModel, getYear methods should be added.

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

chris1980 - 29 Jan 2007 15:52 GMT
John. thanks for a great explanation. I am glad someone was able to
take his time out and explain me a little much better. so what you
trying to say is :

class Car
{
 private String make;
private String model;
private String year;

public Car (String mk, String md, String yr) {
    this.make = mk;
    this.model = md;
    this.year = yr;
}

public String getMake() {
    return make;
}

public String getModel() {
  return mode;
}

public String getYear() {
   return year;
}

I think i am getting the idea of a good encapsulated program. i will
post back if i get stuck. i have good knowledge of java not very good
but good but very new to Arraylist. i thought of using arrays but when
i read the Api arraylist sounds good to me. Thanks alot Kennedy :).
Abhi - 30 Jan 2007 09:51 GMT
Hi Chris,

I think by now John has already hlpd u a lot.....
Well, ur error was that u were returning an arraylist by-->return
cars;  statement
but ur method signature was returning String-->public String addCar()
So u got this error-->C:\Java programs\AddEntry.java:20: incompatible
types
found   : java.util.ArrayList<java.lang.String>
required: java.lang.String
               return cars;
And if u want 2 sort etc u can use the Arrays class
Regards

> import java.util.*;
> import java.util.Arrays;
[quoted text clipped - 19 lines]
>
> }
chris1980 - 30 Jan 2007 17:56 GMT
oh yes john have explained me alot. i was trying to show him that i
know java i am not a newbie and tried to convert his
english(explanation) into my programming skills. Anyways thanks one
more time.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.