简体   繁体   English

将字符串传递给浮点数组?

[英]Passing string to float array?

Hi i have a string of numbers and want to put them into a float array so i can access the values.嗨,我有一串数字,想将它们放入浮点数组中,以便我可以访问这些值。

// this bit works fine, data is split into string array.
String fdata[] = data.split(",");
Float array_f[] = new Float(fdata.length);

// this is the bit which throws an error and causes my app to crash.
for (int i = 0; i < fdata.length; i++){
    float y = Float.parseFloat(fdata[i]);
    array_f[i] = y;
}

The error is a错误是一个

java nullpointerexception: attempt to invoke virtual method java.lang.string java.lang.split(java.lang.string) on a null object reference. java nullpointerexception:尝试在空对象引用上调用虚拟方法 java.lang.string java.lang.split(java.lang.string)。

thanks谢谢

adding to this i think i need to explain a bit more, so i provided more code to show what im trying to do.除此之外,我想我需要多解释一下,所以我提供了更多的代码来展示我想要做什么。

String data; //does this need to define the number of bytes needed then?

start_Acq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    channel_a = String.valueOf(channel_Acq.getSelectedItem());
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                connection.start_acquisition(channel_a);
                data = connection.start_acquisition(channel_a);
                connection.stop_acquisition();
            }catch (Exception e ){
                e.printStackTrace();
            }
        }
    });thread.start();

    data_recived.setText(data);
    String fdata[] = data.split(",");
    Float array_f[] = new Float[fdata.length];

    for(int i = 0; i<fdata.length; i++){
        float y = Float.parseFloat(fdata[i]);
        array_f[i] = y;
    }

}
});

the problem with your code is the initialization, all you have to do is to use the primitive float instead of Float :您的代码的问题在于初始化,您所要做的就是使用原始 float 而不是 Float :

 String data = "123,5468,1,25,36";
    String fdata[] = data.split(",");
    float array_f[] = new float[fdata.length];
    for (int i = 0; i < fdata.length; i++){
        array_f[i] = Float.parseFloat(fdata[i]);
System.out.println(array_f[i]);
}

First of all the data is null;首先data为空; fix it and try by this修复它并尝试这个

float y = java.lang.Float.parseFloat(fdata[i]);

Your array initialization is wrong, it should be like你的数组初始化错误,应该是这样

Float array_f[] = new Float[fdata.length];

after length you should have a semicolon not ','在长度之后你应该有一个分号而不是','

for (int i=0; i<fdata.length;i++){

EDIT Try this it will work编辑试试这个它会工作

String fdata[] = new String[]{"6.5", "8.5"};           //this bit works fine, data is split into string array.
        Float array_f[] = new Float[fdata.length];
        for (int i = 0; i < fdata.length; i++) {          //this is the bit which throws an error and causes my app to crash.
            float y = Float.parseFloat(fdata[i]);
            array_f[i] = y;
        }
        System.out.println(Arrays.asList(array_f));

use this get string array to string then convert float使用这个获取字符串数组到字符串然后转换浮点数

 for(int i=0;stringarray.length>i;i++){float_var = Float.valueOf(string_array.get(i)); float_array.add(float_var);}

then add float array然后添加浮点数组

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

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