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.
Code :
public class convertFile {
String[] array = new String[26];
char ch;
{
for (int i=0; i<array.length; i++) {
}
}
}
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?
Code :
for (int i=0; i<alphabet.length; i++) {
alphabet[i] = alphabet[i++];
System.out.println(alphabet[i]);
}
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
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".
Code :
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]);
}
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.
Code :
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]);
}
Re: How To Increment Array Elements
Explain the logic you are trying to accomplish with this:
Code java:
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?
Re: How To Increment Array Elements
Quote:
Originally Posted by
aussiemcgr
Explain the logic you are trying to accomplish with this:
Code java:
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?
Re: How To Increment Array Elements
Quote:
Originally Posted by
Nuggets
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.
Code :
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].
Re: How To Increment Array Elements
Quote:
Originally Posted by
andreas90
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?
Code :
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]);
}
}
}
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"?
Code :
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;
}
}
}
Re: How To Increment Array Elements
Quote:
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.
Re: How To Increment Array Elements
Quote:
Originally Posted by
copeg
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".
Re: How To Increment Array Elements
Here is newly modified for loop.
Code :
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]);
}
}
}
}
Re: How To Increment Array Elements
Quote:
Originally Posted by
Nuggets
Here is newly modified for loop.
Code :
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.
Re: How To Increment Array Elements
Quote:
Originally Posted by
andreas90
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.
Re: How To Increment Array Elements
Quote:
Originally Posted by
Nuggets
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. :P