2014-11-03 85 views
0

下面你可以看到我的代码。它读取sort.txt其中存在由lenghts第一格式化然后像alhabeticly这样的话:如何在java中跳过txt文件中的行

  • 一个
  • b
  • AB
  • 交流
  • ABC
  • ACD
  • AAAA

每个字中的一个字线。下面的程序是什么增加了单词列表1中的单词长度1,单词长度2数组列表2等。它工作正常,但是当它读取让我们说arraylist3它读取整个sort.txt。我怎样才能做到这一点,所以它跳过单词长度1和2直接到单词长度3并将它们添加到列表3。然后当第一个单词长度为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(); 

} 
}} 
+1

点,而不是列表1,列表2,项目list3,list4等,有一个列表 - 列表ArrayList lists = new ArrayList ()'然后简单地抓住lists.get(...)来添加你的项。而不是10个块都完全相同,现在你有1个块,这是更容易分析和修复。 – 2014-11-03 19:28:14

+0

@ Mike'Pomax'Kamermans奇怪的是,根据他自己的要求,他似乎并不需要多个列表。 – furkle 2014-11-03 19:33:06

回答

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

y基本上我需要为所有的例子做这个,即使让我们说所有其他的人都需要滑雪。相同的6,7,8等... – 2014-11-03 19:35:04

+0

使用变量,我编辑了我的答案 – 2014-11-03 19:39:20

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或更大的所有单词。

+0

真的是新的Java?所以创建1个数组列表?然后从中搜索? – 2014-11-03 19:37:12

+0

@RokIvartnik好吧,根据你的问题,你要做的就是保存长度为3的单词。你正在跳过1和2,结束于4,那么为什么其他9个ArrayList呢? – furkle 2014-11-03 19:39:10

+0

aaah no。我需要所有单词的长度。因此,长度为4的单词需要跳过所有1,2,3并保存长度为4并以5结尾。长度为5的单词需要跳过1,2,3,4保存5并以6结束。以此类推lenghts。 – 2014-11-03 19:42:59

1

你必须按顺序做,但你可以更快地做到这一点:使用数组或的ArrayList:编程

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