【问题标题】:Scanner text file not working扫描仪文本文件不起作用
【发布时间】:2017-10-16 09:52:24
【问题描述】:

我正在尝试扫描带有字符(pta、...)的文本文件,以便每个字母都匹配某个图像。但首先我必须阅读这封信,然后与图像建立联系。

Scanner sc = new Scanner(textfile);
int lenght = 0;
while (sc.hasNextLine()) {              
    try {
        for (int j = 0; j != 0; j++) {                      
            ImageMatrixGUI.getInstance().addImage(Land);
            ArrayImages.add(Land);                  
        }
        char tipo = sc.next().charAt(0);            
        System.out.println(tipo);               
        if (tipo == 'p') {
            ImageMatrixGUI.getInstance().addImage(pine);
        }
    }
}

【问题讨论】:

  • 不工作是什么意思?你得到什么输出?
  • for 循环应该在做什么?
  • 我无法读取文本文件中的字符
  • 好的,但是发生了什么?

标签: java string java.util.scanner


【解决方案1】:

嗯嗯,您确定您的 Scanner 声明正确吗? 应该是这样的:

Scanner sc = new Scanner(new File("FilePath"));
while(sc.hasNextLine()){
   String letters = sc.nextLine();
   System.out.println(letters);
}

这样您就可以读取文件中的每一个字母。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 1970-01-01
    相关资源
    最近更新 更多