Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > NumberFormatException:please help me......!!!

Reply
Thread Tools

NumberFormatException:please help me......!!!

 
 
gbattine
Guest
Posts: n/a
 
      05-22-2006
Hi,
i have a question and i need your help.
I'm developing a java application that have to import data from a txt
file as

3.2 2.1 4.5 6.7 2.3 4.5 3.4 5.5
2.1 3.2 4.7 2.1 3.5 6.7 5.6 3.1

The application counts rows number and columns number and import data
into a bidimensional array of double type.
I compile my code and i have this error

Exception in thread "main" java.lang.NumberFormatException: For input
string: "3
..2 2.1 4.5 6.7 2.3 4.5 3.4 5.5"
at sun.misc.FloatingDecimal.readJavaFormatString(Unkn own
Source)
at java.lang.Double.parseDouble(Unknown Source)
at AddDb.ReadArray(AddDb.java:51)
at AddDb.main(AddDb.java:97)

How can i do?
Please help me with code, i'm a new java user...
Here is my code

import java.util.*;
import java.io.*;
public class AddDb {
private static String fileName="Dato.txt";
private static int Ncolumns=0;
private static int Nrows=0;

public int ColumnsNumber(BufferedReader br)throws IOException {

String line = br.readLine(); //legge una linea di testo terminata
da /n o /r e la restituisce come stringa
StringTokenizer st = new StringTokenizer(line);
Ncolumns=st.countTokens();

return Ncolumns;
}

public int RowsNumber(BufferedReader br)throws IOException {


String line = br.readLine();
while (line != null) {
Nrows++;
line = br.readLine();
}
return Nrows;
}



public double[][]ReadArray(int Nrows,int Ncolumns,BufferedReader
br)throws IOException
{
double[][]x=new double[Nrows][Ncolumns];
for(int i=0; i<Nrows; i++)
{
StringTokenizer tok=new StringTokenizer(br.readLine(),"");/
for(int j=0; j<Ncolumns; j++)
{

x[i][j]=Double.parseDouble(tok.nextToken().trim()) }


}

for(int i=0; i<Nrows; i++)
{
for(int j=0; j<Ncolumns; j++)
{
System.out.println("L'array bidimensionale "+x[i][j]);
}
}
return x;
}




public static void main(String[] args)throws IOException {
FileReader file = new FileReader(fileName); BufferedReader br =
new BufferedReader(file);
AddDb db=new AddDb();
br.mark(99999);
db.ColumnsNumber(br);
System.out.println("Il numero di colonne e' :"+Ncolumns);
br.reset();
br.mark(99999);
db.RowsNumber(br);
System.out.println("Il numero di righe e' :"+Nrows);
br.reset();
System.out.println("Caricamento dell'array di double in
corso.....");
try
{
System.out.println(db.ReadArray(Nrows,Ncolumns,br) );
}
catch(IOException e){
System.out.println(e.getMessage());
}
br.close();

}
}

 
Reply With Quote
 
 
 
 
nkalagarla@gmail.com
Guest
Posts: n/a
 
      05-22-2006
Looks like there is a problem in the constructing StringTokenizer.

StringTokenizer tok=new StringTokenizer(br.readLine(),"");

You may have to set the delimiter to space or tab based on your input.

StringTokenizer tok=new StringTokenizer(br.readLine()," "); // this
should work.

 
Reply With Quote
 
 
 
 
gbattine
Guest
Posts: n/a
 
      05-22-2006
thanks too much...
i have another question....if my file is:

xyz 2,3 4,5 6,7...
asd 3,4 2,1 3,4...

Now i have a first column of string type and i can't use a double
array[][].
How can i do?
I have to store the first column in another array?
Can i use another bidimensional array type that allow me to store a
file composed by a string column and data columns?
Excuse for my english,i wait you help.....

 
Reply With Quote
 
nkalagarla@gmail.com
Guest
Posts: n/a
 
      05-22-2006
If you have to store one string and an array of double, you may have to
use an object. So each line could be stored in an object (say of type
Record that has string and array of double as members) and file could
be represented as array of objects. It is up to you how you want to
store in the memory. You could also use two arrays, one to store first
column and other to store their corresponding numbers.

 
Reply With Quote
 
Rhino
Guest
Posts: n/a
 
