2012-05-28 42 views
0

我知道什麼是越界錯誤,並且我研究了1516錯誤。但是,當我嘗試修復我的代碼時,無論哪種方式都會遇到錯誤。我認爲這與我的無限循環while循環有關,但我無法調試它。越界數組並找不到線掃描儀:1516錯誤

確切的錯誤,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11 
    at Invest.makeCfPanel(Invest.java:131) 
    at Invest.main(Invest.java:25) 

Exception in thread "main" java.util.NoSuchElementException: No line found 
    at java.util.Scanner.nextLine(Scanner.java:1516) 
    at Invest.makeCfPanel(Invest.java:131) 
    at Invest.main(Invest.java:25) 

這是我的txt PASTEBUCKET.COM/2303

和我的代碼是

import java.util.*; 
import java.io.*; 
import java.awt.*; 
import javax.swing.*; 
import java.awt.event.KeyEvent; 

public class Invest extends JFrame { 

public static void main(String[] args) throws Exception{ 
    JFrame frame = new JFrame(); 
    Dimension minSize = new Dimension(1000,500); 
    frame.setMinimumSize(minSize); 
    frame.setTitle("Financial Calculator | Financial Statment Driven Ratios"); 

    JTabbedPane tabbedPane = new JTabbedPane(); 
    //Income Statement Panel 
    JComponent panel1 = makeIsPanel(); 
    tabbedPane.addTab("Income Statement", panel1); 
    tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); 
    //Balance Sheet Panel 
    JComponent panel2 = makeBsPanel(); 
    tabbedPane.addTab("Balance Sheet", panel2); 
    tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); 
    //Cash Flows Panel 
    JComponent panel3 = makeCfPanel(); 
    tabbedPane.addTab("Cash Flows ", panel3); 
    tabbedPane.setMnemonicAt(2, KeyEvent.VK_3); 

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

    frame.add(tabbedPane); 

    JPanel bottompanel = new JPanel(new FlowLayout()); 
    bottompanel.setBackground(new Color(150, 150, 150)); 
    frame.add(bottompanel, BorderLayout.SOUTH); 

    JButton compute = new JButton("Compute Ratios"); 
    bottompanel.add(compute); 


    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 

