2014-12-13 27 views
-1

我正在開發一個程序來演示GUI中的數組,但我遇到了一些障礙。我無法得到正確的解析,我有兩個for循環,似乎有他們的變量有問題。由於我不確定這個障礙在哪裏,所以我包含了整個程序。沒有太多它,它是一個非常小的應用程序。爲什麼我的解析和for循環無法在java中工作?

解析問題在第83和84行;兩個for循環在90-97行。

import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Text; 
import org.eclipse.swt.widgets.Button; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 


public class Monthly_Budget_Calculator { 

    protected Shell shell; 
    private Text txtIntegerDynamic; 
    private Text txtStringDynamic; 

    private int maxSize = 20; 
    private String[] stackArrayString = new String[maxSize]; 
    private int[] stackArrayInt = new int[maxSize]; 
    private int top = -1; 

    public void intpush(int i) { 
     stackArrayInt[++top] = i; 
    } 

    /** 
    * Launch the application. 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try { 
      Monthly_Budget_Calculator window = new Monthly_Budget_Calculator(); 
      window.open(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    /** 
    * Open the window. 
    */ 
    public void open() { 
     Display display = Display.getDefault(); 
     createContents(); 
     shell.open(); 
     shell.layout(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
    } 

    /** 
    * Create contents of the window. 
    */ 

    protected void createContents() { 

     shell = new Shell(); 
     shell.setSize(450, 750); 
     shell.setText("Monthly Budget Calculator"); 

     Label lblStringStatic = new Label(shell, SWT.NONE); 
     lblStringStatic.setBounds(10, 98, 145, 15); 
     lblStringStatic.setText("Enter a string value here:"); 

     txtStringDynamic = new Text(shell, SWT.BORDER); 
     txtStringDynamic.setBounds(10, 119, 182, 21); 

     Label lblIntegerStatic = new Label(shell, SWT.NONE); 
     lblIntegerStatic.setBounds(198, 98, 145, 15); 
     lblIntegerStatic.setText("Enter a decimal value here:"); 

     txtIntegerDynamic = new Text(shell, SWT.BORDER); 
     txtIntegerDynamic.setBounds(198, 119, 145, 21); 

     Button btnAdd = new Button(shell, SWT.NONE); 
     btnAdd.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 
       // adds values to arrays 
       // I'M TRYING TO PARSE THESE TEXT FIELDS INTO THE ARRAY, I JUST HAVEN"T BEEN ABLE TO GET THESE TWO LINES WORKING 
       stackArrayInt.push(Integer.parseInt(txtIntegerDynamic.getText())); 
       stackArrayString.push(txtStringDynamic.getText())); 
       // clears fields 
       txtStringDynamic.setText(""); 
       txtIntegerDynamic.setText(""); 
       // prints to fields 
       String valueToBeInserted=""; 
       // THESE FOR LOOPS ARE HAVING TROUBLE WITH THE I VARIABLE, NOT SURE WHY 
       for (i = 0; i<stackArrayString.length; i++) 
       { 
        valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayString[i]; 
       } 
       for (i = 0; i<stackArrayInt.length; i++) 
       { 
        valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayInt[i]; 
       } 
      } 
     }); 
     btnAdd.setBounds(349, 117, 75, 25); 
     btnAdd.setText("Add"); 

     Label lblExplanation = new Label(shell, SWT.NONE); 
     lblExplanation.setAlignment(SWT.CENTER); 
     lblExplanation.setBounds(10, 10, 414, 82); 
     lblExplanation.setText("This calculator is for budgeting monthly expenses. \r\nAdd the string value descriptor of the expense on the left. \r\nAdd the decimal value descriptor of the expense on the right.\r\nClick \"Add\" to add the expense to the list.\r\nClick \"Clear\" to clear all expenses from the list and start over."); 

     Label lblListStatic = new Label(shell, SWT.NONE); 
     lblListStatic.setBounds(110, 161, 210, 15); 
     lblListStatic.setText("The current expenses accounted for are:"); 

     final Label lblListString = new Label(shell, SWT.BORDER); 
     lblListString.setBounds(60, 182, 203, 450); 

     final Label lblListInteger = new Label(shell, SWT.BORDER); 
     lblListInteger.setBounds(265, 182, 104, 450); 

     Label lblBudgetStatic = new Label(shell, SWT.NONE); 
     lblBudgetStatic.setBounds(20, 654, 182, 15); 
     lblBudgetStatic.setText("Your monthly budget thus far is:"); 

     Label lblBudgetDynamic = new Label(shell, SWT.NONE); 
     lblBudgetDynamic.setBounds(30, 675, 104, 15); 

     Button btnClear = new Button(shell, SWT.NONE); 
     btnClear.addSelectionListener(new SelectionAdapter() { 
      @Override 
      public void widgetSelected(SelectionEvent e) { 
       // empties arrays 
       stackArrayInt = null; 
       stackArrayInt = null; 
       // empties fields and labels 
       txtStringDynamic.setText(""); 
       txtIntegerDynamic.setText(""); 
       lblListString.setText(""); // made field final 
       lblListInteger.setText(""); // made field final 

      } 
     }); 
     btnClear.setBounds(289, 665, 75, 25); 
     btnClear.setText("Clear"); 

     Label divOne = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
     divOne.setBounds(110, 90, 210, 2); 

     Label divTwo = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
     divTwo.setBounds(110, 153, 210, 2); 

     Label divThree = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL); 
     divThree.setBounds(110, 646, 210, 2); 

    } 
} 
+0

[tag:swing]標記已移除,已添加[tag:swt]標記。你的問題與Swing毫無關係,並且混淆了你的問題,你正在吸引錯誤的專家。 – 2014-12-13 03:16:27

+0

非常感謝。我是新來的Java。 – 2014-12-13 03:17:58

+0

您可能不知道代碼中的問題所在,但應描述您正在觀察的內容以及與預期內容的不同之處。否則沒人能幫忙。 – 2014-12-13 03:20:45

回答

0

第一for循環之前在單獨的行添加int i;

在Java中,您需要先聲明一個變量及其類型,然後才能使用它。

  int i; // <---- HERE 
      for (i = 0; i<stackArrayString.length; i++) 
      { 
       valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayString[i]; 
      } 
      for (i = 0; i<stackArrayInt.length; i++) 
      { 
       valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayInt[i]; 
      } 
0

您必須初始化i第一:

for (int i = 0; i<stackArrayString.length; i++) 
{ 
    valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayString[i]; 
} 
for (int i = 0; i<stackArrayInt.length; i++) 
{ 
    valueToBeInserted = valueToBeInserted + "\r\n" + stackArrayInt[i]; 
} 
0

的問題,您的解析器的是,在簡單的Java數組沒有像你試圖調用的那些推方法。如果你需要一個堆棧數據結構的推/彈出功能,你最好去一個deque這樣的:

Deque<Integer> stack = new ArrayDeque<Integer>(); 
    Deque<String> stack2 = new ArrayDeque<String>(); 
    //... 
    stack.push(123); 
    stack.push("abc"); 

i變量的問題很簡單,它並沒有在任何地方宣佈你代碼,正如其他答案中所述。

相關問題