File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes List or arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List or arrays" Watch "List or arrays" New topic
Author

List or arrays

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey guys

I need to display a table showing a list of products, such as
product price unit
apple 1.00 3
pear 2.00 4

can some one please get me started on this, thanks
also i will need to add a method for displaying Total price
such as
product price unit total
apple 1.00 3 3.00
pear 2.00 4 8.00

if anyone can give me simple code and explanation and steps i need to take in other to get started, be much apperciated
thanks mark
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
I don't know how skilled you are in OO, but your items should probably as multiple Fruit objects with type, price, and quantity attributes. Total could be done as an attibute re-computed when one of your price or quantity changing methods are accessed, or computed on the fly when your data is displayed.

As objects the items can held in a Collection. A Collection type that links a fruit name to an Object could be a good idea.


Darryl Failla
Sun Certified Java 2 Programmer
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
First you need to decide (and indicate) whether your application is going to be a GUI or Command Line application.

You probably should create a Fruit class, with properties for "product", "price", and "unit", and a method that gets the total. The interface might look something like this:



You will also probably want to use a collection to store your fruits. Probably, an implementation of "Set" would be the most appropriate type of collection, because you don't want a particular fruit on the list twice (I'm assuming).

You will probably want to create a Builder class which you create, pass the collection to, and then invoke to create your table. If you go with a command line application, you should return a string. If you go with a GUI application, you will likely want to return a JComponent (such as a JTable). Or if you want to be more flexible, provide both, and then you can use either one.

eg:




The rest is all pretty easy. You might want to create you're own table model for your JTable to include appropriate headers. In the command line version, you might want to figure out what sort of justification you want, and figure out how to pad the fields to the right lenght with the correct justification. Also, you will want to look into NumberFormatter, because if the float value is a whole number, then Float.toString will return a string with no decimal places.

Good Luck!

- Adam

hmm... apparently, the ubb formatter doesn't support the color tag...
[ July 18, 2006: Message edited by: Adam Nace ]
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
Your choice of Collection type should be determined by your requirements. If you are required to always list all objects, a Set may work for you. Set will only prevent duplicate entries if an attempt is made to add another copy of the same object. Set entries will not "know" that there are two different Fruit objects with the type attribute of "apple". If a new apple Fruit object is created somehow, it could be stored in the Set.

If your requirement is to be able to pick particular Fruit out of the Collection, Set is not the best option. With a Set, you will be required to iterate through the Set each time, until you find what you are looking for. A Map is better suited for this application. Each fruit name could be used as the key and its corresponding Fruit object as the value.

This would allow for easy "fruit picking" and also ensure that only one Fruit object with the key of "apples", "bananas", or "oranges" exists.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Thanks guys,

Ill give that a shot

Mark
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey Guys,

Actually i have to change how the prog works now, As of now i read in a very basic csv file
pear,1.50,2

then use split() to seperate the commas to get it looking like
pear 1.50 2

code is as follows
String[] textfile = {result}; //this holds the contents of the file
for (int i = 0; i < textfile.length; i++) {
String[] piece = textfile[i].split("[,]");
for (int j = 0; j < piece.length; j++)
System.out.print(piece[j] + "\t");
System.out.print("\n");

is it possible to somehow pass pear, 1.50, 2 into a variable of their own so i can display them in a JTable as follows

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Product",
"Price",
"Units",
"Total"};

private Object[][] data = {
{"Apple", new Integer(2), new Integer(2), new Integer(2*2)}
};
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey Guys,

Actually i have to change how the prog works now, As of now i read in a very basic csv file
pear,1.50,2

then use split() to seperate the commas to get it looking like
pear 1.50 2

code is as follows
String[] textfile = {result}; //this holds the contents of the file
for (int i = 0; i < textfile.length; i++) {
String[] piece = textfile[i].split("[,]");
for (int j = 0; j < piece.length; j++)
System.out.print(piece[j] + "\t");
System.out.print("\n");

is it possible to somehow pass pear, 1.50, 2 into a variable of their own so i can display them in a JTable as follows

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Product",
"Price",
"Units",
"Total"};

private Object[][] data = {
{"Apple", new Integer(2), new Integer(2), new Integer(2*2)}
};

Above is hard coded but i would like to transfer the contents of the array textfile into the table, thanks for your help so far guys and any other help appreciated.

Mark
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Well, if you've got it hard-coded, so you've got the hardest part done. (Incidentally, I would use Double or Float instead of Integer for the price...)

So what you need to do now is read the contents of the CSV file, translate the data to the appropriate data types, and build your Object[][] to use as the table model.

Here's some hints as to how to do this.

- You've got the "split" method for parsing the lines OK.
- Instead of printing them out again as you do now, translate and store them in some sort of variable/collection.
- The Wrapper classes (Integer, Double, Float, etc.) each have a constructor that takes a String as a parameter.
- java.util.List has a .toArray() method that you might be able to take advantage of.

Since this sounds like a homework assignment of sorts, I won't do much more than give hints/critique code.

One additional thing, don't over-engineer this -- unless the assignment is specifically focused on architecture instead of just learning how to program in Java. Architecture is good, but you'll need the basics first.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Thanks,

I now have the array divided into variables;
fruit = piece[0];
price = piece[1];
unit = piece[2];

Now since price has to be a double i converted it into a double;
price2 = Double.parseDouble(price);

which worked granded

but when i try to convert unit to an integer
unit2 = Integer.parseInt(unit);

i got error
Exception in thread "main" java.lang.NumberFormatException: For input string: "3
"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)

so i tried removing white spaces as follows
target = unit.replace(" ", "");
which worked and displayed grand, then i tried converting to int again but the same exception was thrown, can anyone help me with this problem, thanks
mark
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Did you try to parse unit or target?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
sorry what do you mean by parse?
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
i used unit.trim(); which done the trick and i was allowed to convert to integer then

thanks for all your help mark, also i found a good tutorial on list and collections that ill check out like you mentioned

Mark
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Originally posted by Mark Hughes:
i used unit.trim(); which done the trick and i was allowed to convert to integer then

That worked, because your input string had a newline character appended to it. So you were trying to parse "3<newline>" instead of just "3". Because "3<newline>" is not a number, you got a NumberFormatException. By calling trim() first, you stripped the whitespace (the <newline> character is also considered whitespace).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List or arrays