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

Thread: How To Increment Array Elements

  1. #1
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default How To Increment Array Elements

    I need to be able to increment each input value that is a letter in the alphabet by 1. For example, "ABC" would be input, "BCD" would be the output. I know kind of what I need, like I'm thinking maybe charAt method or indexOf. But I don't know how to implement it into my for loop. I don't have much code done because I'm stuck.
    public class convertFile {
    	String[] array = new String[26];
    	char ch;
    	{
     
    	for (int i=0; i<array.length; i++) {
     
    	}
    }
    	}


  2. #2
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Modified code. I input all the letters of the alphabet except for Z. The output does shift the letters by 1, but it's skipping every other letter. For example, input "ABCDEFG", output "BDFH". How do I make it so it does every letter in the String?
    for (int i=0; i<alphabet.length; i++) {
    						alphabet[i] = alphabet[i++];
    						System.out.println(alphabet[i]);
    					}

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How To Increment Array Elements

    Think about how that code runs...in particular how incrementing i from within the loop affects the output. Also remember that a char can be cast to an int which is the ascii value of the character, and which can be incremented

  4. #4
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Modified code again and it now shifts every input found in the array to the next element. How would I track down an input value, for example the character "Z"? When "Z" is entered, I want the program to shift that element in the array to the first. For example, input is "XYZ", output should be "YZA".

    char alphabet[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    		String str = new String(alphabet);
     
    		for (int i=0; i<alphabet.length; i++) {
    			alphabet[i] = alphabet[i+1];
    			System.out.println(alphabet[i]);
    		}

  5. #5
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Been fiddling around with this if statement but it's not working. It prints both "A" and "Z" after Y. For example, input is "XYZ", output is "YAZ". Man, I feel I'm so close to getting this to work properly. I posted my modified for loop below.
    for (int i=0; i<alphabet.length; i++) {
    						alphabet[i] = alphabet[i+1];
    						if (alphabet[i] == alphabet[25]) {
    							System.out.println("A");
    						}
    						else {
    							System.out.println();
    						}
    						System.out.println(alphabet[i]);
    					}

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: How To Increment Array Elements

    Explain the logic you are trying to accomplish with this:
    alphabet[i] = alphabet[i+1];
    if (alphabet[i] == alphabet[25]) {
         System.out.println("A");
    }

    The questions that I have here are:
    1) Are you aware you are overriding the item in index i with the statement alphabet[i] = alphabet[i+1]? So if i was 0 (A), i+1 would be 1 (B). Which means indexes 0 and 1 would both be B after running that statement.
    2) Why are you checking if the ith item is equal to the 25th item?

    I feel like what you should be doing is looping the length of the input (since you want the output to be the same length) instead of looping the alphabet array directly. The first thing I would do is create a method that loops through the alphabet array and returns an integer, representing the starting index (for the example in your first post, since we want to start at B, the method would return 1). I would call that method before the loop and store the returned value in an int that I would use in the loop. In the loop, I would loop until the length of the input, incrementing the index as well as the count. The last thing (or first thing, depends on your design) in the loop is to do a check to see if the current index is outside of the bounds of the alphabet array and, if it is, set the index to 0 (so it points at the letter A after Z).
    I'm not sure if I explained that well. Does it make any sense?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Nuggets (April 1st, 2012)

  8. #7
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by aussiemcgr View Post
    Explain the logic you are trying to accomplish with this:
    alphabet[i] = alphabet[i+1];
    if (alphabet[i] == alphabet[25]) {
         System.out.println("A");
    }

    The questions that I have here are:
    1) Are you aware you are overriding the item in index i with the statement alphabet[i] = alphabet[i+1]? So if i was 0 (A), i+1 would be 1 (B). Which means indexes 0 and 1 would both be B after running that statement.
    2) Why are you checking if the ith item is equal to the 25th item?

    I feel like what you should be doing is looping the length of the input (since you want the output to be the same length) instead of looping the alphabet array directly. The first thing I would do is create a method that loops through the alphabet array and returns an integer, representing the starting index (for the example in your first post, since we want to start at B, the method would return 1). I would call that method before the loop and store the returned value in an int that I would use in the loop. In the loop, I would loop until the length of the input, incrementing the index as well as the count. The last thing (or first thing, depends on your design) in the loop is to do a check to see if the current index is outside of the bounds of the alphabet array and, if it is, set the index to 0 (so it points at the letter A after Z).
    I'm not sure if I explained that well. Does it make any sense?
    I kind of understand. But how do I set the array values to an int index?

  9. #8
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by Nuggets View Post
    Been fiddling around with this if statement but it's not working. It prints both "A" and "Z" after Y. For example, input is "XYZ", output is "YAZ". Man, I feel I'm so close to getting this to work properly. I posted my modified for loop below.
    for (int i=0; i<alphabet.length; i++) {
    						alphabet[i] = alphabet[i+1];
    						if (alphabet[i] == alphabet[25]) {
    							System.out.println("A");
    						}
    						else {
    							System.out.println();
    						}
    						System.out.println(alphabet[i]);
    					}
    Hello Nuggets!
    I think you are close to getting it work properly too. You should put first your if statement and then in your else block do the assignment alphabet[i] = alphabet[i+1] and print alphabet[i].

  10. #9
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by andreas90 View Post
    Hello Nuggets!
    I think you are close to getting it work properly too. You should put first your if statement and then in your else block do the assignment alphabet[i] = alphabet[i+1] and print alphabet[i].
    That helped a lot, thanks! I think what Aussie said made sense though. I need to take the input, rather than the array. I modified code again. Output keeps looping through the array elements and printing them multiple times. Example, Input "WXYZ", Output "XYZAXYZAXYZA". How would I take the input length into my for loop?

    for (int j=0; j<line.length(); j++) {
    					for (int i=0; i<alphabet.length; i++) {
     
    						if (alphabet[i] == alphabet[25]) {
    							System.out.println("A");
    						}
    						else {
    							alphabet[i] = alphabet[i+1];
    							System.out.println(alphabet[i]);
    						}
     
    					}
    			}
    Last edited by Nuggets; March 31st, 2012 at 04:36 PM.

  11. #10
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Here is my modified for loop. I think I have almost solved this problem. Input example, "ABC". Output example is exactly as shown:
    "D
    C
    B"
    How would I change it so it prints horizontally like this, "BCD"?
    		for (int pos = line.length() - 1; pos >= 0; pos--) {
    						for (int i=0; i<alphabet.length; i++) {
    						if (alphabet[pos] != 'Z') {
    							alphabet[pos]++;
    							System.out.println(alphabet[pos]);
    							break;
    						} else {
    							System.out.println("A");
    							break;
    						}
     
    					}
     
    				}

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How To Increment Array Elements

    How would I change it so it prints horizontally like this, "BCD"?
    See the API for PrintStream ( PrintStream (Java Platform SE 6) ), which is what System.out refers to. You use the method println which includes a new line separator, there are other methods (such as print) which do not include the new line. Alternatively you can build a String from the results and print that out once at the end.

  13. #12
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by copeg View Post
    See the API for PrintStream ( PrintStream (Java Platform SE 6) ), which is what System.out refers to. You use the method println which includes a new line separator, there are other methods (such as print) which do not include the new line. Alternatively you can build a String from the results and print that out once at the end.
    I changed println to print and it prints horizontally now. But why is my code printing my output backwards? For example, Input: "ABC", Output: "DCB". I want it to print in the same format as the input is, but shifted one letter. Example of preferred output: "BCD".

  14. #13
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Here is newly modified for loop.

    for (int pos = line.length() - 1; pos >= 0; pos--) {
    						for (int i=0; i<alphabet.length; i++) {
    						if (alphabet[pos] != 'Z') {
    							alphabet[pos]++;
    							System.out.print(alphabet[pos]);
    							break;
    						} else {
    							if (alphabet[pos] == 'Z') {
    								System.out.print("A");
    								break;
    							}
    							else {
    							System.out.print(alphabet[pos]);
    							}
    						}
     
    					}
     
    				}

  15. #14
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by Nuggets View Post
    Here is newly modified for loop.

    for (int pos = line.length() - 1; pos >= 0; pos--) {
    						for (int i=0; i<alphabet.length; i++) {
    						if (alphabet[pos] != 'Z') {
    							alphabet[pos]++;
    							System.out.print(alphabet[pos]);
    							break;
    						} else {
    							if (alphabet[pos] == 'Z') {
    								System.out.print("A");
    								break;
    							}
    							else {
    							System.out.print(alphabet[pos]);
    							}
    						}
     
    					}
     
    				}
    It displays your String backwards because of the outer for loop's header. Try modifying it so that it loops through line from 0 to line.length() and increment pos.

  16. The Following User Says Thank You to andreas90 For This Useful Post:

    Nuggets (April 1st, 2012)

  17. #15
    Member
    Join Date
    Jan 2012
    Posts
    65
    My Mood
    Happy
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by andreas90 View Post
    It displays your String backwards because of the outer for loop's header. Try modifying it so that it loops through line from 0 to line.length() and increment pos.
    Thank you.

  18. #16
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Increment Array Elements

    Quote Originally Posted by Nuggets View Post
    Thank you.
    You are welcome. But i don't think that your last posted code does what you wanted it to do. If you have updated it just ignore me.

Similar Threads

  1. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 11:52 PM
  2. Help camparing array elements
    By Richmond in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 12:23 AM
  3. Array elements comparison
    By Pradeep_Parihar in forum Java Theory & Questions
    Replies: 1
    Last Post: December 10th, 2011, 09:45 AM
  4. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  5. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM