Home > Net >  How do I map String values to a specific data structure Java
How do I map String values to a specific data structure Java

Time:06-24

I am practicing a standalone project to learn Java

I have a txt file that has many lines Eg:

00001 48592 2394
00002 48372 5932
....

I want to read the file line by line and extract certain values and put them into a structure like [values from line 1 , values from line 2 , ... ] I don't know how to construct this structure based on what I have

the file processor

public class FileProcess {
  public List<Data> process(InputStream InputStream) throws IOException {
    List<Data> result = new ArrayList<>();
    String line;

    try(// scanning inputstream) {
      line = sc.nextLine();
      while (line != null) {
        // read thru each line in the txt file

        String id = line.subString(0, 5);
        String code = line.subString(6, 11);

        Data data = new data();
        result.add(data);
      }
      return result;
    }
  }

the data model

  public class Data {
    private String id;
    private String code;
  }

Main

  public static void main(String[] args) {
    ProcessorApplication processorApplication = new ProcessorApplication();

    try (InputStream inputStream = new FileInputStream(processorApplication.inputPath)) {
      FileProcess fileProcess = fileProcess.process(inputStream);
      List<Data> dataList = fileProcess.process(input);
    } catch(IOException e) {
      System.out.printIn("error");
    }
  }

The result output is currently:

[model@xxxx, model@xxxx, model@xxx, ....]

But I am expecting:

   line 1     line2
  id   code   id   code
[00001 48592, 00002 48372, ....]

I don't know how to map the values to be able to get a data structure like this.. Please help, thank you very much

CodePudding user response:

First you would need to add a new constructor in Data class (Required because of the private modifier. Providing setter methods could be an alternative).

public Data(String id, String code) {
    this.id = id;
    this.code = code;
}

Then, change your Main method to something like this:

  while (line != null) {
    // read thru each line in the txt file

    String id = line.subString(0, 5);
    String code = line.subString(6, 11);

    result.add(new Data(id, code)); // This!
  }

And if you need to print the data, you would also need to override the toString method in the Data class, for instance:

public class Data {
private String id;
private String code;
  
public Data(String id, String code) {
    this.id = id;
    this.code = code;
    }

@Override
public String toString() {
    return this.id   " "   this.code;
    }
}

Reference:

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#toString()

CodePudding user response:

You need to set the property of the data class first-

data.id = id;
data.code = code;
result.add(data);

Then you can iterate the result by-

result[index].id;
result[index].code;

CodePudding user response:

Add Getters and Setters in Data class like bellow.

public class Data {
private String id;
private String code;

public String getId(){
   return this.id;
}

public void setId(String id){
  this.id = id;
}

public String getCode(){
   return this.code;
}

public void setCode(String code){
  this.code = code;
}

}

Then you should set your data to Data Object like bellow.

public class FileProcess {
  public List<Data> process(InputStream InputStream) throws IOException {
  List<Data> result = new ArrayList<>();
  String line;

  try(// scanning inputstream) {
     line = sc.nextLine();
     while (line != null) {
    // read thru each line in the txt file

      String id = line.subString(0, 5);
      String code = line.subString(6, 11);

      Data data = new data();
      data.setId(id);
      data.setCode(code);
      result.add(data);
  }
  return result;
}

}

CodePudding user response:

The Answer by admiz635 is correct. You need to add a constructor and an override of toString.

record

You can get both of those by defining your class as a record in Java 16 . A record is appropriate when the main purpose of your class is to communicate data transparently and immutably.

In a record, the compiler implicitly creates the constructor, getters, equals & hashCode, and toString.

By the way, you should devise a more descriptive class name than Data.

record Data ( String id , String code ) {}

FYI, a record can be defined locally within a method, or defined nested within a class, or as its own class in its own .java file.

Example usage.

String[] parts = line.split( " " ) ;
Data d = new Data( parts[0] , parts[1] ) ;
results.add( d ) ;
  • Related