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

Thread: Help with Arrays!

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

    Question Help with Arrays!

    Hey guys! I'm fairly new to java. For my course at uni I've got to design a caeser box that will encrypt a word and decrypt it. I pretty certain I've got the logic behind it so I won't need someone to give me the full code haha that would be cheating anyway. I like a challenge. Anyway.
    I've started my code by reading the string, I've eliminated white spaces and its all lower case. After that I put the string in a char array with a total length of the string without whitespaces. after this I declared a grid with dimensions of x*x where x is the closest root of the length of the string without whitespaces.
    So upto now its all good but this is where the problem starts ...
    I've done 2 for loops to create the grid and then I'm increasing the rows and columns by 1 (ofc) and the values of each grid is equal to the elements of my char array that contains the string.
    this might seem messy but I'll paste the problematic code part:
    for(int row = 0; row<plainTextGrid.length; row++){
        		for(int column = 0; column <plainTextGrid.length; column++){
        			plainTextGrid[row][column]= plainTextArray[0];// this is where I want to set each element to a position on the grid. plainTextGrid is the grid, and plainTextArray is the array
     
     
        			System.out.print(plainTextGrid[row][column] + " ");
        			}
     
        			System.out.println();
        		}
    My question is how can I increase each element of the char array by 1.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Arrays!

    how can I increase each element of the char array by 1.
    You can do normal math with char values: 'a' + 1 = 'b'
    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:

    le pencil (April 12th, 2013)

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

    Default Re: Help with Arrays!

    Quote Originally Posted by Norm View Post
    You can do normal math with char values: 'a' + 1 = 'b'
    I don't actually get what you are trying to say. My char array is in the two for loops, I want to increment the value of it until it reaches the last char in the array. so for example I could do this:
    plainTextGrid[row][column]= plainTextArray[0];
    plainTextArray[0]=plainTextArray[1];
    plainTextArray[1]=plainTextArray[2];
    //etc...

    yeah so basically thats untidy and not useful for my situation

    --- Update ---

    Man Norm you are the best I can't believe how easy of a fix that was!!! cheers!

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Arrays!

    Please explain what you mean by " increment the value"
    x= x+1 // increments the value of x by 1

    The code at the end of the post looks like it is shifting the elements to the left by one slot. Does the shifting wrap around at the end: first element goes in last slot?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Help with Arrays!

    So what I did was add an int above the for loops
    int a = 32;
        	for(int row = 0; row<plainTextGrid.length; row++){
        		for(int column = 0; column <plainTextGrid.length; column++){
     
        			plainTextGrid[row][column]= plainTextArray[32-a];
        			a--;
     
     
        			System.out.print(plainTextGrid[row][column] + " ");
        			}
     
        			System.out.println();
        		}

    and output :
    a l o n g t
    i m e a g o
    , i n a g a
    l a x y f a
    r , f a r a
    w a y Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 33
    at CaesarBox.encrypt(CaesarBox.java:30)
    at CaesarBox.main(CaesarBox.java:8)

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Arrays!

    ArrayIndexOutOfBoundsException: 33
    Looks like a went to -1
    33 = 32 - (-1)
    How many times does the inner loop execute?

    The code needs to check that the index is in bounds.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Arrays
    By LeeDD in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 23rd, 2013, 09:10 PM
  2. Arrays
    By oonagh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 7th, 2012, 10:03 AM
  3. Arrays ?
    By nhiap6 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 17th, 2012, 12:15 PM
  4. Help with Arrays
    By xdx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 12:39 AM
  5. Arrays
    By ruffu054 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 7th, 2011, 11:18 PM

Tags for this Thread