Problems with map implementation...help me!!!Please...

G

gbattine

Hi guys,
i'm a new JSF user and i have a big problem with my application,i hope
you help me.
I'm developing a JSF application that allow user to upload a txt file
in a standard format.
As columns and rows number are variable ,i've developed a simple java
application that counts them.
The first row of the txt file is like

string string string......................string

The second row is like

string double double.................double

the third row is like

string double double....................double

etc...that is the first row has a format and the others another one.


My purpouse is to store this file,that contains the results of some
experiments, in a blob field of a mysql database.
Then i have to store this txt file in a byte array to put it in the
blob.
The problem is i have to execute some queries on this database in a
second moment,so i have to rebuild original format of the file to
execute the query.
How can i do it?
I've begun developing a java class that receive in input the uploaded
file and counts row and columns number.
I know that storing row with different data types needs an object use,
so i have a lot of solutions.
I've selected Map solution that i post below, but my problem is that my
soluction works only from the second line, because the first has a
different format.
My questions are:

1) How can i integrate code for first row?
I've used an array of double,but my first row contains only string.
I need to create another object?How?
2) How can i convert my new HashMap into an array of byte with a known
format?

I've used Map solution because the first column will be the primary key
of the database.
My code is

public String processMyFile() throws IOException {
BufferedReader br = new BufferedReader(new
InputStreamReader(myFile.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
line = line.replace (',', '.');
row++;
// Create a tokenizer for the line.
StringTokenizer st = new StringTokenizer(line);
// Assuming that the first column of every row is a String
and
//the remaining columns are numbers, count the number of
numeric columns.

numberOfNumericColumns = (st.countTokens()-1);
col=(numberOfNumericColumns+1);

// Get the first token from the line. It will be a String
and
//its value will be a unique key for the rest of the row.
this.dataMap = new HashMap<String, double[]>();
// Store each line of the input file as a separate key and
entry in the Map.
String key = st.nextToken().trim();
// Create the array for the numbers which make up the rest
of the line.
double[] array = new double[numberOfNumericColumns];
// Populate the array by parsing the rest of the line.
for (int column = 0; column < numberOfNumericColumns;
column++){
array[column] =
Double.parseDouble(st.nextToken().trim());
}
// Store the first column as the key and the array as the
entry.

this.dataMap.put(key, array); //Associates the specified
value with
//the specified key in this map.

}

return "success";
}
 
O

Oliver Wong

gbattine said:
Hi guys,
i'm a new JSF user and i have a big problem with my application,i hope
you help me.
I'm developing a JSF application that allow user to upload a txt file
in a standard format.
As columns and rows number are variable ,i've developed a simple java
application that counts them.
The first row of the txt file is like

string string string......................string

The second row is like

string double double.................double

the third row is like

string double double....................double

etc...that is the first row has a format and the others another one.

It took me a while to figure out that when you write
"......................", you don't mean the text file actually contains a
bunch of periods, but rather that it contains arbitrarily more of the stuff
just left to the set of periods.

So in other words, your data is a grid of doubles, with each column and
row having a name, and a special name in the top left corner.
My purpouse is to store this file,that contains the results of some
experiments, in a blob field of a mysql database.
Then i have to store this txt file in a byte array to put it in the
blob.

Alternatively, you could store the file as a stream of characters in a
LONGTEXT field in the database. Or you could actually break down the data,
and store each individual double from your text file as an entry in with a
key based on the (experiment ID, column label, row label) triplet.
The problem is i have to execute some queries on this database in a
second moment,so i have to rebuild original format of the file to
execute the query.

I have no idea what the above paragraph means.
How can i do it?

Presumably you've got the contents of the file in memory somewhere,
right? If you want to go to BLOB/LONGTEXT route, just hand that over to SQL
and let it worry about the details.
I've begun developing a java class that receive in input the uploaded
file and counts row and columns number.
I know that storing row with different data types needs an object use,
so i have a lot of solutions.
I've selected Map solution that i post below, but my problem is that my
soluction works only from the second line, because the first has a
different format.

Okay, why are you talking about Maps all of a sudden? Up until now,
you've only been talking about files and databases.
My questions are:

1) How can i integrate code for first row?
I've used an array of double,but my first row contains only string.
I need to create another object?How?

I think your first row is "special" in that it's not actually a row of
data, but it's labels for the data.
2) How can i convert my new HashMap into an array of byte with a known
format?

