简体   繁体   English

如何从文本字段中获取Long值并将表单发送到ArrayList中

[英]How to get Long value from text field and send form into ArrayList

How would I get a long data type in Java? 如何在Java中获取长数据类型? For example, I would get a string like this: 例如,我将得到这样的字符串:

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. 但这似乎不起作用。

A String is not a Long . String不是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

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

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