Results 1 to 6 of 6
Thread: String and char arrays
- 05-08-2013, 04:27 AM #1
Member
- Join Date
- Apr 2013
- Posts
- 41
- Rep Power
- 0
String and char arrays
I really dont know why this isnt working, maybe someone could shed some light on it for me; as i dont use chars very often nor do i relate strings to each other often.
Java Code:for (int i = 0; i < 5; i++){ if (s.equals(perfectNames[i])){ score = 10; break; } if(s.equals(worstNames[i])){ score = 0; break; } } for(int i = 1; i < s.length(); i++){ letter = s.charAt(i); for(int a = 0; a <= vowels.length; a++){ if(letter == vowels[a]){ score++; } } for(int b = 0; b <= startingL.length; b++){ if(letter == startingL[b]){ score++; } } } }
Java Code:Enter any name: rob Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at NameTest.grading(NameTest.java:32) at NameTest.main(NameTest.java:17)
I would have thought this code is right; there is no errors until you type a name in.
oh and this error:
Java Code:at NameTest.grading(NameTest.java:32)
Java Code:if (s.equals(perfectNames[i])){
Java Code:if(letter == vowels[a]){
- 05-08-2013, 04:47 AM #2
Senior Member
- Join Date
- Nov 2012
- Posts
- 257
- Rep Power
- 9
Re: String and char arrays
its telling you that you are trying to add into the array at an index that doesn't exist.
try this:
Replace: references to the arrayLength so:
startingL.length; would become startingL.length -1;
vowels.length; would become vowels.length -1;
Give that a whirl.
- 05-08-2013, 05:12 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: String and char arrays
Just change your relational operator from <= to <. Also, do you really want to begin for loop @ line 12 with i = 1? I can't tell with the posted code.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 05-08-2013, 05:40 AM #4
Member
- Join Date
- Apr 2013
- Posts
- 41
- Rep Power
- 0
Re: String and char arrays
Thank you that fixed the problem for that part. another issue if you can help:
Java Code:for (int i = 0; i < 5; i++){ if (s.equals(perfectNames[i])){ score = 10; break; } if(s.equals(worstNames[i])){ score = 0; break; } }
if it is one of the perfectNames or worstnames it works but if its not i get an error with this line:
Java Code:if (s.equals(perfectNames[i])){
Java Code:if(i == 4){ if(!s.equals(worstNames[i])){ break; } }
- 05-08-2013, 05:42 AM #5
Member
- Join Date
- Apr 2013
- Posts
- 41
- Rep Power
- 0
- 05-08-2013, 06:01 AM #6
Member
- Join Date
- Apr 2013
- Posts
- 41
- Rep Power
- 0
Similar Threads
-
Need help passing Scanner objects into char arrays
By Terminus_Est in forum New To JavaReplies: 1Last Post: 03-27-2012, 03:28 AM -
Help with char and string
By mehnihma in forum New To JavaReplies: 13Last Post: 11-02-2011, 12:06 AM -
check a string char by char
By Sotsiak in forum New To JavaReplies: 2Last Post: 10-23-2010, 10:24 PM -
Comparing two Char arrays
By viperlasson in forum New To JavaReplies: 3Last Post: 01-30-2010, 09:05 AM -
char to string
By kian_hong2000 in forum New To JavaReplies: 2Last Post: 08-25-2008, 02:51 PM
Bookmarks