freelanceprogrammers.org Forum Index » Java

Vectors


View user's profile Post To page top
dale_erwin Posted: Sat Sep 07, 2002 5:53 am


Joined: 11 Apr 2004

Posts: 11
Vectors
Since I have no way of knowing how many items I will be reading
from the database, I have no way of sizing an array. Therefore, I
thought maybe using Vectors would be a viable alternative. The
only trouble is that I can`t figure out how to assign objects to a
Vector in a loop. I tried this:

...
Vector yearBox = new Vector();

j = 0;
while(getNext()) {
// year box
yearBox.add(j, new JComboBox()); <--- error in this line
for (int i = 2000; i < 2021; i++) {
yearBox.get(j).addItem("" + i);
}
yearBox.get(j).setSelectedItem(curItemYear);
j++;
}
...

but I get a compile error "cannot resolve symbol" pointing to "new"
in the line pointed to above. So, I tried this:

...
Vector yearBox = new Vector();

j = 0;
while(getNext()) {
// year box
JComboBox tjcb = new JComboBox;
yearBox.add(j, tjcb);
for (int i = 2000; i < 2021; i++) {
yearBox.get(j).addItem("" + i);
}
yearBox.get(j).setSelectedItem(curItemYear);
j++;
}
...

and I get no compile error. However, since tjcb is an object, I got
to thinking that all the variables in the Vector are going to be
referencing the same object. If this is true, how does one go about
putting objects in a Vector like this? The only example I have
been able to find is with strings which behave differently.
--
Dale Erwin
Salamanca 116
Pueblo Libre
Lima 21, PERU
Tel.51(1)461-3084
Cel.51(1)743-6439
Reply with quote
Send private message
View user's profile Post To page top
yingli75 Posted: Sat Sep 07, 2002 10:56 pm


Joined: 07 Sep 2002

Posts: 1
Vectors
Hi,
As for my previous program, which loads, prints, and exits a file, a problem
occurs when I chose an option. It seems that System.in.read() reads twice every
time I input a number as a choice. I am thinking may be `hitting enter key`
following my input is the problem. But I have no way to discard the `enter`, nor
knowing whether or not this is really the case.
Here is my codes, the attachment of this email is the text file which serves as
the file needed to be loaded and printed out.

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;;

public class IOProgram {
private static final BufferedReader stdinReader = new BufferedReader(new
InputStreamReader(System.in));
public BufferedReader reader = stdinReader;
BufferedReader contents;
// InputStreamReader inputReader = new InputStreamReader();
String filename;
String quanSpace;
String priceSpace;

public static void main(String args[]) {
IOProgram io = new IOProgram();
io.choose();
}
public void choose() {
System.out.println("
Please choose an option:");
System.out.println("1. Load a file" );
System.out.println("2. Print the file" );
System.out.println("3. Exit");
int choice = 0;
try {
choice = System.in.read();
// System.out.println(choice + "this is choice" );
//check if choice =1,2 or 3;
switch (choice) {
case 1: load();
case 2: print();
case 3: exit();
default: {
System.out.println("invalid input.");
choose();
}
}
}
catch (IOException e) {
System.out.println("Not a valid choice");
}
}

public void load() throws RuntimeException {
System.out.println("Please enter the file directory and name:");
try {
BufferedReader reader = getReader();

filename = reader.readLine();
filename = reader.readLine();
contents = getContents(filename);
choose();
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
public void print() throws RuntimeException {
int i = 0;

try {
String firstLine = contents.readLine();
System.out.println(firstLine);

String line = contents.readLine();
while (line.length()!= 0) {
String itemNo = nextItemNo(line);
System.out.print(itemNo);

String desc = nextDesc(line);
System.out.print(desc);

int quan = nextQuan(line);
System.out.print(quanSpace + quan);

double price = nextPrice(line);
DecimalFormat two = new DecimalFormat();
two.setMinimumFractionDigits(2);
two.setGroupingUsed(false);
System.out.println(priceSpace + two.format(price));

line = contents.readLine();
i++;
if (i > 15) {
System.out.println("this is last line: " + line);
}

}
choose();
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}

public BufferedReader getContents (String file) {
File details = new File(file);
if (!details.exists())
throw new RuntimeException(file + " does not exist.");
else if (!details.canRead())
throw new RuntimeException(file + " exists but is unreadable.");
else if (!details.isFile())
throw new RuntimeException(file + " is not a regular file.");
else {
try {
BufferedReader br = new BufferedReader( new
FileReader(details) );
return br;
}
catch (FileNotFoundException e) {
throw new RuntimeException("Failed to open " + file + " for
an unknown reason.");
}
catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}

public String nextItemNo(String line) throws RuntimeException {
String itemNo = line.substring(0, 13);
return itemNo;
}
public String nextDesc(String lineHere) throws RuntimeException {
String line = lineHere;
String desc = line.substring(14, 59);
return desc;
}
public int nextQuan(String lineHere) throws RuntimeException {
String line = lineHere;
String strQuan = line.substring(60, 68);
int length = strQuan.length();
String trimedStrQuan = strQuan.trim();
int trimedLength = trimedStrQuan.length();
int diff = length - trimedLength;
quanSpace = strQuan.substring(0, diff-1);
int quan = Integer.valueOf(trimedStrQuan).intValue();
return quan;
}
public double nextPrice(String lineHere) throws RuntimeException {
String line = lineHere;
String strPrice = line.substring(69, 78);
int length = strPrice.length();
String trimedStrPrice = strPrice.trim();
int trimedLength = trimedStrPrice.length();
int diff = length - trimedLength;
if (diff > 0)
priceSpace = strPrice.substring(0, diff+1);
else {
priceSpace = " ";
}
double price = Double.valueOf(trimedStrPrice).doubleValue();
return price;
}


public void exit() {
System.out.println("program terminated.");
System.exit(0);
}
private BufferedReader getReader() {
return reader;
}
}



---------------------------------
Do You Yahoo!?
Yahoo! Finance - Get real-time stock quotes
----------

Item Number Description Quantity Price
00010 Gold Plated Mouse 100 124.99
00020 Bottle of Chateau Ypres 1939 50 2039.95
00021 Luxury Yacht 10 100399.00
00022 Custom Watermarked Linen Sheet of Paper 10010 1.15
00110 Diamond Encrusted Staple Remover 3 89.99
00120 Gold Plated Dinnerware 79 5000.00
00121 Ermine Mousepad 518 49.92
00122 Sterling Silver Staple 8904 0.25
00123 Silver Coffee Service 2643 3014.75
02001 Redwood Pencil 1 1.00
02005 Half Carat Diamond 694 899.95
02120 Sterling Silver Scissors 3 36.18
12123 Platinum Toothpick 608 249.49
12225 Mink Potholders 92 1124.50
53124 Satin Sheets 12 750.00
84976 Pre-owned Lexus 65 24999.99



[Non-text portions of this message have been removed]
Reply with quote
Send private message
View user's profile Post To page top
koldark1 Posted: Mon Sep 09, 2002 2:11 am


Joined: 09 Sep 2002

Posts: 1
Vectors
Hello Dale,

Friday, September 6, 2002, 7:53:24 PM, you wrote:
DE> but I get a compile error "cannot resolve symbol" pointing to "new"
DE> in the line pointed to above.  So, I tried this:

Have you imported java.util.Vector?

--
Best regards,
Mike mailto:programmer@...
Reply with quote
Send private message
View user's profile Post To page top
dale_erwin Posted: Mon Sep 09, 2002 3:44 am


Joined: 11 Apr 2004

Posts: 11
Vectors
Mike Wills wrote:
>
> Hello Dale,
>
> Friday, September 6, 2002, 7:53:24 PM, you wrote:
> DE> but I get a compile error "cannot resolve symbol" pointing to "new"
> DE> in the line pointed to above. So, I tried this:
>
> Have you imported java.util.Vector?

Yes, now I do. I didn`t then, though. I still could not get the objects
into a vector, so I abandoned the Vector idea. Now I`m doing a database
read of SELECT COUNT(*) first and then sizing my arrays, which seems
to be working so far.
--
Dale Erwin
Salamanca 116
Pueblo Libre
Lima 21, PERU
Tel.51(1)461-3084
Cel.51(1)743-6439
Reply with quote
Send private message
View user's profile Post To page top
boinepally Posted: Fri Mar 26, 2004 11:04 am


Joined: 26 Mar 2004

Posts: 2
Vectors
How do I create a 2-dimensional vecor. It should let me store a
label and value.
For Ex "Red" "apple", "Blue" "Berry", "Green" "grapes".

Please help!
Reply with quote
Send private message
View user's profile Post To page top
nitrusrit Posted: Fri Mar 26, 2004 7:09 pm


Joined: 10 Apr 2004

Posts: 19
Vectors
Why not just use a HashMap? The alternative is to have a vector of vectors, or
perhaps a vector of a custom class that you write which has a label and value.

Dustin

boinepally <boinepally@...> wrote:
How do I create a 2-dimensional vecor. It should let me store a
label and value.
For Ex "Red" "apple", "Blue" "Berry", "Green" "grapes".

Please help!



---------------------------------
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.

[Non-text portions of this message have been removed]
Reply with quote
Send private message
View user's profile Post To page top
hazins Posted: Fri Mar 26, 2004 7:29 pm


Joined: 16 Apr 2004

Posts: 15
Vectors
I would look at the Hashtable API

>>> boinepally@... 3/26/2004 12:04:46 AM >>>
How do I create a 2-dimensional vecor. It should let me store a
label and value.
For Ex "Red" "apple", "Blue" "Berry", "Green" "grapes".

Please help!





Yahoo! Groups Links








[Non-text portions of this message have been removed]
Reply with quote
Send private message
View user's profile Post To page top
boinepally Posted: Mon Apr 19, 2004 7:24 am


Joined: 26 Mar 2004

Posts: 2
Vectors
Thanks for the response...It was very helpful
If the hashMap is empty , is it safe to return a null or an empty
HashMap ?



--- In beginnersclub@yahoogroups.com, Dustin Metzgar
<dustin_metzgar@y...> wrote:
> Why not just use a HashMap? The alternative is to have a vector
of vectors, or perhaps a vector of a custom class that you write
which has a label and value.
>
> Dustin
>
> boinepally <boinepally@y...> wrote:
> How do I create a 2-dimensional vecor. It should let me store a
> label and value.
> For Ex "Red" "apple", "Blue" "Berry", "Green" "grapes".
>
> Please help!
>
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Finance Tax Center - File online. File on time.
>
> [Non-text portions of this message have been removed]
Reply with quote
Send private message
View user's profile Post To page top
nitrusrit Posted: Mon Apr 19, 2004 7:53 am


Joined: 10 Apr 2004

Posts: 19
Vectors
I think returning an empty hashmap (or any other data collection) from a method
call is pretty standard practice. A lot of people have a tendency to depend on
the collection coming back as being a valid collection and often don`t check for
a null value before performing an operation.

Dustin

boinepally <boinepally@...> wrote:
Thanks for the response...It was very helpful
If the hashMap is empty , is it safe to return a null or an empty
HashMap ?



---------------------------------
Do you Yahoo!?
Yahoo! Photos: High-quality 4x6 digital prints for 25¢

[Non-text portions of this message have been removed]
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help