Your program is supposed:
- Start with to take a bunch of characters (
56789
) - Display all of the characters and then, on subsequent lines, continue to display the the characters after removing one from the right
In Java there are structures called
Arrays that contain a group of things.
In your case you should have an array of
characters that contains the values
56789
. (You could also use an array of
Numbers or
Integer or anything else really)
So, after you populate your array, the array will look like this:
-
---------------------
-
data: | 5 | 6 | 7 | 8 | 9 |
-
---------------------
-
index: [0] [1] [2] [3] [4]
-
Please note that the character "5" is in position/index 0 in the array.
With that in mind, this is how you would populate your array:
-
// creating an array variable to hold the information
-
char[] anArray;
-
-
// allocates memory for 5 characters
-
anArray = new char[5];
-
-
// initialize first element
-
anArray[0] = '5';
-
// initialize second element
-
anArray[1] = '6';
-
// and so forth
-
anArray[2] = '7';
-
anArray[3] = '8';
-
anArray[4] = '9';
You can accomplish the same thing in less lines of code by doing the following:
-
char[] anArray = {'5','6','7','8','9'};
-
But it will result in the same thing as demonstrated above.
Now that you have an array, you can loop through the elements within it and display them on your screen.
-
public static void main(String[] args){
-
char[] anArray = {'5','6','7','8','9'};
-
for(int index=0; index<anArray.length; index++){
-
System.out.print(anArray[index]);
-
}
-
System.out.println('');
-
}
-
Now, I will leave it up to you to try and figure out how to print the array by removing the right item. (Hint: you can manipulate the index of the array that you start at while printing)
If you have a problem with it, post back here and I'll help you through it.
Please take a look at the links I've posted because they have very useful information in them.
-Frinny