0

我知道什么是越界错误,并且我研究了 1516 错误。但是当我尝试修复我的代码时,我都会得到一个错误。我认为这与我的 while 循环无限运行有关,但我无法调试它。

确切的错误

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;
}
}
4

1 回答 1

0

Scanner永远不会在嵌套循环中查询它for以确定它hasNextLine()之前nextLine()是否被调用,如果没有下一行则nextLine()抛出。NoSuchElementException

您需要处理该异常或nextLine()在 中进行保护for,并决定减少预期的行数意味着什么。

数组中的索引顺序错误,导致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++)
    {
        ...
    }
}
于 2012-05-28T20:43:10.083 回答