2012-04-08 80 views
1

我有一个文本文件,其中包含11个整数的名称。我需要将这些数字解析为一个数组,其中存储在表示该值(0-10)的维度中的11个整数值中的最大值。解析文本文件输入到数组中

我还需要保持名称的完整性。

例如:

艾莉雅0 0 0 0 0 0 0 0 0 380 215:我正在寻找像艾莉雅输出[9],并且存储在该元件

Aaron 193 208 218 274 279 232 132 36 32 31 41 
Abagail 0 0 0 0 0 0 0 0 0 0 958 
Abbey 0 0 0 0 0 0 0 0 537 451 428 
Abbie 431 552 742 924 0 0 0 0 752 644 601 
Abbigail 0 0 0 0 0 0 0 0 0 953 562 
Abby 0 0 0 0 0 906 782 548 233 211 209 
Abdiel 0 0 0 0 0 0 0 0 0 925 721 
Abdul 0 0 0 0 0 0 0 903 0 0 0 
Abdullah 0 0 0 0 0 0 0 0 0 1000 863 
Abe 248 328 532 764 733 0 0 0 0 0 0 

380值这是代码我迄今:

import java.io.*; 
import java.util.*; 

public class Names 
{ 
    private static String name = "bb"; 
    private static int [] yearRank; 
    private static boolean match; 

     public static void main(String[] args) 
     { 
      java.io.File file = new java.io.File("names.txt"); 
      try 
      { 
       Scanner input = new Scanner(file); 
       while (input.hasNext()) 
       { 
        String num = input.nextLine(); 
        if(match = num.toLowerCase().contains(name.toLowerCase())) 
        { 
         for (int i = 0; i<11; i++) 
         { 

          System.out.println(num); 
         } 
        } 
       } 
      catch(FileNotFoundException e) 
      { 
       System.err.format("File does not exist\n"); 
      } 
     } 

} 

回答

0

num没有你的循环内改变 - 如果你知道空格分隔不同的领域,你可以做,例如(未测试):

String num = input.nextLine(); 
if(match = num.toLowerCase().contains(name.toLowerCase())) { 
    String[] fields = num.split(" "); //split the line into an array, each item contains a field 
    for (String field : fields) { //loop over the array 
     System.out.println(field); //print each field 
    } 
} 
+0

谢谢,这让我走上了正轨。 – 2012-04-08 20:05:55

+0

如果解决了您的问题,您可以[接受答案](http://meta.stackexchange.com/a/5235/179508)。 – assylias 2012-04-08 21:53:27