I have a file that I put some numbers into but then I decided I wanted to label the numbers with a name for each one, like so:
A = 1 2 3
B = 3 4 5
C = 6 7 8
This caused an exception to be thrown. I want to know how I can workaround this so the scanner ‘ignores’ all but the numbers unless I tell it otherwise.
Can anyone help?
EDIT:
Also, I already know what is causing the exception. I am using next.Double(); to pick out the numbers in the file.
public static void readFile() throws IOException, FileNotFoundException {
String fileName = "vectors.txt";
// Reference the file using the the BufferedReader object
BufferedReader input = new BufferedReader(new FileReader(fileName));
//scanner to scan through file
Scanner token = new Scanner(input);
double fileX = token.nextDouble();
double fileY = token.nextDouble();
double fileZ = token.nextDouble();
vecA = new Vector3D(fileX,fileY,fileZ);
fileX = token.nextDouble();
fileY= token.nextDouble();
fileZ = token.nextDouble();
vecB = new Vector3D(fileX, fileY, fileZ);
fileX = token.nextDouble();
fileY= token.nextDouble();
fileZ = token.nextDouble();
//initialize vecC using double values from third line of file
vecC = new Vector3D(fileX, fileY, fileZ);
//close file
input.close();
}
You cannot make the scanner ignore input automatically, but you can write code to skip input yourself until you see that a double is available:
If you need to put this code in multiple places, you can wrap it in a function: