How would I get a long data type in Java? For example, I would get a string like this:
我如何在Java中获得长数据类型?例如,我会得到一个这样的字符串:
String thisString = thisString.getText();
But I try the same with a long data type and it doesn't work. I need it to send it to an ArrayList, and this is the whole code:
但我尝试使用长数据类型,但它不起作用。我需要它将它发送到ArrayList,这是整个代码:
package dao;
import entities.*;
import java.util.*;
import javax.swing.JTextField;
import java.text.*;
import javaapplication1.*;
public class ProductModel{
public List<Product> findAll(){
try {
List<Product> forms = new ArrayList<Product>();
JTextField textDate;
JTextField textFecha;
JTextField textTotal;
return forms;
} catch (Exception e){
return null;
}
}
}
That's my array, and then I have this other file here:
这是我的数组,然后我在这里有另一个文件:
public class Product{
public String date;
public String fac;
public Long total;
}
And then finally, the button that sends the data that to the ArrayList:
最后,将数据发送到ArrayList的按钮:
@Override
public void actionPerformed(ActionEvent e) {
Product form = new Product();
form.date = textDate.getText();
form.fac = textFac.getText();
form.total = textTotal.getText();
forms.add(form);
}
});
But it doesn't seem to work.
但它似乎没有用。
1 个解决方案
#1
2
A String
is not a Long
. That is the reason compiler is shouting on you. You need to parse it.
字符串不长。这就是编译器对你大喊大叫的原因。你需要解析它。
form.total = Long.parseLong(textTotal.getText());
Make sure you have a null check and assign it to 0 if the string is null.
确保您具有空检查,并在字符串为空时将其指定为0。
And once you fix the issue, please give a read to Encapsulation.
一旦你解决了这个问题,请阅读Encapsulation。
#1
2
A String
is not a Long
. That is the reason compiler is shouting on you. You need to parse it.
字符串不长。这就是编译器对你大喊大叫的原因。你需要解析它。
form.total = Long.parseLong(textTotal.getText());
Make sure you have a null check and assign it to 0 if the string is null.
确保您具有空检查,并在字符串为空时将其指定为0。
And once you fix the issue, please give a read to Encapsulation.
一旦你解决了这个问题,请阅读Encapsulation。