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);
for (int i=0;j<3;i++){
x[i] = sc.nextDouble();
y[i] = sc.nextDouble();
z[i] = sc.nextDouble();
}
but when the first nextDouble() is executed an “uncaught” (java.util.InputMismatchException) exception.
What am I doing wrong?
Thank you for any hint.
If
sc.next()works for you, you need to parse your output as it is of the typeStringand hence aInputMismatchException.try: