Results 1 to 9 of 9

Thread: How to indent?

  1. #1
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default How to indent?

    Java Code:
     public class printCards
    {
    public static void main (String []args)
    {
    int x = 0; // keeps count of how many card
    final int MAXCARDS = 20; // maximum number of cards
    
    while (x < MAXCARDS)
    {
    
    Card card = new Card(); //creates a card called card
    String result = card.cardGeneration(); //generates a random card
    System.out.println(result); //prints name of card
    x++;
    }
    }
    }

    All this time I haven't been indenting... I've looked at examples but how do I indent this one?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: How to indent?

    What do you mean? Indent your code or indent the output?

  3. #3
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: How to indent?

    Indent the code... here is what I did. Is it correct?

    Java Code:
    public class printCards
    {
    
        public static void main (String []args)
        {
        int x = 0; // keeps count of how many card
        final int MAXCARDS = 20; // maximum number of cards
    
        while (x < MAXCARDS)
        {
    
        Card card = new Card(); //creates a card called card
        String result = card.cardGeneration(); //generates a random card
        System.out.println(result); //prints name of card
        x++;
        }
        }
    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: How to indent?

    You indent your code when you type it into your IDE. When posting it here, you copy and paste the code and wrap it it in code tags. This will preserve any indentation.

  5. #5
    federer is offline Member
    Join Date
    Nov 2011
    Posts
    6
    Rep Power
    0

    Default Re: How to indent?

    I know, but did I do it right?

    Did I indent it correctly?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: How to indent?

    Does it look right?

  7. #7
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default Re: How to indent?

    There is no absolute rule of "correct" or "incorrect" formatting.

    Normally one does indent further for each '{ ... }' block. You did not do that.

    This is what Eclipse likes:
    Java Code:
    public class printCards {
    
    	public static void main(String[] args) {
    		int x = 0; // keeps count of how many card
    		final int MAXCARDS = 20; // maximum number of cards
    
    		while (x < MAXCARDS) {
    
    			Card card = new Card(); // creates a card called card
    			String result = card.cardGeneration(); // generates a random card
    			System.out.println(result); // prints name of card
    			x++;
    		}
    	}
    }
    More important issues might be...
    1. Class names should start with a capitol letter.
    2. MAXCARDS would make more sense as a 'static' class field.
    3. Think of a better name for the variable "x". (I might call it "cardIdx". But I'm not saying that this is the best possible name.)

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How to indent?

    There's no single "correct" way to indent. You can use spaces or tabs. If you use spaces, use enough spaces to clearly distinguish the nesting depth, but no so many that code frequently runs past the right side of the screen. Most people use 3-5. There are also different ways to use brackets. I pesonally use this:

    Java Code:
    void someMethod() {
        ...
    }
    Some people do this:

    Java Code:
    void someMethod()
    {
        ...
    }
    It doesn't really matter as long as you are consistent.

    I also have a personal policy of always either using brackets around conditional blocks, or putting an entire conditional statement on one line. I never do this:

    Java Code:
    if(condition)
        statement;
    Because it's too easy to do this:


    Java Code:
    if(condition)
        statement;
        statement; // looks like part of the conditional, but it's not
    The link in my sig has some tips about naming.
    Get in the habit of using standard Java naming conventions!

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to indent?

    For what it's worth here are Sun/Oracle's coding standards. I haven't read them for a while, but I think that what they describe is what I do.

Similar Threads

  1. Replies: 8
    Last Post: 04-06-2009, 01:13 AM

Posting Permissions

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