Task :

There is a file. The number of lines is unknown. Each line of the file is a record of the form:

Покупатель Товар Количество 

It is necessary to create an array with all customers.

For each customer, calculate the number of items he purchased for each type of product.

List in alphabetical order the names of buyers.

Input from file:

 Иванов газета 10 Петров ручка 5 Николаев тетрадь 3 Иванов ручка 2 Николаев ручка 1 Петров тетрадь 2 Николаев газета 1 

Displayed data:

 Иванов : газета - 10 шт ; ручка - 5 шт ; Николаев : газета - 1 шт ; ручка - 1 шт ; тетрадь - 3 шт; Петров : ручка - 5 шт ; тетрадь - 2 шт; 

My code is:

 package semnadcat; import java.io.IOException; import java.io.*; /** * * @author Марат */ public class Semnadcat2 { public static void main(String args[]) { try(FileReader reader = new FileReader("C:\\SomeDir\\notes3.txt")){ int c; while((c=reader.read())!=-1){ System.out.print((char)c); } } catch(IOException ex){ System.out.println(ex.getMessage()); } System.out.println(" "); } } 

My mistakes:

I do not understand how you can save the file data to an array. It can only be character-by-word pull information on the screen.

  • If you want to learn programming, read Java books (and not only Java). Without this, it is unlikely that something good will come out (and the answers to StackOverflow will not help much), and you will spend time. - m. vokhm
  • @ m.vokhm personally help me. And books give nothing without practice. - Marat Zimnurov

1 answer 1

In the most primitive case, you can do this:

Create a Customer class:

 public class Customer { private String mSurname; private String mProduct; private int mAmount; public Customer(String line) { String[] customer = line.split(" "); mSurname = customer[0]; mProduct = customer[1]; mAmount = Integer.parseInt(customer[2]); } public String getSurname() { return mSurname; } public String getProduct() { return mProduct; } public int getAmount() { return mAmount; } } 

Create an ArrayList<Customer> and fill it with data from a file:

 ArrayList<Customer> customers = new ArrayList<>(); BufferedReader bufferedReader = new BufferedReader(new FileReader("Customers.txt")); String currentLine; while ((currentLine = bufferedReader.readLine()) != null) { customers.add(new Customer(currentLine)); } bufferedReader.close(); 

And then quite simple.

UPD:

Conclusion of information about customers to the console:

  • Using the usual for loop:

     for (int i=0; i<customers.size(); i++) { Customer customer = customers.get(i); System.out.println(customer.getSurname() + " | " + customer.getProduct() + " | " + customer.getAmount()); } 
  • Using a for each : loop:

     for (Customer customer : customers) { System.out.println(customer.getSurname() + " | " + customer.getProduct() + " | " + customer.getAmount()); } 
  • Using the Stream API (Java 8):

     customers.stream() .map(x -> x.getSurname() + " | " + x.getProduct() + " | " + x.getAmount()) .forEach(System.out::println); 
  • Please tell me what to do next? I don’t understand what to do next. - Marat Zimnurov
  • I realized that in ArrayList <Customter> the file is being read and thrown into the constructor of the Customer class. How can I make a conclusion and operations on the received elements? - Marat Zimnurov
  • @MaratZimnurov, Next you need to sort the resulting list by the mSurname field and output the contents to the console. Read about the collections in one of the classic books (not websites!) On Java: Ekkel, Schildt, or Horstmann, where you can find answers to many of your questions. And because I’m writing you a ready-made code, there will be little confusion. - post_zeew
  • Thanks for the information! - Marat Zimnurov
  • It is impossible to display this field on the console. Well, I do not understand. Tell me a line to output. Does not work. - Marat Zimnurov