I’ve a text (file.txt) file . This file is loaded inside a scanner.
In the files there are various lines and in each line there are two doubles and one integer values divided by spaces.
Like this:
1.0 1.2 2
0.9 1.0 10
50.4 9.2 20
I need to fulfill three arrays.
Each array corresponds to a column. Something like:
double[] x = {1.0,0.9,50.4}
double[] y = {1.2,1.0,9.2}
double[] z = {2,10,20}
I tried to to this using this code:
double x[]= new double [3];
double y[]= new double [3];
double z[]= new double [3];
File f = new File(ClassLoader.getSystemResource(file.txt).toURI());
Scanner sc = new Scanner(f);
String aux;
for (int i=0;i<3;i++){
aux = sc.next();
x[i] = Double.parseDouble(aux);
aux = sc.next();
y[i] = Double.parseDouble(aux);
aux = sc.next();
z[i] = Double.parseDouble(aux);
}
but when the first Double.ParseDouble() I get “NumberFormatException”.
I did the debug and the variable aux has the value of a double (like: 1.0) so Double.parseDouble(aux) should works.
What it’s strange is that it says (when the exception is launched) that “aux” is something like ‘1.0 (with the ‘ before)
Is there any way to DELETE every symbol: like ‘ or , or . or “bom”(like “special spaces”) and so on? I mean.. without the need to specify the full list of possible symbols,because I don’t know the list of “invisible” symbols of a file (I mean: the symbols I can’t see when editing the file).
Thank you for any hint.
Are you sure your file input is exactly like you are stating?
I copied/pasted your exact file input into a file called “numbers.txt” and ran the class below. Seems to work exactly as intended:
If your input is not exactly like you’ve stated (with commas, quotes, etc.) but basically numbers separated by these marks you could make your delimiter (the stuff between meaningful tokens) anything but the stuff you are looking for: