Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Norm

Thread: Help String Arrays

  1. #1
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Help String Arrays

    Hello,
    I have been working on a new bit of code recently that is made to find how many words there are in a sentence. The only problem is that I keep on getting an error when trying to read an array. The error message is:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at AI.Input.get(Input.java:42)
    at AI.Input.runInput(Input.java:26)
    at AI.Run.main(Run.java:26)
    Java Result: 1

    And here is the code:
    Java Code:
    import java.util.*;
    /**
     * @author Zypher
     * Gets the sentence from the computer and splits it up into words for the 
     * computer to process
     */
    public class Input {
            String[] wordArray;
            String word;
            int wordnum = 0;
            int wordletternum = 0;
            String Sentence;
            int totalletter = 0;
            boolean next;
            int wordcount = 0;
        ////////////////////////////////////////////////////////////////////////////
        public void runInput(){      //this is for the run code
            get();
        }
        ////////////////////////////////////////////////////////////////////////////
        public void get(){
            Scanner input = new Scanner(System.in);
            String Sentence = input.nextLine();
            totalletter = Sentence.length();
            //stores the words in the array
            String[] part = Sentence.split(" "); 
    
    
            count();
            //splits the "parts" to find how many words there are
            while(next == true){
                //this is where the issue is!! it does not compute the words after part
                //0, for some reason it does not want to set anything after the first part..
                word = part[wordcount];
                wordletternum = word.length();
                wordcount++;
                count();
            }
            System.out.println(wordcount);
    
        }    
        ////////////////////////////////////////////////////////////////////////////
        //figures out how many words are in the sentence
        public void count(){
            totalletter = totalletter - wordletternum;
            if(totalletter > 0){
                   next = true;
               }else{
                   next = false;
               }
        }
        ////////////////////////////////////////////////////////////////////////////
    }
    Any suggestions would be greatly appreciated
    -Zypher

  2. #2
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help String Arrays

    And I just noticed that line 42 in the error is line 34 in the code...

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help String Arrays

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at AI.Input.get(Input.java:42)
    At line 42 the code used an index past the end of the array which didn't have 2 elements (1 is index for second element)

    Look at line 42 and see why it used an index value past the end of the array.
    Why does the code use a while() loop to go through an array? Normally that is done with a for loop.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Zypher is offline Member
    Join Date
    Feb 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help String Arrays

    I found out what the issue was but now I feel stupid. I was not calculating for the spaces that where lost in the split(" ") statement allowing for it to continue looking for words when there were not any more left.
    In line 45 it needed to be changed to:
    Java Code:
    totalletter = totalletter - wordletternum - 1;
    Thanks for the quick response!
    -Zypher

  5. #5
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    448
    Rep Power
    1

    Default Re: Help String Arrays

    I am just wondering why you do such calculations.

    You get an array and want to go through the array? Nothing simpler than this:
    Java Code:
    		String[] parts = Sentence.split(" ");
    		for (int i = 0; i<parts.length; i++) {
    			// Do something with parts[i]
    		}
    		for (String part: parts) {
    			// Do something with part
    		}
    Your code is really unreadable:
    - If you have a loop then you should not hide the modification of the loop trigger in some method.
    - Try to keep variables local if they are not really required in a class.
    - Try to use names for variables that simple mean something. As an example: I would use something like lettersToProcess instead of totalletter - the number of letters is not changed. You just have to process more.

    With kind regards,

    Konrad

  6. #6
    XeRoFlaWz is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help String Arrays

    Well, a while loop is more suitable in terms of searching an array for a few reasons in my opinion Norm. The most important being, if you're searching for one element, an early exit. If you have an array of 1000 students and you're looking for one, why keep looping through them all when you found the one you want? :)

  7. #7
    XeRoFlaWz is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help String Arrays

    Also, Zypher. You should
    Java Code:
    import java.util.Scanner;
    There's no need to import all of the utility, only what you need.

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

    Default Re: Help String Arrays

    A for loop is probably more convenient. Especially using the for(:) construct. And you can break out of a for loop just as easy as a while loop after you find the item you are looking for.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help String Arrays

    @XeRoFlaWz
    I suppose part of it is personal preferences. A for loop has a natural structure for looking at items in a list. Its looping can be exited the same way as a while loop. The handing of the index to the list is much easier and understandable in a for loop than the extra code that has to be added to a while loop. Using a while loop is unnatural and can cause confusion.
    Last edited by Norm; 04-26-2014 at 02:22 AM.
    XeRoFlaWz likes this.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    XeRoFlaWz is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Default Re: Help String Arrays

    I suppose at the end of the day it all comes down to preference.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Help String Arrays

    Unless other programmers are going to be maintaining it. Then the standard way would be preferred.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help with String arrays
    By maddiexc in forum New To Java
    Replies: 5
    Last Post: 09-13-2013, 02:30 AM
  2. String and char arrays
    By leeroijenkiins in forum New To Java
    Replies: 5
    Last Post: 05-08-2013, 05:01 AM
  3. string arrays and copying
    By ztomtomz in forum New To Java
    Replies: 1
    Last Post: 11-18-2012, 05:26 AM
  4. How do you use an arraylist of string arrays
    By uhertz in forum New To Java
    Replies: 1
    Last Post: 01-16-2012, 09:38 AM
  5. String arrays
    By myka in forum New To Java
    Replies: 1
    Last Post: 03-18-2010, 07:49 PM

Tags for this Thread

Posting Permissions

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