Displaying Information On The Screen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abhishekphukan
    New Member
    • Jul 2014
    • 34

    Displaying Information On The Screen

    How can i write a program to display a number of series like the following:

    56789
    6789
    789
    89
    9

    Code:
    public class displaying {
       
       
       public static void main(String args[]){
    	   int x,y,z;
    	   for(x=0;x<5;x++){
    		 for(y=5;y<10;y++){
    			 z=x+y;
    			 System.out.println(z);
    		 }
    	   }
       }
    }
    i have write the program but its not giving the desired output.Somethin g is missing.Please can anyone tell what condition i required for the completion of the above problem.
    Thank You....
    Last edited by Rabbit; Jan 9 '15, 05:48 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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:
    Code:
           --------------------- 
    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:
    Code:
    // 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:
    Code:
      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.
    Code:
    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
    Last edited by Frinavale; Jan 12 '15, 05:58 PM.

    Comment

    Working...