我知道出界的错误是什么,我研究了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;
}
}
答案 0 :(得分:1)
在嵌套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++)
{
...
}
}
循环中查询数组的维度,而不是重复硬编码值:
{{1}}