如何跳过java中的txt文件中的行

时间:2014-11-03 19:23:34

标签: java

下面你可以看到我的代码。它读取sort.txt,其中首先由长度形成单词然后像糖尿病一样形成:

  • 一个
  • B'/ LI>
  • AB
  • AC
  • ABC
  • ACD
  • AAAA

每行一个字。下面的程序会在数组list1中添加单词lenght 1,单词lenght 2 array list2等。它工作正常,但当它读取时,让我们说arraylist3它读取整个sort.txt。我怎么能这样做它跳过单词lenght 1和2直接转到单词lenght 3并将它们添加到list3。然后当第一个单词长度为4时发现它停止了。所以我不必一遍又一遍地阅读整个文件?

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();

}
}}

3 个答案:

答案 0 :(得分:1)

你必须按顺序进行,但你可以更快地完成它:

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.
}

答案 1 :(得分:0)

你的意思是这样的?跳过长度1和2会增加长度3,并在找到长度4时停止。

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;
    }
}

答案 2 :(得分:0)

您可以使用break关键字停止while循环操作。如果你想要的只是保留长度为3的单词:

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

如果条件中的任何内容都不符合该长度的线条,则只需丢弃读取线,因此无需检查一个或两个。请注意,此解决方案依赖于精确排序的文件,以便所有长度为4的字符串出现在长度为3的所有字符串之后。

好的,根据你的评论,似乎你可能在陈述你想要的东西时有些困惑。如果您想要的是将长度为1的单词放在一个列表中,将长度为2的单词放在下一个列表中,等等,这就是一个解决方案:

// 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);
    }
}

在您阅读完文件后,wordArrayLists的第一个索引将是仅包含1个长度的单词的ArrayList,第二个索引将只包含2个长度的单词,等到第10个索引,它将包含长度为10或更大的所有单词。