views:

129

answers:

4

For example I've

3 books: Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int).

now I want to have an array called books[3][4].

I'm gettin the data I set via setBooknumber like this:
Book1.getBooknumber(), Book1.getBooktitle(),...,Book3.getBookprice().

Now how do I realize this books[3][4] array. I can't call it String books[][] = new String [3][4]. Because I can't get Booknumber (int) into it.

I don't want Booknumber to be String neither Bookprice. How do I realize it, please?

Now to further elaborate it.

I have 2 classes.

public class book{
String Booktitle, Booklanguage;
int Booknumber, Bookprice;

//constructor

//get

//set
}

and

public class bookUI
{
 public static void main(String arg[])
 {
   book book1 = new book();
   book book2 = new book();
   book book3 = new book();

   book1.setBooktitle();
   ...
   book3.setBookprice();

   //Here I want to have books[3][4] Array. And gettin the data via book1.get...book3.get into the array
 }
}
+2  A: 

Notice the repetition of Book in Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int)- it screams for a class type.

class Book {
  int number;
  String title;
  String language;
  int price;
}

Now you can simply have:

Book[] books = new Books[3];

If you want arrays, you can declare it as object array an insert Integer and String into it:

Object books[3][4]
Chandra Patni
Works, but I'd be pissed with all the casting when trying to read data.
chris
What? (15 char limit)
NoCanDo
Now that it's been edited, my comment looks a bit moronic. +1 for the edit regarding the repetition of "book" in the field names. That's a smell that does indeed scream for refactoring.
chris
+6  A: 
public class Book
{
    public int number;
    public String title;
    public String language;
    public int price;

    // Add constructor, get, set, as needed.
}

then declare your array as:

Book[] books = new Book[3];

EDIT: In response to O.P.'s confusion, Book should be an object, not an array. Each book should be created on it's own (via a properly designed constructor) and then added to the array. In fact, I wouldn't use an array, but an ArrayList. In other words, you are trying to force data into containers that aren't suitable for the task at hand.

I would venture that 50% of programming is choosing the right data structure for your data. Algorithms naturally follow if there is a good choice of structure.

When properly done, you get your UI class to look like: Edit: Generics added to the following code snippet.

...
ArrayList<Book> myLibrary = new ArrayList<Book>();
myLibrary.add(new Book(1, "Thinking In Java", "English", 4999));
myLibrary.add(new Book(2, "Hacking for Fun and Profit", "English", 1099);

etc.

now you can use the Collections interface and do something like:

int total = 0;
for (Book b : myLibrary)
{
   total += b.price;
   System.out.println(b); // Assuming a valid toString in the Book class
}
System.out.println("The total value of your library is " + total);
chris
I second this comment. You should strongly consider creating a custom type for this, rather than sticking it in a multidimensional array with a generic Object type. Constant casting is a sign of bad design.
Jason Nichols
I don't understand it. Can you please explain further?
NoCanDo
He's saying a book should not be an array. It's an aggregate type, and should be represented by a class.
Chuck
"Each book should be created on it's own (via a properly designed constructor) and then added to the array." THAT'S WHAT I WANT ;). Excatly what I want. I want create 3 objects (3 books), set all the data, and give it to an array. Then I want to call via book[0][0] the booknumber for example.
NoCanDo
I cannot understand why you insist on `book[0][0]` that much. That is not how it works in Java. If you want to do it like this you have to choose e.g. PHP.
Felix Kling
A: 

Why not create a class Book with properties: Number, Title, and Price. Then store them in a single dimensional array? That way instead of calling

Book[i][j] 

..to get your books title, call

Book[i].Title

Seems to me like it would be a bit more manageable and code friendly.

George
Because it's not what I want.
NoCanDo
Why is it a problem to do it this way? Besides being probably the only way the code is way more easier to understand.
Felix Kling
Becaus I still don't want it and it's not what I'm trying to understand. I'm selflearning Java. It doesn't help me when folks are trowing hunderds of different codesolutions. I want to stay within MY given "codeframe", so I can understand it and THEN move to something different.
NoCanDo
@NoCanDo: If you're self-learning, then you should listen to the people who know what they're doing when they tell you you're going about something the wrong way. And people are not throwing hundreds of different solutions at you — they're suggesting the same thing, which is to make a Book class.
Chuck
Yea, well that might be. But often it's like this analogy: one is learning basic geometry and folks start bombarding him/her with advanced differential calculus. Whereas he/she has no idea about differential calculus. Not in this case right here, but quite often the case on stackoverflow.I mean all the solutions work if I implement them, but those weren't what I was looking for. If it doesn't work, then it doesn't work. I'd appreciate it if folks could explain a beginner WHY and HOW it doesn't work, and not immediately start throwing other, quite better, solutions. It doesn't help at all.
NoCanDo
Chris explained it, so this issue is moot ;).
NoCanDo
+1  A: 

use object type ie Object books[3][4];

GK