简体   繁体   English

在Java中解析String中的double值

[英]Parsing double value from String in Java

I am supposed to extract the double values from the following string: 我应该从以下字符串中提取双精度值:

r ( 4.5, 1.0 ) 10.5 7 ;  

However my code deletes everything but the digits and that's not what I want. 但是我的代码删除了除数字之外的所有内容,这不是我想要的。 My code: 我的代码:

import java.io.File;
import java.util.Scanner;


public class mine {

    public static void main(String[] args) throws Exception
    {

      String str;
      String numbers;

      Scanner SC=new Scanner(System.in);

      System.out.print("Enter string that contains numbers: ");
      str=SC.nextLine();

      //extracting string
      numbers=str.replaceAll("[^0-9]", "");

      System.out.println("Numbers are: " + numbers);
    }
}

Output of my code: 我的代码输出:

Enter string that contains numbers: r ( 4.5, 1.0 ) 10.5 7 
Numbers are: 45101057

I want to find a way to assign the numbers to variable. 我想找到一种将数字分配给变量的方法。 So for example using the following string: r ( 4.5, 1.0 ) 10.5 7 (Assuming a,b,c,d are already declared) I want this: 因此,例如使用以下字符串: r ( 4.5, 1.0 ) 10.5 7 (假设已经声明了a,b,c,d),我想要这样做:

a = 4.5
b = 1.0
c = 10.5 
d = 7

If you would like to keep floating-point numbers and separators, you must include dot character and space, in addition to decimal digits, into the replacement regex: 如果要保留浮点数和分隔符,则在替换正则表达式中,除十进制数字外,还必须包含点字符和空格。

numbers = str.replaceAll("[^0-9. ]", "");

Demo. 演示。

Once you have the string, separating out the individual numbers can be done using the technique discussed in this Q&A: Convert a string of numbers into an array . 有了字符串后,可以使用本问答中讨论的技术来分离单个数字: 将数字字符串转换为数组

Having variables in that fashion is only going to be useful if you're guaranteed that you have four values in your file. 只有在确保文件中有四个值的情况下,以这种方式具有变量才有用。 For flexibility's sake, it's better to place this in a list instead. 为了灵活性起见,最好将其放在列表中。

List<Double> numbers = new ArrayList<>();
// assuming str already has the numbers...
for(String s : str.split(" ") {
    numbers.add(Double.parseDouble(s));
}

Declaring these as individual variables uses 99% of the same approach above, just swapping out a list for the individual values. 将它们声明为单个变量将使用上面相同方法的99%,只是将单个值换成一个列表。 I leave that as an exercise for the reader. 我把它留给读者作为练习。

Try this (it uses a second scanner to try to parse each token as a Double; if it's not a double, the parsing will fail): 尝试以下操作(它使用第二个扫描器尝试将每个令牌解析为Double;如果不是double,则解析将失败):

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter string that contains numbers: ");
    String str = sc.nextLine();

    Scanner token = new Scanner(str);
    while token.hasNext() {
         String tokenStr = token.next();
         try {
             double d = Double.parseDouble(tokenStr);
             // YOUR CODE: do something with d here, such as appending
             // it to an array, assigning it to a var, or printing it out.

         } catch (NumberFormatException e) {
             // no op
         }
    }
}

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

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