-2

I have a text file with the following format(the words at each line are seperated with tab):

stri1   stri2   stri3
stri4   stri5   stri6
stri7   stri8
stri9   stri0   stri5

As you can see i have some lines with only two words. I have a class to save the words of each line:

public class Entity{
   private word1,word2,word3;
   
   //constructor and getter/setter methods
}

I want to save the text values using the following code:

for(String i : filelines){
   String[] line = i.split("\t");
   if(line[2] == null){
      listOfEntities.add(new Entity(line[0], line[1], null));
   }
   else{
      listOfEntities.add(new Entity(line[0], line[1], line[2]));
}

When i try to execute this code i get an ArrayIndexOutOfBoundsException because some line have only 2 words. How can i handle this situation because i want also the null values in order to make some sql queries later.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
mariohez
  • 31
  • 4
  • 1
    How is this related to C++? Please don't add irrelevant tags. – Some programmer dude Mar 10 '21 at 20:37
  • As for your problem, even rather mediocre tutorials, books or classes should have some information about how to get the size of an array. Once you know how to get the size of the array `line`, then you can easily solve your problem. – Some programmer dude Mar 10 '21 at 20:38

2 Answers2

0

Mod with length of array will return 0 if array exceed legth it will store zeroth index other than that you have to do multiple ifs as you have done for second index. Another way I would suggest pass the whole array in Entity parameter and in Entity class run a for loop till array length which will run till available lines in array only.

for(String i : filelines){
   String[] line = i.split("\t");
   if(line[2 % line.length] == null){
      listOfEntities.add(new Entity(line[0], line[1 % line.length], null));
   }
   else{
      listOfEntities.add(new Entity(line[0], line[1 % line.length], line[2 % line.length]));
}
Syed Mohib Uddin
  • 718
  • 7
  • 16
0

I have another option for you. You have to separate the options in the file with an element. This looks like this:

  • stri1-stri2-stri3
  • stri4-stri5-stri6
  • stri7-stri8
  • stri9-stri0-stri5

For this you have to change your entity class a little bit:

public static class Entity {

    private String[] words;

    public Entity(String[] words) {
        this.words = words;
    }

    public String[] getWords() {
        return words;
    }

}

To read out the whole thing now, I wrote you a code.

    List<Entity> array = new ArrayList<>();
    Scanner scanner = new Scanner(new File("MyFile.txt"));

    while (scanner.hasNext()) {
        String line = scanner.next();
        String[] options = line.split("-");
        array.add(new Entity(options));
    }

    array.forEach(entity -> {
        System.out.println("Entity Words > " + Arrays.toString(entity.getWords()));
    });

I hope I could help you with this.

ZoneCloud
  • 1
  • 2
  • @user15358848 i tried it with other methods but i have found this as a better way. If there is a TAB element between the two words, only the first element of the line is added. – ZoneCloud Mar 11 '21 at 06:29
  • @user15358848 Okay, you could use any character to separate the words. But it is better (in my opinion) not to use a space / TAB as a placeholder. In general I don't stand to read out something like that but rather work with either YAML or JSON. See Google Gson: [link](https://github.com/google/gson) its a better way, to save / read objects in a file or database. – ZoneCloud Mar 11 '21 at 10:38
  • @user15358848 If you have a question or don't know something, why not test it out for yourself? And I wrote that it doesn't really matter what you use as a placeholder. If you think you know better, then show me a better source. There is the so-called Internet, through which you can find out something yourself. I have tried to explain this to you, but if you don't understand the explanation, then just use Google. – ZoneCloud Mar 11 '21 at 11:28