Topic: 各位帮我看一下这个异常怎么解决?

  Print this page

1.各位帮我看一下这个异常怎么解决? Copy to clipboard
Posted by: oatregnaf
Posted on: 2004-12-09 13:08

这是《Data Sructures & Algorithms in java》中的一个关于分割符匹配的一个程序。Smile

//brackets.java
//stack used to check mathing breakets

import java.io.*; // for I/O
////////////////////////////////////////////////////////////////////////////////////////
class StackX
{
  private int maxSize;
  private char[] stackArray;
  private int top;
  //---------------------------------------------------------
  public StackX(int s)
  {
    maxSize = s;
    stackArray = new char[maxSize];
    top = -1;
    }
  //---------------------------------------------------------
  public void push(char j)
  {
    stackArray[++top] = j;
    }
  //---------------------------------------------------------
  public char pop()
  {
    return stackArray[top--];
    }  
  //---------------------------------------------------------
  public char peep()
  {
    return stackArray[top];
    }
  //---------------------------------------------------------
  public boolean isEmpty()
  {
    return(top==-1);
     }
   //--------------------------------------------------------
  } // end class StackX

////////////////////////////////////////////////////////////////////////////////////////////////

class BracketCher
{
  private String input;
  //-------------------------------------------------------------
  public BracketCher(String in)
  { input = in;}
    public void check()
    {
    int stackSize = input.length();
    StackX theStack = new StackX(stackSize);
    
    for(int j=0;j<input.length();j++)
    {
      char ch = input.charAt(j);
      switch(ch)
      {
        case'{':
        case'[':
        case'(':
          theStack.push(ch);
          break;
        case'}':
        case']':
        case')':
          if(!theStack.isEmpty())
          {
            char chx = theStack.pop();
            if((ch=='}' && chx!='{') || (ch==']' && chx!='[') || (ch==')' && chx!'('))
              System.out.println("Error: "+ch+"at"+j);
            }
          else
            System.out.println("All right!");
            break;
        default:
          break;
    
        } // end switch
      } // end for
    if(!theStack.isEmpty())
       System.out.println("Error: missing right delimter");
    }// end check()
  //------------------------------------------------------------
  } // end class BracketCher

///////////////////////////////////////////////////////////////////////////////////////////////

class BracketsApp
{
  public static void main(String[] args)
  {
    String input;
    while(true)
    {
      System.out.println("Enter string containing delimiters:");
      System.out.flush();
      input = getString();
      if(input.equals(""))
        break;
        
      BracketCher theChecker = new BracketCher(input);
      theChecker.check();
      } // end while
    } // end main()
  //--------------------------------------------------------------
  public static String getString() throws IOException
  {
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    String s = br.readLine();
    return s;  
    }
  } // end class BracketsApp

前两个类编译都是正常的,第三个类编译的时候有这样的异常:
BreacketsApp.java:12: unreported exception java.io.IOException; must be caught or declared to be thrown
input = getString();
^
1 error

小弟实在是找不出错误的原因请各位 指点一二


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923