Increasing versatility of parsing - Java
I'm developing a program which takes a .txt, retrieves the numbers from it, and puts them into an array for further analysis. Here's what I've got, where 'line' is the string coming in-
stringArray = line.split(" ");
doub开发者_如何学CleArray = new double[stringArray.length];
for(int i=0; i<stringArray.length; i++)
{
doubleArray[i] = Double.parseDouble(stringArray[i]);
}
This works if the string is something like "6.5 8 2 4.3 1 67". However, I'm trying to get it to filter out commas, multiple spaces, and other letters too. I think this just has to do with modifying the .split part, but I'm unsure how.
Any ideas?
For example:
stringArray = line.split("[^0-9.+Ee-]+");
will split on any set of characters that are not 0,1,2,3,4,5,6,7,8,9, '.', '+', 'E', 'e' or '-' - which are all the characters that can appear in a double.
You may go through line char by char retrieving only digits and points, then use line.spit()
on the purified string. I think this would be faster than regexp when you analyze a lot of data.
Use the Apache commons lang StringUtil class. Check out the split method.
Use a Scanner to do the work for you.
import java.util.Scanner;
Scanner s = new Scanner(myInputString);
s.useDelimiter(",");
List<Double> doubles = new ArrayList<Double>();
while( s.hasNextDouble() ) {
doubles.add(s.nextDouble())
}
You can tweak what is passed in to useDelimiter to suite your needs.
精彩评论