Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法添加包含货币的字符串_Java_Android - Fatal编程技术网

Java 无法添加包含货币的字符串

Java 无法添加包含货币的字符串,java,android,Java,Android,我的代码中有两个错误,我不知道如何解决它 请给他们看看,告诉我怎么办 以下是我的应用程序的代码: public class Food { String price = null; String name = null; boolean selected = false; public Food(String price, String name, boolean selected) { super(); this.price = price; this.name =

我的代码中有两个错误,我不知道如何解决它

请给他们看看,告诉我怎么办

以下是我的应用程序的代码:

public class Food {

String price = null;
String name = null;
boolean selected = false;

public Food(String price, String name, boolean selected) {
    super();
    this.price = price;
    this.name = name;
    this.selected = selected;
}

public String getPrice() {
    return price;
}
public void setPrice(String price) {
    this.price = price;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public boolean isSelected() {
    return selected;
}
public void setSelected(boolean selected) {
    this.selected = selected;
}
}

我试图尽可能地解决这些问题,但代码的不同位置会出现其他错误,因此我需要最佳解决方案。

totalPrice为int,而in Food model类的price为String。所以你需要换衣服

for (Food f : foodList) {
            if (f.isSelected) {
                totalPrice += Integer.parseInt(f.getPrice());
            }

            //totalPrice variable now has the total of all selected items
        }

和final Food=foodList.getposition;需要定义final

对于第一个错误,需要将食物变量设置为final:

// Make this final.
final Food food = foodList.get(position);
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
          food.setSelected(b);
        }
 });
对于第二个错误,需要将字符串值转换为整数:

for (Food f : foodList) {
  if (f.isSelected) {
    totalPrice += Integer.valueOf(f.getPrice());
  }
}
但最好将价格变量设置为整数或食品类中的价格或

更新


永远不要把你的货币保存在你的价格中。相反,您可以添加一个新变量,如货币名称。不要为了可移植性和未来无bug的代码而保存它。

第二个问题是钱作为字符串。你不能简单地加上1个SAR和2个SAR,然后期望得到3个SAR

如果这是一种单一货币系统,请不要在任何地方追加SAR,也不要使用字符串存储值(整数或长整数),并存储货币的最低分割,例如halalas

新食品15 SAR,鸡肉粉,假;成为新食品1500,鸡肉粉,假

然后向用户显示价格:

String forDisplay = String.format("%.2f SAR", totalHalas/100);
或者,特别是如果这是一个多货币系统,请为您的货币创建一个类:

就我个人而言,我会为此使用一个库:

但您也可以自己滚动:

public class Money {
   private final String currency;
   private final long value;
   private final int dp;

   public Money(long value, int dp, String currency){
       this.value = value;
       this.dp = dp;
       this.currency = currency;
   }

   //optional helper factory method
   public static Money sar(long halas){
       return new Money(halas, 2, "SAR");
   }

   public Money add(Money other){
       if(!other.currency.equals(currency))
           throw new RuntimeException("Can't add different currencies");
       if(!other.dp.equals(dp))
           throw new RuntimeException("The decimal places do not match");
       return new Money(value + other.value, dp, currency);
   }

   public String toString(){
       return String.format("%."+dp+"f %s", value/Math.pow(10,dp), currency);
   }
}
用法示例:

System.out.println(new Money(1500,2,"SAR").add(new Money(623,2,"SAR")));
//or:
System.out.println(Money.sar(1500).add(Money.sar(623)));
:


在第一个图像中,将变量设为final,在第二个图像中,使用Integer.parseIntyourstring;将闪烁的文本光标置于错误上方,然后按ALT+ENTER->ENTER键这是多币种系统吗?如果没有,则无需使用字符串表示价格,并在每个字符串中重复货币。同意@weston,我认为您希望将价格和getPrice的类型更改为int。这将解决问题2。您看到价格中的内容了吗?您看到当前价格中的内容了吗?令人惊讶的是,错误消失了:。。。但我仍然无法在这里解析foodList和isSelected的符号:for Food f:foodList{如果f.isSelected{你运行了吗?@weston:谢谢你告诉我。我不会更改答案。只需添加一个关于真实世界用法的描述。@Sahora:作为我更新的答案,你需要将价格和货币分开。我的答案会起作用,但价格值会被削减。
System.out.println(new Money(1500,2,"SAR").add(new Money(623,2,"SAR")));
//or:
System.out.println(Money.sar(1500).add(Money.sar(623)));
21.23 SAR