Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Array help

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Array help

    Hello, I'm new to java and I'm pretty bad at it. When I run my program it says array index out of bounds. I'm supposed to write a program that translates an English sentence to morse code with a driver and utility class. I have to read in a txt file that list the morse code translation like:
    A .-
    B -...

    I tried reading in the txt file and then calling on a method in my utility class to store them in an array

    (Driver)
     
    while(file.hasNext())
    		{
    			int r=0;
    			String q = file.nextLine(); 
    			utilty.setM(q,r);
    			r++;
    		}
    (Utility)
    (I made the array [26][2])
    public void setM(String q, int r)
    	{
    		Char letter=c.charAt(0);
    		code[r][0]=Character.toString(letter);
    		code[r][1]=codeString.substring(1);
    	}
    Then later to translate the code I call on a method in my utility class where I bring in g as a string argument (g is the next letter in the string I'm string to translate)
    int w=0;
    if(g==code[w][0])
    {
        w=0;
        h=code[w][1];
    }
    else
    {
      w++;
    }
    I'm sorry if this looks choppy, I just don't want to get accused of cheating so I only listed the parts I thought might be the problem. I just need a hint, thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,824
    Thanks
    55
    Thanked 2,344 Times in 2,315 Posts

    Default Re: Array help

    array index out of bounds.
    The index has gone past the end of the array.
    Remember: the values of array indexes range from 0 to the array length-1.
    An array with 2 slots has the two indexes: 0 & 1

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    verisimilitude (April 13th, 2013)

Similar Threads

  1. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 10:35 PM
  2. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  3. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 01:57 PM
  4. Replies: 2
    Last Post: May 13th, 2011, 04:08 AM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM