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)

これは私のテキストです 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ことはなく、次の行がない場合はスローされます。forhasNextLine()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 に答える