Results 1 to 4 of 4
  1. #1
    Libertyman is offline Member
    Join Date
    Jun 2010
    Posts
    26
    Rep Power
    0

    Default i cant crack this code

    my task is to use a loop,, i need the loop to go till 77777

    the problem is that the numbers need to be octagon type.. 1-8 instead of 1-10. i have no idea how to make that conversion. i created 2 loops...

    i tried to come up with a code for 2 hours. but no luck



    Java Code:
    class Ex6
    {
    	public static void main(String[] args)
    	{
    		int count;
    		count=0;
                    
               
                  
    		while(count<77778)
    		{         
                             int n1;
                                 n1=0;
     
                                 while(n1<8)
    
                                {   
                                             
    
                                    
    
                                      n1=n1+1;    }
    		
                            System.out.println(count+n1);
                               
    			count=count+1;
    		}
    
    	}
    
    
    
    
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    use <= 077777 in the loop condition
    and the toString(int, int) method of Integer, with the radix being 8.

    IOW, hardcoded integers that begin with 0 are assumed to be octal by the compiler (which is what you want so simply precede 77777 with a 0 as shown above). Then you also want to print the values in octal representation, so you need to use a radix of 8 in the static toString method (along with the integer to display). For the rest of the code a simple ++ works (in fact, you can easily change the while to a for that contains nothing but the println with the toString method).

    Edit: Of course, it is also easily doable as a one line while loop, too.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Continues to cross-post after being warned. 3 day ban.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Replies: 5
    Last Post: 04-01-2010, 03:57 AM
  2. Replies: 1
    Last Post: 03-04-2010, 11:19 AM
  3. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  4. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •