简体   繁体   English

如何在Java中使用扫描仪读取.txt文件时删除inputMismatchException

[英]how to remove inputMismatchException while reading a .txt file using scanner in java

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

public class FrequencyCount
{
    public static void main(String...args) throws IOException
    {
        int count0=0;
        int count1=0;
        int x=0;
        //System.out.println(new File(".").getCanonicalPath());
        Scanner scan=new Scanner(new File("F:\\DUCAT COCHNG\\java progs\\IO\\str.txt.txt"));
        while(scan.hasNext())
        {
            x=scan.nextInt();
            if(x==0)
                count0++;
            else if(x==1)
                count1++;
        }   

        System.out.println("Frequency of 0's is: "+count0);
        System.out.println("Frequency of 1's is: "+count1);

    }
}

output shown is: 显示的输出是:

F:\\DUCAT COCHNG\\java progs\\IO>java FrequencyCount Exception in thread "main" java.util.InputMismatchException: For input string: " 00000111010010011001010010100101010001000101110111110101010011101001001010010010 1" at java.util.Scanner.nextInt(Scanner.java:2165) at java.util.Scanner.nextInt(Scanner.java:2118) at FrequencyCount.main(FrequencyCount.java:15) F:\\ DUCAT COCHNG \\ java progs \\ IO> java FrequencyCount线程“ main”中的异常java.util.InputMismatchException:对于输入字符串:java.util.Scanner.nextInt(Scanner.java:2165)上的java.util.Scanner.nextInt(Scanner.java:2165)的“ 00000111010010011001010010100101010010101001010100” 1 .util.Scanner.nextInt(Scanner.java:2118)位于FrequencyCount.main(FrequencyCount.java:15)

F:\\DUCAT COCHNG\\java progs\\IO> F:\\ DUCAT COCHNG \\ java编\\ IO>

str.txt file is: str.txt文件是:

000001110100100110010100101001010100010001011101111101010100111010010010100100101 000001110100100110010100101001010100010001011101111101010100111010010010100100101

I think this may help: 我认为这可能会有所帮助:

try{
        Scanner scan = new Scanner(new File(filename));
        while (scan.hasNext())
        {
           String str = scan.next(); 
            char[] myChar = str.toCharArray();
            for(char c: myChar){
                x=Character.getNumericValue(c);
                if(x==0) 
                    count0++; 
                else if(x==1) 
                    count1++; 
            }
        }
       System.out.println("Frequency of 0's is: "+count0);
       System.out.println("Frequency of 1's is: "+count1);
   }
  catch(Exception e)
  { e.printStackTrace();}

You want to use scan.hasNextInt() instead of scan.hasNext() . 您要使用scan.hasNextInt()而不是scan.hasNext() The latter checks for any token, and I suspect you have a new line at the end of your file that scanner is parsing. 后者检查是否有任何令牌,我怀疑您在扫描程序正在解析的文件末尾有新行。 This obviously can't be converted to an integer, and so the exception is thrown. 显然不能将其转换为整数,因此将引发异常。

You may also want to close your scanner as well. 您可能还需要关闭scanner Just use scan.close() . 只需使用scan.close() Not strictly necessary here in such a small program, but good practice. 在如此小的程序中,这不是严格必要的,但是是很好的实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM