Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default 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++;
    				}
    			}
    		}
    		}
    my error:
    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)
    So what im trying to make is a name test; you write a name and it tells you how good of a name it is.
    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)
    refers to this line of code:
    Java Code:
    if (s.equals(perfectNames[i])){
    but its also given me errors when i comment that code out at this part:
    Java Code:
    if(letter == vowels[a]){
    any ideas?

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    3

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

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,908
    Rep Power
    5

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: String and char arrays

    Quote Originally Posted by monkeyjr97 View Post
    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.
    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;
    			}
    		}
    its not breaking out of this loop for some reason ..
    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])){
    tried adding this to make it break out but it doesnt work for some reason
    Java Code:
    			if(i == 4){
    				if(!s.equals(worstNames[i])){
    					break;
    				}
    			}
    THANKS

  5. #5
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: String and char arrays

    Quote Originally Posted by jim829 View Post
    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,
    Jim
    ah yes both your answers are correct thank you, and no thats just part of one of my methods; sorry for the confusion

  6. #6
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: String and char arrays

    Nevermind fixed the problem. thanks guys.

Similar Threads

  1. Need help passing Scanner objects into char arrays
    By Terminus_Est in forum New To Java
    Replies: 1
    Last Post: 03-27-2012, 03:28 AM
  2. Help with char and string
    By mehnihma in forum New To Java
    Replies: 13
    Last Post: 11-02-2011, 12:06 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 10:24 PM
  4. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 09:05 AM
  5. char to string
    By kian_hong2000 in forum New To Java
    Replies: 2
    Last Post: 08-25-2008, 02:51 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
  •