      05-22-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> If you have to store one string and an array of double, you may have to
> use an object. So each line could be stored in an object (say of type
> Record that has string and array of double as members) and file could
> be represented as array of objects. It is up to you how you want to
> store in the memory. You could also use two arrays, one to store first
> column and other to store their corresponding numbers.
>


Another option would be to use a Collection of some kind.

For example, if the String uniquely identifies the array of doubles that
follows it on a line of the input file, you could use the String as a key to
a Map and then store the array as the entry for that key. Then, you could
obtain the array associated with a given key value very easily and work with
it. See program AddDb2 below.

Another approach would be to create an Object corresponding to each row of
the input file, as suggested by "nkalagarla", then put these objects in a
Collection. See program AddDb3 and class Dato2Righe below.

If you look at AddDb2 and AddDb3, you'll see that they are both flexible
enough to handle any number of lines in the input file without first having
to count the lines: they each open the input file (Data2.txt) once, and keep
reading a line at a time until the file is exhausted. As the examine each
line from the input file, they treat the first column as a String that
contains a key which describes the rest of the line. They count the number
of remaining columns on that line and assume that the remaining columns all
contain String representations of doubles. These doubles are then stored in
an array. It doesn't matter how many doubles are on each line and it doesn't
matter if some lines have more doubles than others. That makes both programs
much more flexible than your original program - and more efficient since the
file doesn't have to be read three separate times but only once.

AddDb2 stores the line from the input file as a Map element: the first
column is the key and the array of doubles is the entry associated with that
key. The displayMap() method displays the contents of the Map.

AddDb3 stores the line from the input file as an object belonging to class
Dato2Righe. That object contains two things: a String key and an array of
doubles. Each Dato2Righe object is added to a Set. The displaySet() method
displays the contents of the Set.

I don't want to give you a big sales pitch about Collections but if you read
this "trail" (chapter) from the Java Tutorial, I think you'll begin to see
the advantages of Collections over arrays:
http://java.sun.com/docs/books/tutor...ons/index.html.

Yet another approach you could use for storing and retrieving your data is
using a database like Access, MySQL, or DB2. That adds considerable overhead
to your environment and you have to take time to learn how to use the
database but it gives you a lot more ways of storing and retrieving data
with an almost infinite capacity for data.

================================================== ===============
/*
* Created on 22-May-2006
*
*/
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"; //$NON-NLS-1$

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[]>();

/* 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....."); //$NON-NLS-1$

/* 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 numberOfNumericColumns = st.countTokens() - 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.
*/
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]);
}
}
}

}
}
================================================== ===============

================================================== ===============
/*
* Created on 22-May-2006
*
*/
package gbattine;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

public class AddDb3 {

private static String fileName = "Dato2.txt"; //$NON-NLS-1$

private Set<Dato2Righe>dataSet = null;

public static void main(String[] args) throws IOException {

new AddDb3();
}

public AddDb3() throws IOException {

/* Get a reference to the input file. */
FileReader file = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(file);

/* Create the empty set. */
this.dataSet = new HashSet<Dato2Righe>();

/* Populate the set from the input file. */
populateSet(bufferedReader);

/* Display the contents of the set. */
displaySet(this.dataSet);

/* Close the reader. */
bufferedReader.close();
}

/**
* Populate a TreeSet 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 set containing one Dato2Righe object for each line of the
input file
* @throws IOException
*/
public Set populateSet(BufferedReader bufferedReader) throws IOException
{

System.out.println("Caricamento dell'array di double in
corso....."); //$NON-NLS-1$

/* 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 numberOfNumericColumns = st.countTokens() - 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.
*/
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());
}

/* Create the object which will contain the data from this row.
*/
Dato2Righe dato2Righe = new Dato2Righe(key, array);

/* Store the object in the Set. */
try {
this.dataSet.add(dato2Righe);
} catch (RuntimeException excp) {
System.out.println("Message: " + excp.getMessage());
//$NON-NLS-1$
excp.printStackTrace();
}
}


return this.dataSet;
}


public void displaySet(Set<Dato2Righe> mySet) {

/*
* The set is a collection of Dato2Righe objects. Iterate through
the values of the set,
* using methods within the Dato2Righe class to obtain the key and
its array.
*/
for (Dato2Righe oneRow : mySet) {

/* Get and display the key. */
System.out.print(oneRow.getKey() + " : "); //$NON-NLS-1$

/* Get the array. */
double[] myArray = oneRow.getArray();

/*
* 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]);
}
}
}

}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
/*
* Created on 22-May-2006
*
*/
package gbattine;

public class Dato2Righe {

private String key = null;
private double[] array = null;

public Dato2Righe(String myKey, double[] myArray) {

this.key = myKey;
this.array = myArray;
}

public String getKey() {

return this.key;
}

public double[] getArray() {

return this.array;
}

}

================================================== ===============


--
Rhino


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments