简体   繁体   中英

how to skip lines in txt file in java

Below you can see my code. It reads sort.txt in which there are words formated first by lenghts then alhabeticly like so:

  • a
  • b
  • ab
  • ac
  • abc
  • acd
  • aaaa

One word in each line. What below program does it adds words lenght 1 in array list1, words lenght 2 array list2 ect. It works fine but when it reads lets say arraylist3 it read the whole sort.txt. How can i make it so it skips words lenght 1 and 2 goes directly to words lenght 3 and adds them to list3. And then When first word lenght 4 in found it stops. So i dont have to read the whole file over and over again?

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class swithc {

public static void main(String[] args) {
String izbira;
int dolzina=0;
Scanner in = new Scanner(System.in);
String vnos;
Scanner input = new Scanner(System.in);

ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();
ArrayList list4 = new ArrayList();
ArrayList list5 = new ArrayList();
ArrayList list6 = new ArrayList();
ArrayList list7 = new ArrayList();
ArrayList list8 = new ArrayList();
ArrayList list9 = new ArrayList();
ArrayList list10plus = new ArrayList();

try {

    File file = new File("sort.txt");
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String vrstica;

    while ((vrstica = bufferedReader.readLine()) != null) {
        if (vrstica.length() == 1) {
            list1.add(vrstica);
        }
        if (vrstica.length() == 2) {
            list2.add(vrstica);
        }
        if (vrstica.length() == 3) {
            list3.add(vrstica);
        }
        if (vrstica.length() == 4) {
            list4.add(vrstica);
        }
        if (vrstica.length() == 5) {
            list5.add(vrstica);
        }
        if (vrstica.length() == 6) {
            list6.add(vrstica);
        }
        if (vrstica.length() == 7) {
            list7.add(vrstica);
        }
        if (vrstica.length() == 8) {
            list8.add(vrstica);
        }
        if (vrstica.length() == 9) {
            list9.add(vrstica);
        }
        if (vrstica.length() > 9) {
            list10plus.add(vrstica);
        }
    }
    do{
        do {
            System.out.println("Vnesi dožino besede, ki jo iščeš:");
            if (in.hasNextInt()) {
                dolzina = in.nextInt();
            } else if (in.hasNextLine()) {
                System.out.printf("Napačen vnos! Poskusi ponovno:%n ",
                        in.nextLine());
            } 
        } while (dolzina <= 0);



    System.out.println("Vnesi besedo za neznano črko vpiši * :");
    vnos = input.nextLine();
    vnos = vnos.replace("*", ".");

    if (dolzina == 1) {
        for (int i = 0; i < list1.size(); i++) {
            String s = (String) list1.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }

    if (dolzina == 2) {
        for (int i = 0; i < list2.size(); i++) {
            String s = (String) list2.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }
    if (dolzina == 3) {

        for (int i = 0; i < list3.size(); i++) {
            String s = (String) list3.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 4) {

        for (int i = 0; i < list4.size(); i++) {
            String s = (String) list4.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 5) {
        for (int i = 0; i < list5.size(); i++) {
            String s = (String) list5.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 6) {
        for (int i = 0; i < list6.size(); i++) {
            String s = (String) list6.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 7) {
        for (int i = 0; i < list7.size(); i++) {
            String s = (String) list7.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 8) {
        for (int i = 0; i < list8.size(); i++) {
            String s = (String) list8.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina == 9) {
        for (int i = 0; i < list9.size(); i++) {
            String s = (String) list9.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }
    }
    if (dolzina > 9) {
        for (int i = 0; i < list10plus.size(); i++) {
            String s = (String) list10plus.get(i);
            if (s.matches(vnos))
                System.out.println(s);
        }

    }
    dolzina=-1;
    System.out.println("Ponovni vnos (da/ne):");
    Scanner inn= new Scanner (System.in);
    izbira = inn.next();

}while (izbira.equalsIgnoreCase("da"));
    bufferedReader.close();
} catch (IOException e) {
    e.printStackTrace();

}
}}

You would have to do it sequentially, but you can do it faster:

int TARGET_LEN = 3;
...
while ((vrstica = bufferedReader.readLine()) != null) {
    if (vrstica.length() < TARGET_LEN) {
        continue;
    } else if (vrstica.length() > TARGET_LEN) {
        break;
    }
    // Here you know that your word is of length TARGET_LEN
    // Add it to your list:
    list.add(vrstica); // You do not need many lists now, only one.
    // Do whatever else you may need to do.
}

You mean something like this? This skips length 1 and 2 adds length 3 and stops when length 4 is found.

int num = 3;
while ((vrstica = bufferedReader.readLine()) != null) {
    //Do nothing for length 1 and 2
    if (vrstica.length() == num) {
        list3.add(vrstica);
    } else if (vrstica.length() > num) {
        //stop reading the file
        break;
    }
}

What you could do is use the break keyword to stop operation on your while loop. If all you want is to keep words of length 3:

while ((vrstica = bufferedReader.readLine()) != null) {
    if (vrstica.length() == 3) {
        list3.add(vrstica);
    }  else if (vrstica.length() == 4) {
        break;
    }
}

There's no need to check one or two, given that the read line will just be discarded if nothing within your conditionals fits a line of that length. Note that this solution relies upon the file being ordered precisely so that all strings of length 4 occur after all strings of length 3.

Okay, based on your comment, it seems like you may have been somewhat confusing in stating what you wanted. If what you want is to put words with length 1 in one list, and words with length 2 in the next, etc., here's a solution:

// note that ArrayList needs a type it will contain
ArrayList<ArrayList<string>> wordArrayLists = new ArrayList<ArrayList<string>>();

// fill the top ArrayList with 10 ArrayList<string>s
for (int i = 0; i < 10; i++) {
    wordArrayLists.add(ArrayList<string>());
}

while ((vrstica = bufferedReader.readline()) != null) {
    int index = vrstica.length - 1;

    // skip if the word length is 0 or greater than 10
    if (index < 0) { 
        continue;
    }

    if (index >= 10) {
        wordArrayLists(9).add(vrstica);
    }
    else {
        wordArrayLists.get(index).add(vrstica);
    }
}

After you've finished reading the file, the first index of the wordArrayLists will be an ArrayList containing only 1-length words, the second index will contain only 2-length words, etc. up to the 10th index, which will contain all words of length 10 and greater.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM