【问题标题】:Trouble with scanner and delimiter扫描仪和分隔符有问题
【发布时间】:2015-10-12 18:30:12
【问题描述】:

我的扫描仪使用分隔符读取我的文本文件时遇到问题。我试图将每个字符串保存在一个数组中,但是当我打印它时,数组保持为空......我知道为什么

        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 lands@Germany invades Denmark and Norway@Nazi Germany and its Axis partners invade the Soviet Union@

【问题讨论】:

  • 嗨!我帮不了你,因为我不知道这是哪种编程语言。添加语言标签!
  • 我认为upperBound - lowerBound 一定是非正数

标签: arrays string file delimiter


【解决方案1】:

使用 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

中是一种很好的做法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-03
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多