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 9 of 9

Thread: Inserting characters from string into 2d array?

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

    Unhappy Inserting characters from string into 2d array?

    Hi there,
    I am new to java and am having trouble with some code... I am trying to input the characters from a string into a 2d array. The array size depends on the amount of characters in the string (get the square root and round up to the nearest int so that the array is a square grid). I'm getting an out of bounds error, but not sure why. I would appreciate any help! Thanks

    This is what I have so far:

    public class Practice {
    	public static void main(String[] args) {
     
    		String text = args[0];
    		text = text.replaceAll("\\s", "");
    		text = text.toLowerCase();
    		System.out.println(text);
     
    		float squareRoot = (float) Math.sqrt(text.length());
    		int dimension = (int) Math.ceil(squareRoot);
    		int x = dimension;
    		int y = dimension;
     
    		 char[] charArray = text.toCharArray();
    		 char[][] myArray = new char[x][y];
    		 int nextChar = 0;
    		 for (int a = 0; a < x; ++a) {
    			 for (int b = 0; b <y; ++b) {
    				 myArray[x][y] = charArray[nextChar]; 
    				 nextChar++;
    			 }
    		 }
    }
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Inserting characters from string into 2d array?

    You're iterating x*y times, which is likely more than charArray.length, because you round up. So nextchar will be >= than charArray.length, causing the Exception.

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

    amf19 (April 12th, 2013)

  4. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inserting characters from string into 2d array?

    ah, thank you.. that makes sense. Still unsure as to how I would go about fixing this though?

  5. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Inserting characters from string into 2d array?

    You have to stop looping as soon as nextchar will be >= charArray.length

  6. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inserting characters from string into 2d array?

    I've tried putting an if statement in, but that doesn't seem to work:

    		char[] charArray = text.toCharArray();
    		char[][] myArray = new char[x][y];
    		int nextChar = 0;
    			for (int a = 0; a < x; ++a) {
    				for (int b = 0; b < y; ++b) {
    					if (nextChar < charArray.length) {
    					myArray[x][y] = charArray[nextChar];
    					nextChar++;
    					}
    			}
    		}

  7. #6
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Inserting characters from string into 2d array?

    What does "doesn't seem to work" mean? Did you blow up your toaster? What error messages/stack traces do you get now?

  8. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inserting characters from string into 2d array?

    I still get the same error - out of bounds exception

  9. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Inserting characters from string into 2d array?

    Aha! You're not using the loop variables as index!
    myArray[a][b] = charArray[nextChar];
    You're using x and y which are out of bounds.

  10. The Following User Says Thank You to PhHein For This Useful Post:

    amf19 (April 12th, 2013)

  11. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Inserting characters from string into 2d array?

    You're right! Still out of bounds error though after fixing!!

    --- Update ---

    Wait ignore last post, it worked! Thank you so much

Similar Threads

  1. Replies: 2
    Last Post: March 28th, 2013, 10:54 AM
  2. Trouble with getting middle characters from a string.
    By doonar15 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 24th, 2013, 07:41 PM
  3. Split a string into individual characters
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 6th, 2012, 03:55 PM
  4. Get characters between specific elements in string
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2011, 07:51 PM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 09:49 AM