C
C
CheshKin2015-06-30 14:33:19
Java
CheshKin, 2015-06-30 14:33:19

How to get an integer?

When summing numbers in JAVA and writing them in label = instead of the sum, it gives 3.15E7,
How to get around?

ReadFile reader4 = new ReadFile();
        reader4.read("src//test.txt");
        reader4.calculateSum();

        userName5.setText(String.valueOf("     Общая Сумма:    \n"+reader4.calculateSum()));
        grid.add(userName5, 2, 20);

SumClass.java - it counts and passes to first
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ivanzemzerov on 10.02.15.
 */
public class SumClass {

    List<User> users = new ArrayList<User>();

    public void read(String path) {
        BufferedReader readFromFile = null;
        try {
            readFromFile = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = readFromFile.readLine()) != null) {
                User newUser = new User(line);
                users.add(newUser);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (readFromFile != null)
                try {
                    readFromFile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }
    }

    public double calculateSum() {
        double sum = 0;
        for (User user : users) {
            sum += user.getKey();
        }
        return sum;
    }

    private class User {
        public double key = 0;
        public String value;


        public User(String line) {
            if (line != null && !line.isEmpty()) {
                String[] entries = line.split("\\s", 8);
                this.key = Double.valueOf(entries[6]); // можно завернуть в
                // try {} catch
                this.value = entries[5];
            } else {
                // логируем
            }
        }

        public double getKey() {
            return key;
        }

        public String getValue() {
            return value;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
protven, 2015-06-30
@CheshKin

Show the code. And so I can nawang something like
int intValue = (int) Math.round(doubleValue);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question