简体   繁体   English

Java用字母和数字解析Sting的整数

[英]Java parse a Sting with letters and numbers for an Integer

I'm working with data that is a String followed by spaces and then a numeric value. 我正在使用的数据是字符串,后跟空格,然后是数字值。

ncols         10812
nrows         10812
xllcorner     -107.0005555556
yllcorner     36.99944444444
cellsize      9.2592592593e-05

I'm trying to just read in just the numeric value. 我正在尝试仅读取数字值。 I know that from going to String to Integer or Double I can use the standard type conversions. 我知道从String到Integer或Double我可以使用标准的类型转换。

Integer.valueOf(stringOfInteger);
Double.valueOf(stringOfDouble);

In order to get just the numeric value I tried this as a test: 为了只获取数值,我尝试了一下作为测试:

BufferedReader br = new BufferedReader(new FileReader(path));
String line = br.readLine();
line.replace("[a-z]","");
line.replace(" ","");
System.out.println(line);

and it output ncols 10812 并输出ncols 10812

I'm also worried about reading the cellsize value as it has an exponential. 我还担心读取像元大小的值,因为它具有指数级。

You can do this for each line: 您可以为每一行执行此操作:

...
String[] fields = line.split("\\s+");
String name = fields[0];
float value = Float.parseFloat(fields[1]);
...

This code will split each line in fields using the spaces as a separator. 此代码将使用空格作为分隔符将字段中的每一行拆分。 The first field is a String so you can use it directly (or ignore it). 第一个字段是String因此您可以直接使用(或忽略它)。 The second one is a Float value so you have to convert it before using it. 第二个是Float值,因此在使用前必须对其进行转换。 You can use Double if you prefer. 如果愿意,可以使用Double

Try this one 试试这个

    BufferedReader br = new BufferedReader(new FileReader(path));
    String line = null;
    while ((line = br.readLine()) != null) {
        // split each line based on spaces
        String[] words = line.split("\\s+");
        //first word is name
        String name = words[0];
        // next word is actual number
        Double value = Double.valueOf(words[1]);
        System.out.println(name + ":" + value);
    }
    // don't forget to close the stream
    br.close();

Output: 输出:

    ncols:10812.0
    nrows:10812.0
    xllcorner:-107.0005555556
    yllcorner:36.99944444444
    cellsize:9.2592592593E-5

If all you want all the numeric values do a split on the space and the second item will contain your numeric value. 如果只需要所有数值,请在空格上进行拆分,第二项将包含您的数值。 Then you can do any conversions as needed and not have to worry about removing any exponents. 然后,您可以根据需要进行任何转换,而不必担心删除任何指数。

String[] data = new line.split(" ");
//remove all the spaces from the second array for your data
data[1] = data[1].replaceAll("\\s", "");
//parse to whatever you need data[1] to be

You could use the split function in Java as follows: 您可以按以下方式在Java使用split函数:

String [] dataArr = data.split("[ \t]+"); //assumes @data is you data string variable name

The dataArr , then, will look like this: 然后, dataArr将如下所示:

dataArr[0] =  "ncols"
dataArr[1] =  "10812"
dataArr[2] =  "nrows"
dataArr[3] =  "10812"
.
.
.

dataArr[n - 1] = "9.2592592593e-05"   // @n is the size of the array

You could, then, use the Integer.parseInt(String str) to parse your numerical data into integers. 然后,您可以使用Integer.parseInt(String str)将数字数据解析为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM