Task: Display a list of all smartphones grouped by manufacturer. Question: How to read the file so that all unique columns (Serial Number, Manufacturer, Model, Operating System, RAM Size) are read separately and recorded with the ArrayList into an array and then each line can be passed through the form of getters. Here is the form:

public class IntoBox { static void p(String s) { System.out.println(s); } public int number; public String maker; public String model; public String os; public int ram; public IntoBox (int number, String maker, String model, String os, int ram){ this.number = number; this.maker = maker; this.model = model; this.os = os; this.ram = ram; this.i = i; } //getters public int getNumber(){ return number; } public String getMaker(){ return maker; } public String getModel(){ return model; } public String getOs(){ return os; } public int getRam(){ return ram; } //setters public void setNumber(int number){ this.number=number; } public void setMaker(String maker){ this.maker=maker; } public void setModel(String model){ this.model=model; } public void setOs(String os){ this.os=os; } public void setRam(int ram){ this.ram=ram; } 

Sample data from file: 1111 Samsung A5 Android 1000
As I read the file:

 ArrayList<String> arrayList = new ArrayList<String>(); File inputFile = new File("C:\\data\\data.txt"); BufferedReader reader = new BufferedReader(new FileReader(inputFile)); while (reader.ready()) arrayList.add(reader.readLine()); reader.close(); 
  • one
    The answer is to create a file parser that reads line by line and breaks a string into the required fields and passes objects to the array. What have you tried for this? - xAqweRx
  • @xAqweRx, i.e. arrayList.add (reader.readLine (getNumber (), ..., getRam ())); ? - PoGostu
  • Add the parsing code to understand what's wrong. One line doesn't help - xAqweRx
  • @xAqweRx, understand ... I do not know what "parsing" is, I'm new to java. - PoGostu
  • Parsing in this context means - parsing the string according to the necessary parameters. Provide a sample of the information from the file + how you read the file - xAqweRx

1 answer 1

An example of what might work, but here are a bunch of holes. It is necessary to check the incoming data and so on.

 BufferedReader br = null; try { String sCurrentLine; String filePath = "filePath"; br = new BufferedReader(new FileReader( filePath )); ArrayList<IntoBox> inboxes = new ArrayList(); while ((sCurrentLine = br.readLine()) != null) { String[] params = sCurrentLine.split(" "); inboxes.add( new IntoBox ( Integer.parseInt( params[0]), params[1], params[2], params[3], Integer.parseInt( params[4])) ); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } }