简体   繁体   中英

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

How would I get a long data type in 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:

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:

    @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 . 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.

And once you fix the issue, please give a read to Encapsulation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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