Answer the question
In order to leave comments, you need to log in
Sum from ArryList when scanning a file?
Good afternoon!
I've been struggling with this problem for three days now. I need to get the sum of the first column. The readable file has an entry like: " 1 Petya Pupkin". THOSE. let's say there will be 300 records with different numbers, and I will need to get the sum of these numbers (assuming that I read them from a file and records will be added to the file).
Example:
A .txt file contains
1 Petya Pupkin
15 Sashka Noviy
9 Bot Add
8 Serega Petrovich
Output SUM (33) (And Names and Surnames are discarded)
Here is the actual code that reads from the file and displays the entire file to the console:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
String line;
List<User> mass = new ArrayList<User>();
public void reader(){
BufferedReader readFromFile = null;
try {
readFromFile = new BufferedReader(new FileReader("src//1.txt"));
while ((line = readFromFile.readLine())!=null){
User newUser = new User();
newUser.key = line;
mass.add(newUser);
//User newUser1 = new User();
//newUser1.chislo = Integer.parseInt(String.valueOf(newUser));
//mass.add(newUser1);
}
}catch (IOException e) {
e.printStackTrace();
}finally{
if (readFromFile !=null)
try{
readFromFile.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Main test = new Main();
test.reader();
for (User newUser : test.mass)
System.out.println(newUser.key);
//Main test1 = new Main();
//test1.reader();
//for(User newUser1 : test1.mass)
//System.out.println(newUser1.chislo);
}
private class User {
public String key;
public int chislo;
}
}
Answer the question
In order to leave comments, you need to log in
I'll expand bimeg's answer a bit and post the source code from the comments.
public class Reader {
private 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 int calculateSum() {
int sum = 0;
for (User user : users) {
sum += user.getKey();
}
return sum;
}
private class User {
public int key = 0;
public String value;
public User(String line) {
if (line != null && !line.isEmpty()) {
String[] entries = line.split("\\s", 2);
this.key = Integer.valueOf(entries[0]); // можно завернуть в
// try {} catch
this.value = entries[1];
} else {
// логируем
}
}
public int getKey() {
return key;
}
public String getValue() {
return value;
}
}
public static void main(String[] args) {
Reader reader = new Reader();
reader.read("src//1.txt");
System.out.println(reader.calculateSum());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question