2012-05-28 23 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