Answer the question
In order to leave comments, you need to log in
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);
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question