2015-04-20 56 views
0

所以我需要这个从文件中读取数据,然后在接收到两个整数后在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) 
+0

只是一个小纸条:确认你看这数据,在添加的println变量名:'的System.out.println( “杜尔=” + DUR);' – moffeltje

+0

什么你想让我们展示吗? – moffeltje

回答

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 
    } 
} 
+0

非常感谢你!这帮了我很多。我仍然抛出另一个异常,但它不是直到文件结束时,一行只是字母。我应该能够自己找出那一个,尽管 – Ajlec12

1

你可能会得到这样的例外在这里:

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

由于说的javadoc,一个MismatchException在情况下抛出你尝试获得“nextInt()”时,它不是一个int:

/** 
* 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

@Tom true。编辑。 –

+0

现在好多了:)。 – Tom