String Index out of range
Hey, stumped as to why this is wrong. Its the start of a program that will count the number of vowels in a sentance, and I've hit a hurdle. Once I can overcome this the rest of the program should be ok.
Any idea why its wrong?
Code :
import javax.swing.JOptionPane;
public class PracticeString
{
public static void main(String Args[])
{
String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
int UserInputLength = UserInput.length();
int Loop = 0;
int ACount = 0;
int ECount = 0;
int ICount = 0;
int OCount = 0;
int UCount = 0;
char Vowel;
int testing = 0;
while (Loop <= UserInputLength)
{
for (Loop = 0;Loop<=UserInputLength;Loop= Loop + 1)
{
if (UserInput.charAt(Loop)== 'A' )
{
ACount++;
}
}
}
System.out.println("The total number of A's are " +ACount);
}
}
Quote:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:687)
at PracticeString.main(PracticeString.java:22)
Process completed.
Re: String Index out of range
Basically, the last index of the String is the size of the String minus 1. So, you do not want to go while Loop is less than OR EQUAL TO the length of the String, but rather just less than it.
Look at the word String.
The size of String is 6, but the index range of String is 0-5
Also on that note, why do you have the FOR Loop nested in the WHILE loop? That doesn't accomplish anything since the WHILE loop will quit on the condition that the FOR loop is met...
Re: String Index out of range
Quote:
Originally Posted by
aussiemcgr
Basically, the last index of the String is the size of the String minus 1. So, you do not want to go while Loop is less than OR EQUAL TO the length of the String, but rather just less than it.
Look at the word String.
The size of String is 6, but the index range of String is 0-5
Also on that note, why do you have the FOR Loop nested in the WHILE loop, that doesn't accomplish anything since the WHILE loop will quit on the condition that the FOR loop is met...
It made sense in my head. I works, thank you very much for helping me with the problem, you were right and once I just subtracted Loop by 1, it worked. Here is the finished code incase anyone is interested.
Code :
import javax.swing.JOptionPane;
public class PracticeString
{
public static void main(String Args[])
{
String UserInput= JOptionPane.showInputDialog("Enter a sentance: ");
int UserInputLength = UserInput.length();
int Loop = 0;
int ACount = 0;
int ECount = 0;
int ICount = 0;
int OCount = 0;
int UCount = 0;
char Vowel;
int testing = 0;
while (Loop <= UserInputLength)
{
for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
{
if ((UserInput.charAt(Loop-1)== 'A') || (UserInput.charAt(Loop-1)== 'a') )
{
ACount++;
}
}
for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
{
if ((UserInput.charAt(Loop-1)== 'E') || (UserInput.charAt(Loop-1)== 'e') )
{
ECount++;
}
}
for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
{
if ((UserInput.charAt(Loop-1)== 'I')|| (UserInput.charAt(Loop-1)=='i') )
{
ICount++;
}
}
for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
{
if ((UserInput.charAt(Loop-1)== 'O')|| (UserInput.charAt(Loop-1)== 'o') )
{
OCount++;
}
}
for (Loop = 1;Loop<=UserInputLength;Loop= Loop + 1)
{
if ((UserInput.charAt(Loop-1)== 'U') || (UserInput.charAt(Loop-1)== 'u'))
{
UCount++;
}
}
}
System.out.println("The total number of A's are " +ACount);
System.out.println("The total number of E's are " +ECount);
System.out.println("The total number of I's are " +ICount);
System.out.println("The total number of O's are " +OCount);
System.out.println("The total number of U's are " +UCount);
System.out.println("The original sentance was: " +UserInput);
}
}
Re: String Index out of range
For your reference, you can do this a few other ways as well.
One way would be by using a method:
Code java:
import javax.swing.JOptionPane;
public class JunkTesting
{
public static void main(String[] args)
{
String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
//Run method for both uppercase and lower case chars
int ACount = numberOfTimes(UserInput,'a') + numberOfTimes(UserInput,'A');
int ECount = numberOfTimes(UserInput,'e') + numberOfTimes(UserInput,'E');
int ICount = numberOfTimes(UserInput,'i') + numberOfTimes(UserInput,'I');
int OCount = numberOfTimes(UserInput,'o') + numberOfTimes(UserInput,'O');
int UCount = numberOfTimes(UserInput,'u') + numberOfTimes(UserInput,'U');
System.out.println("The total number of A's are " +ACount);
System.out.println("The total number of E's are " +ECount);
System.out.println("The total number of I's are " +ICount);
System.out.println("The total number of O's are " +OCount);
System.out.println("The total number of U's are " +UCount);
System.out.println("The original sentance was: " +UserInput);
}
public static int numberOfTimes(String s,char c)
{
int count = 0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i) == c)
count++;
}
return count;
}
}
Another way would be to use one loop:
Code java:
import javax.swing.JOptionPane;
public class JunkTesting
{
public static void main(String[] args)
{
String UserInput= JOptionPane.showInputDialog("Enter the sentance: ");
//Run method for both uppercase and lower case chars
int ACount = 0;
int ECount = 0;
int ICount = 0;
int OCount = 0;
int UCount = 0;
for(int i=0;i<UserInput.length();i++)
{
if(UserInput.charAt(i)=='a' || UserInput.charAt(i)=='A')
ACount++;
else if(UserInput.charAt(i)=='e' || UserInput.charAt(i)=='E')
ECount++;
else if(UserInput.charAt(i)=='i' || UserInput.charAt(i)=='I')
ICount++;
else if(UserInput.charAt(i)=='o' || UserInput.charAt(i)=='O')
OCount++;
else if(UserInput.charAt(i)=='u' || UserInput.charAt(i)=='U')
UCount++;
}
System.out.println("The total number of A's are " +ACount);
System.out.println("The total number of E's are " +ECount);
System.out.println("The total number of I's are " +ICount);
System.out.println("The total number of O's are " +OCount);
System.out.println("The total number of U's are " +UCount);
System.out.println("The original sentance was: " +UserInput);
}
}
There are tons of other ways as well, those are just to first two that come to mind for me.