阅读数据和例外

时间:2015-04-20 11:38:15

标签: java exception

所以我需要这个来读取文件中的数据,然后在接收到两个整数后在finch上播放音调。在文件中有一些整数在其中有字母,如75K,我想跳过它并删除对中的另一个整数。我不认为我完全理解异常但有人可以告诉我吗?这是代码(我输入println所以我可以看到发生了什么):

public static void main(String[] args) {
        Finch finch = new Finch();
        int dur, freq;
        Scanner inputStream = null;
        String trash;

        try {
            inputStream = new Scanner(new File("F:\\Java\\NetbeansFinchFolder\\SourceFiles\\Code\\Lab5Data.txt"));
        } catch (Exception e) {
            System.out.println("invalid input");
        } 
        while (inputStream.hasNext()) {

            if (inputStream.hasNextInt()) {
                dur = inputStream.nextInt();
                freq = inputStream.nextInt();
                System.out.println(dur);
                System.out.println(freq);
                if (dur > -1 && freq > -1) {
                    finch.setLED(Color.GREEN);
                    finch.playTone(freq, dur);

                }

            } else if(!(inputStream.hasNextInt()))
            {
                finch.setLED(Color.RED, 1000);
                trash = inputStream.next();
                trash = inputStream.next();
            }
        }

    }

}

输出:

Connecting to Finch...this may take a few seconds...
262
500
262
500
262
500
294
250
330
500
330
250
294
250
330
250
349
250
392
500
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at Code.LabAssign5.main(LabAssign5.java:34)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)

2 个答案:

答案 0 :(得分:1)

你可能在这里遇到这个例外:

if (inputStream.hasNextInt()) {
  dur = inputStream.nextInt();
  freq = inputStream.nextInt(); //Here, you did not check that freq is an int.

正如javadoc所说,如果你试图在不是int的情况下获得“nextInt()”时抛出MismatchException:

/**
 * Scans the next token of the input as an <tt>int</tt>.
 *
 * <p> An invocation of this method of the form
 * <tt>nextInt()</tt> behaves in exactly the same way as the
 * invocation <tt>nextInt(radix)</tt>, where <code>radix</code>
 * is the default radix of this scanner.
 *
 * @return the <tt>int</tt> scanned from the input
 * @throws InputMismatchException
 *         if the next token does not match the <i>Integer</i>
 *         regular expression, or is out of range
 * @throws NoSuchElementException if input is exhausted
 * @throws IllegalStateException if this scanner is closed
 */
public int nextInt() {
    return nextInt(defaultRadix);
}

在您的情况下,75K不是int,您必须手动解析它。

一些提示:

  • String.split(String regex)与好的分隔符一起使用
  • 使用Matcher和相应的正则表达式。

以下是Matcher(数字)\d的示例:

String s = "75K 28m";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(s);
while (m.find()) {
  System.out.println(m.group());
}

输出:

75
28

另一个例子展示了如何在非数字(\\D)上分割字符串:

String s = "75K 28m";
String[] result = s.split("\D+");
System.out.println(result[0]);
System.out.println(result[1]);

输出:

75
28

答案 1 :(得分:0)

你可以做这样的事情来捕捉异常并以某种方式处理它:

if( inputStream.hasNextInt() )
{
    try
    {
        dur = inputStream.nextInt();
        freq = inputStream.nextInt();
        System.out.println( dur );
        System.out.println( freq );
        if( dur > -1 && freq > -1 )
        {
            finch.setLED( Color.GREEN );
            finch.playTone( freq, dur );
        }
    }
    catch( InputMismatchException exception )
    {
        // do exception handling
    }
}