NoSuchMethodException

0

Może ktoś dokładnie opisac kiedy występuje wyjątek bo w programie za nic nie moge doszukac się błędów.

0

wyjatek jest to informacja ze w programie nastapil jakis blad . wyjatki sa wyzwalane slowem throw lub wylapywane przez catch .

twoje pytanie jest zbyt ogolne , pokaz lepiej ten kod .

0

Głónwa częśc tego kodu pochodzi z książki java sztuka programowania wydawnictwa helion. Chciałem to tego analizatora dodac obsługe stringów ale gdy w klasie, która tstuje ten analizator po wywołanu metody evaluate nie wypisuje żadnych wyników 9a wcześniej nie wiem czemu wywalało NoSucgMethodException.

// Klasa wyjątów dla błędów analizatora.  
class ParserException extends Exception {  
  String errStr; // opis błędu 
 
  public ParserException(String str) { 
    errStr = str; 
  }   
  
  public String toString() {  
    return errStr; 
  }  
}  

//Klasa wyniku
class Result{
    double nval;
    String sval;
    private boolean typ; 
    public Result(){
        
    }
    public Result(double liczba){
        nval = liczba;
        typ = false;
    }
    public Result(String napis){
        sval = napis;
        typ = true;
    }
    public boolean what(){
        if(typ == true)
            return true;
        return false;
    }
    public void ustTyp(boolean jaki){
        typ = jaki;
    }
}
  
class Parser {  
  // Rodzaje tokenów. 
  final int NONE = 0; 
  final int DELIMITER = 1; 
  final int VARIABLE = 2; 
  final int NUMBER = 3; 
  final int STRING = 4;
 
  // Rodzaje błędów składniowych. 
  final int SYNTAX = 0; 
  final int UNBALPARENS = 1; 
  final int NOEXP = 2; 
  final int DIVBYZERO = 3; 
 
  // Token oznaczający koniec wyrażenia. 
  final String EOE = "\0"; 
 
  private String exp;   // teksty wyrażenia  
  private int expIdx;   // aktualny indeks w wyrażeniu  
  private String token; // aktualny token  
  private int tokType;  // typ tokenu 
  private String stype; 
  
  // Początek analizatora.  
  public String evaluate(String expstr) throws ParserException 
  {  
    Result result = new Result();  
    exp = expstr;  
    expIdx = 0;  
    String gotowe;
   
    getToken();  
    if(token.equals(EOE))  
      handleErr(NOEXP); // brak wyrażenia  
 
    // Przeanalizuj i oblicz wyrażenie. 
    result = evalExp2(); 
    if(result.what() == true){
        gotowe = result.sval;
    }
    else{gotowe =  Double.toString(result.nval);}
  
    if(!token.equals(EOE)) // ostatnim tokenem musi być EOE  
      handleErr(SYNTAX);  
    gotowe = "";
    return gotowe;  
  }  
    
  // Dodanie lub odjęcie dwóch elementów.  
  private Result evalExp2() throws ParserException 
  {  
    char op;  
    Result result; 
    Result partialResult;
    
    result = new Result();
    partialResult = new Result();
 
    result = evalExp3();  
 
    while((op = token.charAt(0)) == '+' || op == '-') {  
      getToken();  
      partialResult = evalExp3();  
      switch(op) {  
        case '-':  
          result.nval = result.nval - partialResult.nval;  
          break;  
        case '+':
          if(result.what() == true){
              if(partialResult.what() == true){
                  result.sval = result.sval + partialResult.sval;
                  result.ustTyp(true);
              }
              else{
                  handleErr(SYNTAX);
              }
          }
          else if(result.what() == false){
              if(partialResult.what() == false){
                  result.nval = result. nval + partialResult.nval;
                  result.ustTyp(false);
              }
              else{
                  handleErr(SYNTAX);
              }
          }           
          break;  
      }  
    }  
    return result; 
  }  
    
  // Pomnożenie lub podzielenie dwóch elementów.  
  private Result evalExp3() throws ParserException 
  {  
    char op;  
    Result result; 
    Result partialResult; 
    
    result = new Result();
    partialResult = new Result();
    
    result = evalExp4();  
 
    while((op = token.charAt(0)) == '*' ||  
           op == '/' || op == '%') {  
      getToken();  
      partialResult = evalExp4();  
      switch(op) {  
        case '*':  
          result.nval = result.nval * partialResult.nval;  
          break;  
        case '/':  
          if(partialResult.nval == 0.0)  
            handleErr(DIVBYZERO);  
          result.nval = result.nval / partialResult.nval;  
          break;  
        case '%':  
          if(partialResult.nval == 0.0)  
            handleErr(DIVBYZERO);  
          result.nval = result.nval % partialResult.nval;  
          break;  
      }  
    }
    result.ustTyp(false);
    return result; 
  }  
    
