2015-10-12 138 views
-1

即时通讯我的扫描仪读取我的文本文件与delemiter有问题。我试着去每个字符串保存在一个数组但是当我打印出来的阵列保持emty ... IDK为什么扫描仪和分隔符的问题

 Scanner s = new Scanner(file); 
     s.useDelimiter("@"); 


     int length = (upperBound - lowerBound)+1; 
     String [] Entries = new String[length]; 

     for(int i=0; i < length; i++) 
     { 
      Entries[i] = s.next(); 
     } 

    //When I print this the array location is empty:(   
    System.out.println(Entries[0]); 

这是我的文本文件的内容:

The Germans occupy the rump Czech [email protected] invades Denmark and [email protected] Germany and its Axis partners invade the Soviet [email protected] 
+0

嗨!我无法帮助你,因为我不知道这是哪种编程语言。添加语言标签! –

+0

我认为upperBound - lowerBound必须是非积极的 – ergonaut

回答

0

使用while循环读取文件和存储项目到数组是这样的:

Scanner s = new Scanner(file); 
    int length = (upperBound - lowerBound)+1; 
    String [] Entries = new String[length]; 
    int count = 0; 
    while (s.hasNext()){ 
     s.useDelimiter("@"); 
     Entries[count] = s.next(); 
     count++; 
    } 

使用for循环打印出你的阵列是这样的:

for (int i = 0; i < Entries.length; i++) { 
     System.out.println(Entries[i]); 
    } 

我还建议使用“条目”作为变量名称。保留变量名称是一种很好的做法CamelCase