Confusing IndexOutOfBounds Error?
Hey guys,
I'm looking for a tad bit of help on something.
I have written a program that uses a menu system, using a switch menu with cases.
Now everything is working fine as I want it, but when I go through some of the menu items, it returns to the menu with an error being outputted saying the indexisoutofbounds(0).
Now I know this means that it is reading a blank input or something. But I haven't had the chance to input anything blank, and I the only difference I can see between the methods that don't cause it, and the ones that do is that the ones that don't close a resultset at the end of them.
The code is here, I'm hoping I am being an idiot and it is really obvious!
Code:
import java.util.*;
import java.io.*;
import java.sql.*;
import stockBean.*;
public class StockProgram
{
private static Scanner input = new Scanner(System.in);
private static Connection link = null;
private static ResultSet results = null;
public static void main(String[] args)throws IOException, SQLException, ClassNotFoundException
{
String choice;
char option;
do
{
displayMenu();
choice = input.nextLine();
option = choice.charAt(0);
switch (option) //Menu Choices
{
case '1': addStock(); //Enter a result
break;
case '2': calculateCost(); //Display a match result
break;
case '3': showAllStock(); //Display the season's results
break;
case '4': stockLevels(); //Calculate the cost of tickets
break;
case '5': //quit
break;
default: System.out.println("\t Sorry, That Is Not A Valid Option, Try Again");
}
}while (option != '5');
}
public static void displayMenu() //Displays Menu
{
System.out.println("---Menu---");
System.out.println("1. Add New Stock");
System.out.println("2. Calculate Order");
System.out.println("3. Show All Stock");
System.out.println("4. Retrieve Stock Levels");
System.out.println("5. Quit");
}
public static void addStock() throws ClassNotFoundException //Add Stock To Database
, SQLException
{
System.out.println("Option 1 Selected");
StockAccess stock = new StockAccess();
System.out.println("Stock Code?: ");
String stockCode = input.nextLine();
System.out.println("Stock Description?: ");
String description = input.nextLine();
System.out.println("Current Level?: ");
int currentLevel = input.nextInt();
System.out.println("Re-Order Level?: ");
int reorderLevel = input.nextInt();
System.out.println("Cost?: ");
float cost = input.nextFloat();
stock.setStuff(stockCode, description, currentLevel, reorderLevel, cost);
String complete = stock.addStock();
System.out.print(complete);
}
public static void stockLevels() throws ClassNotFoundException //Shows Single Result
, SQLException
{
System.out.println("Option 4 Selected");
StockAccess stock = new StockAccess();
System.out.println("Stock Code?: ");
String stockCode = input.nextLine();
results = stock.checkLevel(stockCode);
System.out.println();
try
{
if (results.next())
{
String stockCodeFinal = results.getString(1);
String desciption = results.getString(2);
int currentLevel = results.getInt(3);
int reorderLevel = results.getInt(4);
float price = results.getFloat(5);
System.out.print("\t\t" + stockCodeFinal);
System.out.print("\t\t" + desciption);
System.out.print("\t\t" + currentLevel);
System.out.print("\t\t" + reorderLevel);
System.out.print("\t\t" + price);
}
else
{
System.out.println("*No Result Found*");
}
System.out.println();
}
catch(SQLException sqlEx)
{
System.out.println("* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
}
results.close();
}
public static void showAllStock() throws ClassNotFoundException //Display All Stock
, SQLException
{
OrderCost stockOrder = new OrderCost();
results = stockOrder.showAll();
try
{
System.out.println();
System.out.println("\n\t\tStockCode\tDescription\tCurrent Level\tRe-Order Level\tPrice");
System.out.println("\t\t_________\t___________\t_____________\t______________\t_____\n");
while (results.next())
{
System.out.print("\t\t" + results.getString(1));
System.out.print("\t\t" + results.getString(2));
System.out.print("\t\t" + results.getInt(3));
System.out.print("\t\t" + results.getInt(4));
System.out.println("\t\t£" + String.format("%.2f",results.getFloat(5)));
}
System.out.println();
results.close();
}
catch(SQLException sqlEx)
{
System.out.println("* Error retrieving data! *");
sqlEx.printStackTrace();
System.exit(1);
}
}
public static void calculateCost() throws SQLException, ClassNotFoundException //Calculate Cost
{
OrderCost stock = new OrderCost();
System.out.println("Stock Code?: ");
String stockCode = input.nextLine();
stock.setStockCode(stockCode);
System.out.println("Quantity: ");
int quantity = input.nextInt();
stock.setQuantity(quantity);
float cost = stock.getCost();
if(cost == 0)
{
System.out.print("Sorry Incorrect/No Code");
}
else
{
System.out.println("£"+String.format("%.2f",cost));
}
}
}
The Error Is
Code:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
at StockProgram.main(StockProgram.java:28)
Cheers for any help guys!
- Tommy