protected static JComponent makeIsPanel() { 
     String[] columnNames = {"Entry", "Year", "Year", "Year", "Year"}; 

    Object[][] data = {{"Period Ending:", "2011", "Snowboarding", Integer.valueOf(5), false}, 
     {"Values in Millions of $", "12-31", "Rowing", Integer.valueOf(3), true}, 
     {"Revenue:", "46,542", "Knitting", Integer.valueOf(2), false}, 
     {"Revenue Other:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Revenue:", "46,542", "Speed reading", Integer.valueOf(20), true}, 
     {"Cost of Revenue:", "18,216", "Speed reading", Integer.valueOf(20), true}, 
     {"Gross Profit:", "28,326", "Speed reading", Integer.valueOf(20), true}, 
     {"Selling Gen. Admin Expenses:", "12,111", "Speed reading", Integer.valueOf(20), true}, 
     {"Research & Development:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Depreciation/Amortization:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Operating Expense:", "154", "Speed reading", Integer.valueOf(20), true}, 
     {"Operating Income:", "35,810", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest Income (Exp.), Net NonOp:", "10,732", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest/Invest Inc. Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest Inc. Net Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Gain/Loss on Sale of Assets:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Other, Net:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Net Income Before Taxes:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Income Tax - Total:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Minority Interest:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Equity In Affiliates:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Accounting Change:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Discontinued Operations:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Extraordinary Items:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Net Income:", "Brown", "Pool", Integer.valueOf(10), false}}; 
    JPanel isPanel = new JPanel(); 
    JTable isTable = new JTable(data, columnNames); 
    JScrollPane scrollPane = new JScrollPane(isTable); 
    isPanel.add(scrollPane, BorderLayout.CENTER); 

    return isPanel; 
} 

    protected static JComponent makeBsPanel() { 
    String[] columnNames = {"Entry", "Year", "Year", "Year", "Year"}; 
Object[][] data = {{"Period Ending:", "2011", "Snowboarding", Integer.valueOf(5), false}, 
     {"Values in Millions of $", "12-31", "Rowing", Integer.valueOf(3), true}, 
     {"Cash:", "46,542", "Knitting", Integer.valueOf(2), false}, 
     {"Cash and Cash Equivalents:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Short Term Investments:", "46,542", "Speed reading", Integer.valueOf(20), true}, 
     {"Cash and Short Term Investments:", "18,216", "Speed reading", Integer.valueOf(20), true}, 
     {"Accounts Recievable- Trade, Net:", "28,326", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Receivables, Net:", "12,111", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Inventory:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Prepaid Expenses:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Other Current Assets, Total:", "154", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Current Assets:", "35,810", "Speed reading", Integer.valueOf(20), true}, 
     {"Propert/Plant/Equipment - Gross:", "10,732", "Speed reading", Integer.valueOf(20), true}, 
     {"Accumulated Depreciation,Total:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Property/Plant/Equipment - Net:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Gain/Loss on Sale of Assets:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Other, Net:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Net Income Before Taxes:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Income Tax - Total:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Minority Interest:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Equity In Affiliates:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Accounting Change:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Discontinued Operations:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Extraordinary Items:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Net Income:", "Brown", "Pool", Integer.valueOf(10), false}}; 

    JPanel bsPanel = new JPanel(); 
    JTable bsTable = new JTable(data, columnNames); 
    JScrollPane scrollPane = new JScrollPane(bsTable); 
    bsPanel.add(scrollPane, BorderLayout.CENTER); 

    return bsPanel; 
} 
    protected static JComponent makeCfPanel() throws IOException { 
    String[] columnNames = {"Entry","Year","Year", "Year", "Year", "Year","Year","Year", "Year", "Year", "Year"}; 

    File f = new File ("data/IS.txt"); 
    Scanner input = new Scanner(f); 


     Object [][] fs = new String[53][11]; 

      while (input.hasNextLine()) 
      { 
       for (int col=0;col<53;col++){ 
        for (int row=0;row<11;row++){ 
         fs[row][col] = input.nextLine(); 
        // System.out.printf("%s\t",fs[row][col]); 
        } 
       } 
       if (input.hasNextLine()) 
        input.nextLine(); 

      } 


    /*Object[][] data = {{"Period Ending:", "2011", "Snowboarding", Integer.valueOf(5), false}, 
     {"Values in Millions of $", "12-31", "Rowing", Integer.valueOf(3), true}, 
     {"Revenue:", "46,542", "Knitting", Integer.valueOf(2), false}, 
     {"Revenue Other:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Revenue:", "46,542", "Speed reading", Integer.valueOf(20), true}, 
     {"Cost of Revenue:", "18,216", "Speed reading", Integer.valueOf(20), true}, 
     {"Gross Profit:", "28,326", "Speed reading", Integer.valueOf(20), true}, 
     {"Selling Gen. Admin Expenses:", "12,111", "Speed reading", Integer.valueOf(20), true}, 
     {"Research & Development:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Depreciation/Amortization:", "-", "Speed reading", Integer.valueOf(20), true}, 
     {"Total Operating Expense:", "154", "Speed reading", Integer.valueOf(20), true}, 
     {"Operating Income:", "35,810", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest Income (Exp.), Net NonOp:", "10,732", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest/Invest Inc. Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Interest Inc. Net Non-Operating:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Gain/Loss on Sale of Assets:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Other, Net:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Net Income Before Taxes:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Income Tax - Total:", "White", "Speed reading", Integer.valueOf(20), true}, 
     {"Minority Interest:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Equity In Affiliates:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Accounting Change:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Discontinued Operations:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Extraordinary Items:", "Brown", "Pool", Integer.valueOf(10), false}, 
     {"Net Income:", "Brown", "Pool", Integer.valueOf(10), false}};*/ 
    JPanel cfPanel = new JPanel(); 
    JTable cfTable = new JTable(fs, columnNames); 
    JScrollPane scrollPane = new JScrollPane(cfTable); 
    cfPanel.add(scrollPane, BorderLayout.CENTER); 

    return cfPanel; 
} 
} 

回答

1

Scanner從未嵌套循環for以確定是否內查詢它hasNextLine()之前nextLine()被稱爲和nextLine()拋出NoSuchElementException如果t這裏沒有下一行。

您需要處理該例外情況,或在for範圍內保護nextLine(),並確定這意味着預計的行數會減少。

進入數組的索引的順序錯誤,導致ArrayIndexOutOfBoundsException。更改爲:

fs[col][row] = input.nextLine(); 

考慮查詢它的尺寸的陣列中的for環路而不是重複的硬編碼值:

for (int col = 0, col_size = fs.length; col < col_size; col++) 
{ 
    for (int row = 0, row_size = fs[col].length; row < row_size; row++) 
    { 
     ... 
    } 
} 
+0

喂,由於。當你說保護時,我不太關注。我可以看到我如何將它複製到另一個變量中......這樣保護它嗎?但似乎並沒有太大的成就。 – user1093111

+0

保護我的意思是在'for'裏面'input.nextLine()'之前有'input.hasNextLine()'。如果沒有下一行,那麼你需要決定它對應用程序意味着什麼:失敗或者是適當的默認值,可以用來填充當前未設置的數組元素? – hmjd

+0

是有意義的。所以我在嵌套循環之前放置了一條if語句,以及a和else關閉循環。 – user1093111