charAT Throws StringIndexOutOfBoundsException.
The program works fine until the user has finished entering the Swell Heights then it returns to the menu and then Throws this silly exception.
=(
import java.util.Scanner;//import the Scanner Class
import java.lang.reflect.Array;//import the Array Class
/***************************************************
@Program Name: SwellHeights.java
@Purpose: To get swell information and the display
proccessed information about the swells.
***************************************************/
//Start of the Program
publicclass SwellHeights
{
//creates a Global Array so that the functions can get access to it
publicstaticfloat[] arraySwellHeight =newfloat[7];
//Start of the MAIN function
publicstaticvoid main(String args[])
{
float day = 0;//Init var to get data from dayLargeSwell()
float smallest = 0;//Init var to get data from smallSwell()
float largest = 0;//Init var to get data from largeSwell()
float average = 0;//Init var to get data from averageSwell()
Scanner in =new Scanner (System.in);//Creates a scanner to capture input
char choice;//declare var for use in Case Statement
do//Starts do while loop
{
//Program heading
System.out.println("SWELL HEIGHT ANALYSIS PROGRAM");
System.out.println("*****************************");
System.out.println("");
System.out.println("a)Enter maximum daily swell heights");
System.out.println("b)Analyse swell height figures");
System.out.println("c)Exit program");
System.out.println("");
System.out.println("Enter Option a, b or c");
System.out.println("Please use lowercase for your answer.");
System.out.println("");
String s = in.nextLine();//Sets user input as s
choice = s.charAt(0);//charAt only reads the 1st char in s
//Switch statment to define where the program will go to
switch(choice)
{
//If a in input form user it runs this section
case'a' :
{
int count;//declares a counter to use in for loop
System.out.println("");
System.out.println("ENTER MAXIMUM SWELL HEIGHTS FOR THE LAST 7 DAYS");
System.out.println("===============================================");
System.out.println("Note: Input swell height figures in metres");
System.out.println("");
System.out.println("Enter 7 daily Swell Hieghts\n");
for (count = 0; count < 7; count++)
//This loop will run 7 times
{
System.out.print(" Enter swell heights for day " + (count+1)+" $: ");
SwellHeights.arraySwellHeight [count] = in.nextFloat();
System.out.println(SwellHeights.arraySwellHeight [count]);
//This is creating an Array with 7 elements
}
System.out.println("");
}
//Stops the program from carrying on to case b
break;
//If b is input from user it runs this section
case'b' :
System.out.println("");
System.out.println("MAXIMUN DAILY SWELL HEIGHTS FOR THE WEEK");
System.out.println("****************************************");
System.out.println("");
System.out.println("Day Metres");
System.out.println("");
// + SwellHeights.arraySwellHeight[#] displays what is inside that element
System.out.println("1 " + SwellHeights.arraySwellHeight[0]);
System.out.println("2 " + SwellHeights.arraySwellHeight[1]);
System.out.println("3 " + SwellHeights.arraySwellHeight[2]);
System.out.println("4 " + SwellHeights.arraySwellHeight[3]);
System.out.println("5 " + SwellHeights.arraySwellHeight[4]);
System.out.println("6 " + SwellHeights.arraySwellHeight[5]);
System.out.println("7 " + SwellHeights.arraySwellHeight[6]);
System.out.println("");
System.out.println("============================================================================");
System.out.print("Average swell height: " );
average = averageSwell();//calls the averageSwell() function
System.out.println(average);//prints out the result of that functions process
System.out.print("Smallest swell height: " );
smallest = smallSwell();//calles the smallSwell() function
System.out.println(smallest);//prints out the result of that funtions process
System.out.print("Largest swell height: " );
largest = largeSwell();//calles the largeSwell() function
System.out.print(largest);//prints out the result of the functions process
System.out.print(" metres occured on day ");
day = dayLargeSwell();//calles the daylargeSwell() function
System.out.println(day);//prints out the result of that functions process
System.out.println("============================================================================");
System.out.println("");
//Stops the program from carrying on to case c
break;
//If c in input form user it runs this section
case'c' :
System.out.println("");
System.out.println("Thank you and Goodbye");
//Stops the program from carrying on to default
break;
default :
System.out.println(choice+" Is An Invalid Selection" );
System.out.println("");
//Stops the program from exiting the case statement
break;
//End of Case Statement
}
}
//End of Do-While Loop, when user enters c
while(choice !='c');
//End of main()
}
/***************************************
Purpose: To calculate the average swell
height from the values in the
Array.
@return: Returns the average to be
displayed in main()
***************************************/
publicstaticfloat averageSwell()
{
int count = 0;//Init var for Do-While Loop
float total = 0.0f;//Init var for the total
float average = 0.0f;//Init var to hold the average
//Start of Do-While loop
do
{
//Accumulate Total
total = total + SwellHeights.arraySwellHeight[count];
count = count + 1;
}
//Finishes when count is equal to 7
while (count < 7);
//Average is total / number of elements
average = total/count;
//Returns the average var to main()
return average;
}//end of averageSwell()
/**************************************
Purpose: To calculate the minimum swell
height from the values in the
Array.
@return: Returns the minimum to be
displayed in main()
**************************************/
publicstaticfloat smallSwell()
{
int count = 0;//Init var for Do-While Loop
float minimum;// Declare float to hold minimum
//minimum is then assigned the first element
minimum = SwellHeights.arraySwellHeight[0];
//Start of Do-While loop
do
{
//Check for new minimum
if (SwellHeights.arraySwellHeight[count] < minimum)
{
//New minimum
minimum = SwellHeights.arraySwellHeight[count];
}
//Incrementing count
count = count + 1;
}
//Finishes when count is equal to 7
while (count < 7);
//Returns the minimum var to main()
return minimum;
}//End of smallSwell()
/**************************************
Purpose: To calculate the maximum swell
height from the values in the
Array.
@return: Returns the maximum to be
displayed in main()
**************************************/
publicstaticfloat largeSwell()
{
int count = 0;//Init var for Do-While Loop
float maximum;// Declare float to hold maximum
//maximum is then assigned the first element
maximum = SwellHeights.arraySwellHeight[0];
//Start of Do-While loop
do
{
//Check for new maximum
if (SwellHeights.arraySwellHeight[count] > maximum)
{
//New maximum
maximum = SwellHeights.arraySwellHeight[count];
}
//Incrementing count
count = count + 1;
}
//Finishes when count is equal to 7
while (count < 7);
//Returns the maximum var to main()
return maximum;
}//End of largeSwell()
/**************************************
Purpose: To calculate the day of the
largest swell height from the
values in the Array.
@return: Returns the day of the largest
swell to be displayed in main()
**************************************/
publicstaticfloat dayLargeSwell()
{
int count = 0;//Init var for Do-While Loop
int day = 0;//Init var to hold days
float maximum;//Declare float to hold maximum
//maximum is then assigned the first element
maximum = SwellHeights.arraySwellHeight[0];
//Start of Do-While loop
do
{
//Check for new maximum
if (SwellHeights.arraySwellHeight[count] > maximum)
{
//New maximum
maximum = SwellHeights.arraySwellHeight[count];
//Maximum element number caputured
day = count + 1;
}
//Incrementing count
count = count + 1;
}
//Finishes when count is equal to 7
while (count < 7);
//Returns the day var to main()
return day;
}//End of dayLargeSwell
}//End of Program