  // Przetworzenie potęgowania.  
  private Result evalExp4() throws ParserException 
  {  
    Result result; 
    Result partialResult; 
    
    result = new Result();
    partialResult = new Result();
    double ex;  
    int t;  
    
    result = evalExp5();  
 
    if(token.equals("^")) {  
      getToken();  
      partialResult = evalExp4();  
      ex = result.nval;  
      if(partialResult.nval == 0.0) {  
        result.nval = 1.0;  
      } else  
        for(t=(int)partialResult.nval-1; t > 0; t--)  
          result.nval = result.nval * ex;  
    }  
    result.ustTyp(false);
    return result; 
  }  
    
  // Obliczenie pojedynczego + lub -.  
  private Result evalExp5() throws ParserException 
  {  
    Result result; 
    String  op; 
    result = new Result();
 
    op = "";  
    if((tokType == DELIMITER) &&  
        token.equals("+") || token.equals("-")) {  
      op = token;  
      getToken();  
 
    }  
    result = evalExp6();  
 
    if(op.equals("-")) result.nval = -result.nval;
    
    result.ustTyp(false);
    return result;  
  }  
    
  // Obsługa wyrażeń z nawiasami.  
  private Result evalExp6() throws ParserException 
  {  
    Result result; 
    result = new Result();
 
    if(token.equals("(")) {  
      getToken();  
      result = evalExp2();  
      if(!token.equals(")"))  
        handleErr(UNBALPARENS);  
      getToken();  
    }  
    else result = atom();  
 
    return result; 
  }  
    
  // Pobranie wartości liczby.  
  private Result atom() throws ParserException  
  {  
    Result result = new Result(); 
 
    switch(tokType) {  
      case NUMBER:  
        try {  
          result = new Result(Double.parseDouble(token));
          result.ustTyp(false);
        } catch (NumberFormatException exc) {  
          handleErr(SYNTAX);  
        }  
        getToken();  
        break;
      case STRING:
          result = new Result(stype);
          result.ustTyp(true);          
      default:  
        handleErr(SYNTAX);  
        break;  
    }  
    return result; 
  }  
    
  // Obsługa błędu.  
  private void handleErr(int error) throws ParserException 
  {  
    String[] err = {  
      "Błąd składniowy",  
      "Brak jednego z nawiasów",  
      "Brak wyrażenia",  
      "Dzielenie przez zero"  
    };  
  
    throw new ParserException(err[error]);  
  }  
    
  // Pobranie następnego tokenu.  
  private void getToken()  
  {  
    tokType = NONE;  
    token = "";  
     
    // Sprawdzenie końca wyrażenia.  
    if(expIdx == exp.length()) { 
      token = EOE; 
      return; 
    } 
    
    // Pominięcie białych spacji. 
    while(expIdx < exp.length() &&  
      Character.isWhitespace(exp.charAt(expIdx))) ++expIdx;  
  
    // Pominięcie spacji na końcu wyrażenia. 
    if(expIdx == exp.length()) { 
      token = EOE; 
      return; 
    } 
  
    if(isDelim(exp.charAt(expIdx))) { // czy operator  
      token += exp.charAt(expIdx);  
      expIdx++;  
      tokType = DELIMITER;  
    }  
    else if(Character.isLetter(exp.charAt(expIdx))) { // czy zmienna  
      while(!isDelim(exp.charAt(expIdx))) {  
        token += exp.charAt(expIdx);  
        expIdx++;  
        if(expIdx >= exp.length()) break;  
      }  
      tokType = VARIABLE;  
    }  
    else if(Character.isDigit(exp.charAt(expIdx))) { // czy liczba  
      while(!isDelim(exp.charAt(expIdx))) {  
        token += exp.charAt(expIdx);  
        expIdx++;  
        if(expIdx >= exp.length()) break;  
      }  
      tokType = NUMBER;  
    } 
    else if(isString(exp.charAt(expIdx))){
        expIdx++;
        while(!isString(exp.charAt(expIdx))){
            stype += exp.charAt(expIdx);
            expIdx++;
            if(expIdx >= exp.length()) break; 
        }
        tokType = STRING;
    } 
    else { // nieznany znak przerywa analizę 
      token = EOE; 
      return; 
    } 
  }  
    
  // Zwraca true, jeśli c jest przerywnikiem.  
  private boolean isDelim(char c)  
  {  
    if((" +-/*%^=()".indexOf(c) != -1))  
      return true;  
    return false;  
  } 
  //Zwraca true, jeśli c jest początkiem lub końcem Stringa
  private boolean isString(char c){
      if (c == '"')
              return true;
      return false;
  } 
    
} 

0

gdzie jest glowna klasa , gdzie main ??

0

import java.io.*;

class PDemo {
public static void main(String args[])
throws IOException
{
String expr;

BufferedReader br = new 
  BufferedReader(new InputStreamReader(System.in)); 
Parser p = new Parser();  

System.out.println("Wpisz puste wyrażenie, aby zakończyć.");  

for(;;) {  
  System.out.print("Wpisz wyrażenie: ");  
  expr = br.readLine();  
  if(expr.equals("")) break;  
  try { 
    System.out.println("Wynik: " + p.evaluate(expr));  
    System.out.println(); 
  } catch (ParserException exc) { 
    System.out.println(exc); 
  } 
}  

}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1