Results 1 to 3 of 3
  1. #1
    tibbyuk is offline Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    2
    Rep Power
    0

    Default 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!

    Java 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
    Java 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
    Last edited by tibbyuk; 08-08-2008 at 11:42 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    an error being outputted saying the indexisoutofbounds(0).
    When you get errors, please copy and paste all the the text here. The error message says what line the error occured on and what the bad index value was.

  3. #3
    tibbyuk is offline Member
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Confusing with Frameworks
    By hisouka in forum New To Java
    Replies: 0
    Last Post: 08-05-2008, 05:52 AM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •