【问题标题】:Best way to convert string with doubles and characters into just doubles in Java在Java中将带有双打和字符的字符串转换为双打的最佳方法
【发布时间】:2014-05-07 04:25:55
【问题描述】:

如果我已经问了一个已经回答的问题,但我不确定作为初学者的最佳方法,请原谅我。

我有一个类似下面的字符串,转换这个字符串的最佳方法是什么,以便我只有双打 3.4134388041,0.63117288 等,而我丢弃其余的?

谢谢!

Shading:Building:Detailed,
    39,                      !- Name
    ,                        !- Transmittance Schedule Name
    4,                       !- Number of Vertices
    3.4134388041,            !- Vertex 1 X-coordinate {m}
    0.63117288,              !- Vertex 1 Y-coordinate {m}
    2.2012378517,            !- Vertex 1 Z-coordinate {m}
    3.4134388041,            !- Vertex 2 X-coordinate {m}
    10.01517288,             !- Vertex 2 Y-coordinate {m}
    2.2012378517,            !- Vertex 2 Z-coordinate {m}
    2.9134388041,            !- Vertex 3 X-coordinate {m}
    10.01517288,             !- Vertex 3 Y-coordinate {m}
    2.2012378517,            !- Vertex 3 Z-coordinate {m}
    2.9134388041,            !- Vertex 4 X-coordinate {m}
    0.63117288,              !- Vertex 4 Y-coordinate {m}
    2.2012378517;            !- Vertex 4 Z-coordinate {m}

【问题讨论】:

  • 这可能取决于语言。

标签: java string double next


【解决方案1】:

以下代码应该隔离字符串中不能被 1 整除的每个数字,分别打印它们:

public static void main(String[] args)
{
    String text = ""; // string was too long to fit here for tidiness
    Matcher search = Pattern.compile("(?!=\\d\\.\\d\\.)([\\d.]+)").matcher(text);
    while (search.find())
    {
        double doub = Double.parseDouble(search.group(1));
        if (doub mod 1 != 0)
        {
            System.out.println(doub);
        }
    }
}

【讨论】:

  • 嗨,谢谢,这很好,但是,我如何将这个 doub 放入数组而不是打印它,这是我现在卡住的地方,欢呼谢谢
  • while 循环之前使用ArrayList doubArray = new ArrayList();,然后在循环内在doub 的定义下方添加doubArray.add(doub)。这将具有 ArrayList 中的值,而不是应该更好地工作的 Array
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2019-01-22
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
相关资源
最近更新 更多