是否有一种简单的方法可以将列表类型从String更改为Float并将其也应用于其他列表?无需使代码复杂。我曾经使用过Python,因此android和java语法使我搞砸了很多事情
我得到了一个csv文件,其中第一列包含字符串值,其余列包含浮点值。我想在调用list.get(value_id)
时从列表获取Float数据以下代码:
package com.example.nick.calcy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Mix extends AppCompatActivity {
InputStream inputStream;
List<String> komponenty,em,bi,ws,ca,fs,na,L,M = new ArrayList<>();
String[] ids;
EditText et1,et2,et3,et4,et5,et6,et7;
Float sum;
Button buttonSum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mix);
et1 = findViewById(R.id.editText11);
et2 = findViewById(R.id.editText12);
et3 = findViewById(R.id.editText13);
et4 = findViewById(R.id.editText14);
et5 = findViewById(R.id.editText15);
et6 = findViewById(R.id.editText16);
et7 = findViewById(R.id.editText17);
buttonSum = findViewById(R.id.button11);
/**Loading the input file*/
inputStream = getResources().openRawResource(R.raw.brojler);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
ids=csvLine.split(",");
try{
komponenty.add(ids[0]);
em.add(ids[1]);
bi.add(ids[2]);
ws.add(ids[3]);
ca.add(ids[4]);
fs.add(ids[5]);
na.add(ids[6]);
L.add(ids[7]);
M.add(ids[8]);
}catch (Exception e){
Log.e("Unknown error",e.toString());
}
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
/** button activity*/
buttonSum.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(et1.getText().toString().trim().length() == 0 ||
et2.getText().toString().trim().length() == 0 ||
et3.getText().toString().trim().length() == 0 ||
et4.getText().toString().trim().length() == 0 ||
et5.getText().toString().trim().length() == 0 ||
et6.getText().toString().trim().length() == 0 ||
et7.getText().toString().trim().length() == 0) {
CharSequence text = "Blank fields detected";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(getApplicationContext(),Summary.class);
Float x1 = Float.parseFloat(et1.getText().toString());
Float x2 = Float.parseFloat(et2.getText().toString());
Float x3 = Float.parseFloat(et3.getText().toString());
Float x4 = Float.parseFloat(et4.getText().toString());
Float x5 = Float.parseFloat(et5.getText().toString());
Float x6 = Float.parseFloat(et6.getText().toString());
Float x7 = Float.parseFloat(et7.getText().toString());
sum = x1+x2+x3+x4+x5+x6+x7;
i.putExtra("x1", x1);
i.putExtra("x2", x2);
i.putExtra("x3", x3);
i.putExtra("x4", x4);
i.putExtra("x5", x5);
i.putExtra("x6", x6);
i.putExtra("x7", x7);
if (sum == 100){
startActivity(i);
} else{
CharSequence text = "Sum must equal 100%";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
}}
});
}
}
我有2个主意
更改我读取csv文件的方式 我试图那样做
Float ids2;
ids2 = Float.parseFloat(csvLine.split(","));
然后将其添加到准备好的列表中,但是我弄乱了数据类型
第二个想法写函数,将简单地根据其ID从列表中转换单个值的数据类型
private Float getFloat(List list, int id)
{
Object value;
String step2;
Float changed;
value = list.get(id);
step2 = value.toString();
changed = Float.parseFloat(step2);
return changed;
}
可悲的是,它在使用时崩溃了我的应用程序
09-03 17:42:39.166 18888-18888/com.example.nick.calcy E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
@编辑 其实我发现了一个问题,我没有正确填写列表,试图解决该问题
@编辑2 调试器显示,我无法从原始文件夹中获取数据。
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
I/chatty: uid=10083(u0_a83) com.example.nick.calcy expire 8 lines
E/Unknown error: java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
立即使用该代码
package com.example.nick.calcy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Mix extends AppCompatActivity {
InputStream inputStream;
List<String> komponenty = new ArrayList<String>();
List<String> em = new ArrayList<String>();
String[] ids;
EditText et1,et2,et3,et4,et5,et6,et7;
Float sum,x11;
Button buttonSum;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mix);
et1 = findViewById(R.id.editText11);
et2 = findViewById(R.id.editText12);
et3 = findViewById(R.id.editText13);
et4 = findViewById(R.id.editText14);
et5 = findViewById(R.id.editText15);
et6 = findViewById(R.id.editText16);
et7 = findViewById(R.id.editText17);
buttonSum = findViewById(R.id.button11);
inputStream = getResources().openRawResource(R.raw.brojler);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null)
{
ids=csvLine.split(",");
try{
komponenty.add(ids[0]);
em.add(ids[1]);
}
catch (Exception e){
Log.e("Unknown error",e.toString());
}
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
/** button activity*/
buttonSum.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if(et1.getText().toString().trim().length() == 0 ||
et2.getText().toString().trim().length() == 0 ||
et3.getText().toString().trim().length() == 0 ||
et4.getText().toString().trim().length() == 0 ||
et5.getText().toString().trim().length() == 0 ||
et6.getText().toString().trim().length() == 0 ||
et7.getText().toString().trim().length() == 0) {
CharSequence text = "Blank fields detected";
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
else{
Intent i = new Intent(getApplicationContext(),Summary.class);
Float x1 = Float.parseFloat(et1.getText().toString());
Float x2 = Float.parseFloat(et2.getText().toString());
Float x3 = Float.parseFloat(et3.getText().toString());
Float x4 = Float.parseFloat(et4.getText().toString());
Float x5 = Float.parseFloat(et5.getText().toString());
Float x6 = Float.parseFloat(et6.getText().toString());
Float x7 = Float.parseFloat(et7.getText().toString());
sum = x1+x2+x3+x4+x5+x6+x7;
x11 = 100*getFloat(em,0);
i.putExtra("x1", x1);
i.putExtra("x2", x2);
i.putExtra("x3", x3);
i.putExtra("x4", x4);
i.putExtra("x5", x5);
i.putExtra("x6", x6);
i.putExtra("x7", x7);
if (sum == 100){
startActivity(i);
} else{
CharSequence text1 = "Sum must equal 100%";
Toast.makeText(getApplicationContext(), text1, Toast.LENGTH_LONG).show();
}
}}
});
}
private Float getFloat(List<String> list, int id)
{
String value = list.get(id);
return Float.parseFloat(value);
}
}
答案 0 :(得分:0)
通常有更优雅的解决方案,但是作为快速解决方案,您可以将功能更改为:
private Float getSum(List<String> list)
{
Float sum = 0f;
if (!list.isEmpty() && list.size() > 1) {
for (int i = 1; i < list.size(); i++) {
sum+= Float.parseFloat(list.get(i));
}
}
return sum;
}
答案 1 :(得分:0)
尝试一下:
遍历List<String>
并将每个项目转换为Float,然后将其添加到List<Float>
。
我写了这个函数,它使用List<String>
并返回一个List<Float>
。
我希望它对您有用。
List<Float> getList(List<String> stringList)
{
List<Float> floatList = new ArrayList<>();
for (int i = 0; i < stringList.size(); i++)
{
Float number = Float.valueOf(stringList.get(i));
floatList.add(number);
}
return floatList;
}
然后,像这样调用函数:
List<Float> floats = getList(strings);
答案 2 :(得分:0)
这就是您所需要的:
private List<Float> getList(List<String> stringList)
{
List<Float> floatList = new ArrayList<>();
for (int i = 0; i < stringList.size(); i++)
{
Float number = Float.valueOf(stringList.get(i));
floatList.add(number);
}
return floatList;
}