2016-11-09 87 views
0

我在Net-beans JFrame中製作一個計算器,並使用堆棧來幫助計算輸入的變量。我似乎遇到了這個錯誤StringIndexOutOfBounds:0,我似乎無法弄清楚如何解決它發生時。每當我按下啓動堆棧的相等按鈕,彈出錯誤。我認爲它的錯誤與我的堆棧,但我再也找不出來。我真的需要一些新鮮的眼光。Java JFrame字符串索引超出界限錯誤

我使用/進口的鞦韆和.awts,但我不認爲他們給我的錯誤這裏是我的波動。

import java.awt.Color; 
    import java.awt.Font; 
    import java.awt.Graphics2D; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.io.PrintWriter; 
    import static java.lang.Math.round; 
    import java.util.NoSuchElementException; 
    import java.util.Scanner; 
    import javax.swing.JFileChooser; 

這裏是我的堆棧:

public class StackCalc 
{ 
private LinkedList stackList = new LinkedList(); 
private int top, maxTop; 
public Object removedEle; 
public Object topEle; 

public StackCalc(int mt) 
{ 
    maxTop=mt; 
    top=-1; 
} 
public boolean isFull() 
{ 
    return top == maxTop-1; 
} 
public boolean push (Object O) 
{ 
    if(!isFull()) 
    { 
     stackList.addFirst(O); 
     top++; 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
public boolean pop() 
{ 
    if(!stackList.isEmpty()) 
    { 
     removedEle= stackList.removeFirst(); 
     top--; 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
public void getTop() 
{ 
    topEle=stackList.getFirst(); 
} 
public boolean isEmpty() 
{ 
    return stackList.isEmpty(); 
} 

} 

這裏是我覺得是給我這個錯誤

static void processExpR(String exp) 
    { 
     boolean advance = true; 
     String token = " "; 
     int loc = exp.indexOf(token); 
     while (loc != -1) 
     { 
      if (token.isEmpty()){ 
       return; 
      } 
       else if (advance){ 
       token = exp.substring(0,loc); 
       exp = exp.substring(loc+1); 
      } 

      char ch = token.charAt(0);//there is a specific problem with this line 
      if(Character.isDigit(ch)){ 
       advance = true; 
       s1R.push(token); 
      } 
      else 
      { 
       if(s2R.isEmpty()) 
       { 
        advance = true; 
        s2R.push(token); 
       } 
       else 
       { 
        advance = false; 
        calcR(); 
       } 
      } 
      if(advance){ 
       loc = exp.indexOf(" "); 
      } 
     }//end of while 
     if (Character.isDigit(exp.charAt(0))) 
     { 
      s1R.push(exp); 
     } 
     else 
     { 
      s2R.push(exp); 
     } 
     while (!s2R.isEmpty()) 
     { 
     calcR(); 
     } 
    } 

任何幫助將是非常讚賞的代碼。我好像真的迷失在這裏。謝謝。

+0

加入整個堆棧跟蹤到你的帖子。 –

回答

1

,問題就出現在這裏:

token = exp.substring(0,loc); 

上面一行需要一個子從EXP。過了一會兒,你這樣做:

char ch = token.charAt(0);//there is a specific problem with this line 

而發生的事情是:你從EXP削減和存儲到令牌字符串...是。因此,當您嘗試訪問該字符串的索引0時,您會被告知:該字符串甚至沒有索引0(並且只有在令牌爲空!時纔會發生這種情況!)。

因此,這裏的答案是兩個方面:

  1. 做這樣的charAt(someIndex)調用之前你最好檢查如果前期你的字符串實際上該指數
  2. 但在你的情況,單獨檢查將無濟於事。

你看,你的問題基本上是你的邏輯如何計算你的「子串」索引可能是錯誤的。

我的建議:坐下來,手動處理您的輸入數據。含義:使用示例輸入,並手動執行你的程序。爲了理解你的計數器/索引變量的樣子,瞭解你的代碼是如何處理它的輸入的。如果你發現太麻煩了,至少要學習使用調試器來做到這一點(但是做到這一點,手動兩次仍然會告訴你在這裏有超過50或100個答案!)

+0

謝謝GhostCat,我會試試這個 –