Iterate through every key-value pair, and recusrively convert the key
and the value to an array of byte with a known format.
I've used Map solution because the first column will be the primary key
of the database.

Either I've completely misunderstood your problem, or this is a bad
idea. The first column of your tabular data from the text file probably
won't be unique.
My code is
[most of the code snipped]
// Assuming that the first column of every row is a String
and
//the remaining columns are numbers, count the number of
numeric columns.

Don't use // style comments, as line wrapping will cause confusion as to
where the comments end. Use /**/ style comments instead.

Also, you always return the literal constant "success". There's a few
problems with this. First of all, it's useless, because there's no code
branch which returns "failure", so you the calling client can just assume
that your code will succeed without actually checking the return value.
Second of all, reporting success/failure as a return code is more of a
C-style programming than Java style. In Java, you'd "report" success by
simply not returning anything (i.e. declare your method void). Report a
failure by throwing an exception.

Other than that, there aren't any real major problems other than not
handling the first column specially.

- Oliver
 
G

gbattine

Thanks very much for your availability and excuse me for my english and
my little experience with JSF,java and mysql.
I try to be more clear because the problem is slightly changed
This is a piece of my file


ProbesetID MAO.CEL MAO1.CEL MAO2.CEL MAM.CEL....
1007_s_at 2527,515572
2745,427613 2721,744091
1053_at 39,40240421
39,61677933 40,49511453
....... .

The .... mean the number of rows and number of columns are variable,but
i know them from my java application
About my txt files my first row must be ever present and change from
file to file,but it has only string value, it's a heading.
The other rows have the same format that is string double double double
etc....
I have to store this file as it is.
I 've thinked i can store the heading into an object, a generic row(not
the first) into another one.
Later my file will have an objetc(heading) and an Arraylist(for the
other rows).
Can you help me with this code?
How can i set initial size of arraylist?(Suppose my file can have a
number of rows from 10000 to 50000).

Once this is done i have to store this complex object(the object and
the arraylist) into an array of bytes,using convention to insert a "
"(white space) that separe the columns and a special character(ex.§)
to separe different rows.
Using these convention and knowing the number of columns i can create
an inverse procedure that read the array of bytes and show exactly in
my web application the original file as i 'ive uploaded it.

Can you help me with some code and ideas?

Thanks very much
 
O

Oliver Wong

gbattine said:
I try to be more clear because the problem is slightly changed
This is a piece of my file


ProbesetID MAO.CEL MAO1.CEL MAO2.CEL MAM.CEL....
1007_s_at 2527,515572
2745,427613 2721,744091
1053_at 39,40240421
39,61677933 40,49511453
...... .

The .... mean the number of rows and number of columns are variable,but
i know them from my java application

Does every row within a given file have the same number of columns? I'm
going to assume so for the rest of this post.
About my txt files my first row must be ever present and change from
file to file,but it has only string value, it's a heading.
The other rows have the same format that is string double double double
etc....
I have to store this file as it is.
I 've thinked i can store the heading into an object, a generic row(not
the first) into another one.
Later my file will have an objetc(heading) and an Arraylist(for the
other rows).

If you have to store the file "as is" (presumably in a DB), then just
store the file. Don't do any parsing, don't create arraylists, or anything
like that. Otherwise, you'll lose whitespace information, among other
things.
Can you help me with this code?
How can i set initial size of arraylist?(Suppose my file can have a
number of rows from 10000 to 50000).

Like I said, don't do any parsing of any kind. Just dump the entire
string a a field in the DB.

If you really want to stick with ArrayList, don't set an initial size at
all. Or set the initial size to 10000. Or 50000. It really doesn't matter.
What you set it to, because the ArrayList will grow as needed.
Once this is done i have to store this complex object(the object and
the arraylist) into an array of bytes,using convention to insert a "
"(white space) that separe the columns and a special character(ex.§)
to separe different rows.

Now it sounds like you must NOT store the file "as is", but rather do
some sort of complicated conversion. Which is it? If you need to store it as
bytes, instead of as characters, you'll need to choose na encoding system.
Using these convention and knowing the number of columns i can create
an inverse procedure that read the array of bytes and show exactly in
my web application the original file as i 'ive uploaded it.

If you need to show the data exactly as it was formatted, you should not
do the above mentioned "compiclated conversion". Get your requirements
clarified from your boss, client, teacher, or whoever it is asking you to do
this stuff.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top