having problem with array of arrays


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: having problem with array of arrays

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    9

    Unhappy having problem with array of arrays

    hi! am quite confused with a program that I need to do. I have to make a program for a collection of basketball players. Each player comes with information like age, name, height, and others. I have to make use of arrays only to store those information. The thing is, I also need to have features like adding records, deleting, searching,displaying. Should I make use of a switch to add these features? I am also having trouble with the adding record feature. Where should I start? Anybody who knows?.. I badly need help....

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Do you know how to create a "player" class which would hold the information, or are you looking at the implementation of parallel arrays to hold the information?

    If you are planning to have a "team" which is a collection of instances of the "player" class, then the "team" object could use an array to store its player objects. Your player class would know how to construct a player instance, "toString" the players, and how to compare them. Your team class would have the methods for inserting a player into its storage, for finding a player, for deleting the player, for finding the player instance and asking it to display its name or other information.

    If you are using parallel arrays, then the different kinds of information for each of the players would be held at the same index of each of the arrays. To assemble the information for player "i", you could then access name[i], address[i], age[i], height[i], weight[i], experience[i], etc.

  3. #3
    Join Date
    Mar 2007
    Posts
    9

    re: having problem with array of arrays

    hi! thanks for the reply... i am just wondering, should i use this kind of array declaration?

    char[][] team = new char[][];

    am familiar with array but am still quite confused with a 2D array

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    what information would a 2-dimensional array of char be holding?

    my thought is that you would have a class Team and one of the data fields of the class would be Player[] players = new Player[N] where N is the number of players.

  5. #5
    Join Date
    Mar 2007
    Posts
    9
    actually, am still thinking what info that array would be holding...
    but can i possibly use class for the "team" and array for information about each players? if that would be the case, how would i display say, all information about a specific player? the thing is, i would have to use arrays for the information and not on the "team".

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    That is why I asked you how much you know about constructing a class. The topic for this thread refers to "array of arrays" and I don't know if your aim is to implement everything using single dimension and multi-dimension arrays - and that's it - or by a more OOP-based approach which is more often the basis for writing programs in Java.

    A Player class would hold information about a particular player and provide methods to manipulate that information:

    class Player implements Comparable

    A Player class could have data fields such as:

    private string firstName;
    private string lastName;
    private string college;
    private int uniformNumber;
    private int age;
    private int weight;
    private float height;

    and various methods which operate on these data members:

    [constructors]
    Player();
    Player( string firstName, string lastName, ..... )

    [setters and getters]
    void setName (string firstName, string lastName)
    string getName()
    void setAge ( int age );
    int getAge();
    int compareTo( Player otherPlayer );
    string toString();

    Then, you make a Team by associating several Players with the team, storing the player objects in an array or other Collection.

    If you are limited to the procedural-based approach, only creating arrays which hold data, then let us know that limitation and post some code showing how you are thinking you will approach organizing these arrays (remembering the limitations on arrays)
    Last edited by nspils; 03-29-2007 at 07:04 AM.

  7. #7
    Join Date
    Mar 2007
    Posts
    9
    hi! i get the logic now... actually, i just wanna ask if I need to put a BufferedReader for every method say, the setName(), wherein there should be an input from the user? i actually tried it but there was an error.. something like unreported exception java.io.IOexception; must be caught or declared to be thrown..
    thanks for the idea anyway.. it helped a lot.. am almost done with the program....

  8. #8
    Join Date
    Mar 2007
    Posts
    9
    i mean the getName() method.... not the setName... am stuck with that error... i tried adding catch (IOException e) {} ... the problem now is that it cant find IOException symbol... why is that? do you have any idea?

  9. #9
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    A BufferedReader is for reading a stream - file or other input stream. You would use one per stream, opening and reading and closing as needed. So you might associate a reader or scanner to the "console" input stream and then open it to read input and then close it - not create a new one.

    Normally, a method like "getName()" is a request from a process from outside of the object asking the object to share or report the content of a "private" piece of data. The outsider cannot (should not be able to) manipulate the data directly. You are asking the class to send me the data so that I can use it. Normally the object will already have the data, and will just "return" that data to the calling process. There will not be a stream involved, there won't be any input from the user.

    You certainly can structure your program as you want, but normally the process of asking a user for data to be input or reading from a file or other stream occurs in a module of its own and not as a part of a getter. I would think that your "user" will be in the Team class ... that is where method main will be, that will be the spoke at the center of your program. So, inside an instance of Team you don't have access to the data of each player ... you have to ask that instance for the data, you need to provide to the instance's interface the data you want it to store (setter method). You should not be able to manipulate "playerX.firstName" from the Team instance - you would ask the instance to return the firstName by calling playerX.getName() and assigning it to a temporary string variable in the Team instance. No stream involved, no BufferedReader needed.

    If you want to read more, search "java getter" in Google and you'll find all sorts of articles from JavaPro and JavaWorld and other sources.

  10. #10
    Join Date
    Mar 2007
    Posts
    9
    i've here a sample that i've tried to do... am not sure about the array and also how will I be able to do the association for this particular program? pls help....
    import java.io.*;



    public class coursework{

    private String plate;
    private String brand;
    private int price;
    private int speed;
    private String color;
    private String model;

    public void setPlate(String plate)
    {
    this.plate = plate;
    }


    public void setBrand(String brand)
    {
    this.brand = brand;
    }

    public void setPrice(int price)
    {
    this.price = price;
    }

    public void setSpeed(int speed)
    {
    this.speed = speed;
    }

    public void setColor(String color)
    {
    this.color = color;
    }

    public void model(String model)
    {
    this.model = model;
    }

    public void getInfo(){
    String carPrice;
    String carSpeed;
    try{
    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter plate no. of car:");
    plate=input.readLine();
    System.out.println("Enter brand of car:");
    brand=input.readLine();
    System.out.println("Enter price of car:");
    carPrice=input.readLine;
    price=Integer.parseInt(carPrice);
    System.out.println("Enter speed of car:");
    carSpeed=input.readLine();
    speed=Integer.parseInt(carSpeed);
    System.out.println("Enter color of car:");
    color=input.readLine();
    System.out.println("Enter car model:");
    model=input.readLine();
    }catch(IOexception e) {}
    }


    }

    public static void main (String args[])throws Exception
    {
    coursework cs = new coursework();

    carArr[] carArrs = new carArr[];
    for (int x=0;x<size;x++)
    carArrs[x] = new carArr();
    carArrs[x].cs.plate;
    carArrs[x].cs.brand;
    carArrs[x].cs.price;
    carArrs[x].cs.speed;
    carArrs[x].cs.color;
    carArrs[x].cs.model;

  11. #11
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    I don't think this expresses what you want to do, and it sure won't work since you cannot address private data in this way.

    Code:
    carArr[] carArrs = new carArr[];
    for (int x=0;x<size;x++)
    carArrs[x] = new carArr();
    carArrs[x].cs.plate;
    carArrs[x].cs.brand;
    carArrs[x].cs.price;
    carArrs[x].cs.speed;
    carArrs[x].cs.color;
    carArrs[x].cs.model;
    It seems to me that you would want to have

    Code:
    public class Coursework
    {
         private String plate;
         private String brand;
         private int price;
         private int speed;
         private String color;
         private String model;
    
         public void setPlate(String plate)
        {
            this.plate = plate;
        }
    
        public void setBrand(String brand)
        {
            this.brand = brand;
        }
    
        public void setPrice(int price)
        {
            this.price = price;
        }
    
        public void setSpeed(int speed)
        {
            this.speed = speed;
        }
    
        public void setColor(String color)
        {
            this.color = color;
        }
    
        public void setModel(String model)
       {
           this.model = model;
       }
    
        public string getPlate()
        {
            return plate;
        }
    
        public string getBrand()
        {
            return brand;
        }
    
        public int getCarPrice()
        {
            return carPrice;
        }
    
        public int getCarSpeed()
        {
            return carSpeed;
        }
    
        public string getColor()
        {
            return color;
        }
      
        public string getModel()
        {
            return model;
        }
         toString(0)?  compareTo()? any other methods?
    }

    And then you will want to create a "TestCS" class

    in your TestCS class
    Code:
    public class TestCS
    
    public Coursework createCS() 
    {
        Coursework tempCS = new Coursework; 
        try
        {
             BufferedReader input=new BufferedReader(new inputStreamReader(System.in));
         }
         catch( IOException e )
        {
             System.out.println( "Could not initialize the input reader");
        }
    
        System.out.println("Enter plate no. of car:");
        String plate=input.readLine();
        tempCS.setPlate(plate);
    
        System.out.println("Enter brand of car:");
        String brand=input.readLine();
        tempCS.setBrand( brand );
    
        System.out.println("Enter price of car:");
        String carPrice=input.readLine;
        int price=Integer.parseInt(carPrice);
        tempCS.setPrice( price );
    
        System.out.println("Enter speed of car:");
        string carSpeed=input.readLine();
        int speed=Integer.parseInt(carSpeed);
        tempCS.setSpeed( speed );
    
        System.out.println("Enter color of car:");
        String color=input.readLine();
        tempCS.setColor( color );
    
        System.out.println("Enter car model:");
        String model=input.readLine();
        tempCS.setModel( model );
    
        return tempCS;
    }
    
    public static void main (String args[])throws Exception
    {
        TestCS myTest = new TestCS;
    
        ArrayList<Coursework> myList = new ArrayList<Coursework>;
    
        HERE, CREATE A MENU DRIVEN - DO YOU WANT TO ADD ANOTHER?
        WHICH WILL TERMINATE WITH
    
        myList.add( this.createCS() );
     
    }

  12. #12
    Join Date
    Mar 2007
    Posts
    9

    anyone who wants to check this program?.. having a hard time..

    hi! thanks for the reply.. i tried the program but it didn't work.. can u kndly check on this one.... thanks again.....


    public class coursework{

    private String plate;
    private String brand;
    private int price;
    private int speed;
    private String color;
    private String model;

    public String getPlate()
    {
    return plate;
    }

    public String getBrand()
    {
    return brand;
    }

    public int getPrice()
    {
    return price;
    }

    public int getSpeed()
    {
    return speed;
    }

    public String getColor()
    {
    return color;
    }

    public String getModel()
    {
    return model;
    }

    public String compareTo(Object anotherPerson) throws ClassCastException {
    if (!(anothercoursework instanceof coursework))
    throw new ClassCastException("A Person object expected.");
    String anothercourseworkBrand = ((coursework) anothercourseworkBrand).getBrand();
    return this.brand - anothercourseworkBrand;
    }



    public static void main (String args[])throws Exception{
    coursework cs = new coursework();

    char choice;
    System.out.println("CAR COLLECTION MAIN MENU");
    System.out.println("[A] Display all cars information");
    System.out.println("[B] Add another record to the collection");
    System.out.println("[C] Search for a record");
    System.out.println("[D] Display records based on brand");
    System.out.println("[E] Exit");
    System.out.println("Please Enter your choice [A-E]:");
    choice=(char)System.in.read();
    System.in.read();System.in.read();
    choice=choice.toUpperCase();

    switch(choice)
    {
    case 'A':
    System.out.println("CARS INFORMATION");
    cs.displayCar();
    break;
    case 'B':
    System.out.println("ADD RECORD:");
    cs.addCar();
    break;
    case 'C':
    System.out.println("ENTER PLATE NO. OF CAR TO SEARCH");
    cs.searchCar();
    case 'D':
    System.out.println("LIST OF CARS BASED ON BRAND NAME");
    cs.displayBrand();
    case 'E':
    System.out.println("EXITING...");
    break;
    default:
    System.out.println("Invalid Choice![A-E only].");
    }


    class carcollect{
    coursework[] info = new coursework[100];
    int x=0;

    void addEntry(String plate, String brand, String price, String speed, String color, String model);
    {
    info[x] = new coursework();
    info[x].plate = plate;
    info[x].brand = brand;
    info[x].price = price;
    info[x].speed = speed;
    info[x].color = color;
    info[x].model = model;
    x++;
    }
    }




    public void addCar(){
    String carPrice;
    String carSpeed;
    try{
    BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter plate no. of car:");
    info[x].plate=input.readLine();
    System.out.println("Enter brand of car:");
    info[x].brand=input.readLine();
    System.out.println("Enter price of car:");
    carPrice=input.readLine;
    info[x].price=Integer.parseInt(carPrice);
    System.out.println("Enter speed of car:");
    carSpeed=input.readLine();
    info[x].speed=Integer.parseInt(carSpeed);
    System.out.println("Enter color of car:");
    info[x].color=input.readLine();
    System.out.println("Enter car model:");
    info[x].model=input.readLine();
    }catch(IOexception e) {}
    }

    public void displayCar(){
    System.out.println("ALL CARS INFORMATION");
    for(i=0; i<100;i++){
    coursework work = new coursework[i];
    String plate = coursework.getPlate();
    String brand = coursework.getbrand();
    String price = coursework.getprice();
    String speed = coursework.getspeed();
    String color = coursework.getcolor();
    String model = coursework.getmodel();
    System.out.println("Plate No. of Car:" + plate);
    System.out.println("Brand Name:" + brand);
    System.out.println("Price:" + price );
    System.out.println("Speed:" + speed);
    System.out.println("Color:" + color);
    System.out.println("Model:" + model);
    }
    }

    public void searchCar(){
    for(int i=0; i<100; i++){
    if(plate.equals(info[i].name))
    return info[i].brand;
    return info[i].price;
    return info[i].speed;
    return info[i].color;
    return info[i].model;
    }
    return null;
    }


    public void displayBrand(){
    Arrays.sort(persons);
    System.out.println("SORTED BY BRAND:");

    for (int i=0; i<4; i++) {
    coursework cw = coursework[i];
    String plate = coursework.getPlate();
    String brand = coursework.getbrand();
    String price = coursework.getprice();
    String speed = coursework.getspeed();
    String color = coursework.getcolor();
    String model = coursework.getmodel();
    System.out.println("Plate No. of Car:" + plate);
    System.out.println("Brand Name:" + brand);
    System.out.println("Price:" + price );
    System.out.println("Speed:" + speed);
    System.out.println("Color:" + color);
    System.out.println("Model:" + model);
    }
    }

  13. #13
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    It would be very helpful if you would report what kind of error you are getting (either compiler error or runtime error) and what error is being reported, rather than "I tried the program and it didn't work".

    You cannot call non-static methods (instance methods) by making a call to its class. Therefore, your "getter" calls in your main should be
    String plate = cw.getPlate();
    String brand = cw.getBrand();

    etc.

    There are a whole slew of problems with your code. Too numerous to work through in bulk through this medium. I suggest you speak with a TA or your instructor to test you on your basic understanding and point out where you need to restructure your code.

    First place to work ... starting from the bottom since it is what I see in my window ...
    It would be better to have the instances of your coursework class "report themselves" for you, rather than making six calls to get data and six print calls. How nice and easy would it be to make one statement,

    System.out.println( cw.toString() );

    rather than twelve statements.

Similar Threads

  1. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 10:57 PM
  2. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  3. Changing Array Length Problem
    By Damo in forum ASP.NET
    Replies: 3
    Last Post: 10-15-2002, 02:58 PM
  4. Array to bitmap problem
    By David Vowles in forum VB Classic
    Replies: 0
    Last Post: 10-12-2001, 12:32 PM
  5. array declarations in VB.NET
    By Bill McCarthy in forum .NET
    Replies: 14
    Last Post: 04-10-2001, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center