G
gbattine
Hi, thanks to your helps i have developed a java application that
receive an input file dato2.txt with this format
string double double double...... read the file and create an object
with the key and the value.
When i compile i have no error but at runtime i have this exception
Caricamento dell'array di double in corso..... Il numero di colonne del
file Þ12 Exception in thread "main" java.lang.NumberFormatException:
For input string: "2 527,515572" at
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at
java.lang.Double.parseDouble(Unknown Source) at
AddDb2.populateMap(AddDb2.java:73) at AddDb2.<init>(AddDb2.java:21) at
AddDb2.main(AddDb2.java:12)
Here is the code:
//package gbattine;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class AddDb2 {
private static String fileName = "Dato2.txt";
private Map <String, double[]>dataMap = null;
public static void main(String[] args) throws IOException {
new AddDb2();
}
public AddDb2() throws IOException {
/* Get a reference to the input file. */
FileReader file = new FileReader(fileName);
BufferedReader br = new BufferedReader(file);
/* Create the empty map. */
this.dataMap = new TreeMap<String, double[]>(); //mettere hash
/* Populate the map from the input file. */
populateMap(br);
/* Display the contents of the map. */
displayMap(this.dataMap);
/* Close the reader. */
br.close();
}
/**
* Populate a TreeMap from an input file.
*
* <p>This method accomodates an input file with any number of
lines in
it. It also
* accomodates any number of doubles on the input line as long as
the
first value on the
* line is a String. The number of doubles on each input line can
also
vary.</p>
*
* @param bufferedReader
* @return a Map that has one element for each line of the input
file;
the key is the first column from the input file and the entry is the
array
of doubles that follows the first column
* @throws IOException
*/
public Map populateMap(BufferedReader bufferedReader) throws
IOException
{
System.out.println("Caricamento dell'array di double in
corso.....");
/* Store each line of the input file as a separate key and
entry in
the Map. */
String line = null;
while ((line = bufferedReader.readLine()) != null) {
/* 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.
*/
int ColNumber=st.countTokens()-1;
//int numberOfNumericColumns = st.countTokens() - 1;
int numberOfNumericColumns=(ColNumber-1);
System.out.println("Il numero di colonne del file
è"+ColNumber);
/*
* 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.
*/
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);
}
return this.dataMap;
}
public void displayMap(Map<String,double[]> myMap) {
/* Iterate through the values of the map, displaying each key
and
its corresponding array. */
for (Map.Entry<String, double[]> entry : myMap.entrySet()) {
/* Get and display the key. */
System.out.print(entry.getKey() + " : "); //$NON-NLS-1$
/* Get the array. */
double[] myArray = entry.getValue();
/*
* Display each value in the array. Put a semicolon after
each
value except the last.
* Keep all the values for a given key on a single line.
*/
for (int ix = 0; ix < myArray.length; ix++) {
if (ix < myArray.length - 1) { //the value is not the
last one in the array
System.out.print(myArray[ix] + "; "); //$NON-NLS-1$
} else { //the value is the last one in the array
System.out.println(myArray[ix]);
}
}
}
}
}
Another question is:how can i return from this function the ColNumber
value to import it in another function that i'm developing? Thanks very
much
receive an input file dato2.txt with this format
string double double double...... read the file and create an object
with the key and the value.
When i compile i have no error but at runtime i have this exception
Caricamento dell'array di double in corso..... Il numero di colonne del
file Þ12 Exception in thread "main" java.lang.NumberFormatException:
For input string: "2 527,515572" at
sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at
java.lang.Double.parseDouble(Unknown Source) at
AddDb2.populateMap(AddDb2.java:73) at AddDb2.<init>(AddDb2.java:21) at
AddDb2.main(AddDb2.java:12)
Here is the code:
//package gbattine;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class AddDb2 {
private static String fileName = "Dato2.txt";
private Map <String, double[]>dataMap = null;
public static void main(String[] args) throws IOException {
new AddDb2();
}
public AddDb2() throws IOException {
/* Get a reference to the input file. */
FileReader file = new FileReader(fileName);
BufferedReader br = new BufferedReader(file);
/* Create the empty map. */
this.dataMap = new TreeMap<String, double[]>(); //mettere hash
/* Populate the map from the input file. */
populateMap(br);
/* Display the contents of the map. */
displayMap(this.dataMap);
/* Close the reader. */
br.close();
}
/**
* Populate a TreeMap from an input file.
*
* <p>This method accomodates an input file with any number of
lines in
it. It also
* accomodates any number of doubles on the input line as long as
the
first value on the
* line is a String. The number of doubles on each input line can
also
vary.</p>
*
* @param bufferedReader
* @return a Map that has one element for each line of the input
file;
the key is the first column from the input file and the entry is the
array
of doubles that follows the first column
* @throws IOException
*/
public Map populateMap(BufferedReader bufferedReader) throws
IOException
{
System.out.println("Caricamento dell'array di double in
corso.....");
/* Store each line of the input file as a separate key and
entry in
the Map. */
String line = null;
while ((line = bufferedReader.readLine()) != null) {
/* 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.
*/
int ColNumber=st.countTokens()-1;
//int numberOfNumericColumns = st.countTokens() - 1;
int numberOfNumericColumns=(ColNumber-1);
System.out.println("Il numero di colonne del file
è"+ColNumber);
/*
* 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.
*/
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);
}
return this.dataMap;
}
public void displayMap(Map<String,double[]> myMap) {
/* Iterate through the values of the map, displaying each key
and
its corresponding array. */
for (Map.Entry<String, double[]> entry : myMap.entrySet()) {
/* Get and display the key. */
System.out.print(entry.getKey() + " : "); //$NON-NLS-1$
/* Get the array. */
double[] myArray = entry.getValue();
/*
* Display each value in the array. Put a semicolon after
each
value except the last.
* Keep all the values for a given key on a single line.
*/
for (int ix = 0; ix < myArray.length; ix++) {
if (ix < myArray.length - 1) { //the value is not the
last one in the array
System.out.print(myArray[ix] + "; "); //$NON-NLS-1$
} else { //the value is the last one in the array
System.out.println(myArray[ix]);
}
}
}
}
}
Another question is:how can i return from this function the ColNumber
value to import it in another function that i'm developing? Thanks very
much