Results 1 to 9 of 9
Thread: How to indent?
- 11-07-2011, 02:17 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
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?
- 11-07-2011, 02:34 AM #2
Re: How to indent?
What do you mean? Indent your code or indent the output?
- 11-07-2011, 02:49 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
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++; } } }
- 11-07-2011, 03:07 AM #4
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.
- 11-07-2011, 03:13 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 6
- Rep Power
- 0
Re: How to indent?
I know, but did I do it right?
Did I indent it correctly?
- 11-07-2011, 03:25 AM #6
Re: How to indent?
Does it look right?
- 11-07-2011, 03:31 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
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:
More important issues might be...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++; } } }
- Class names should start with a capitol letter.
- MAXCARDS would make more sense as a 'static' class field.
- 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.)
- 11-07-2011, 04:08 AM #8
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:
Some people do this:Java Code:void someMethod() { ... }
It doesn't really matter as long as you are consistent.Java Code:void someMethod() { ... }
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:
Because it's too easy to do this:Java Code:if(condition) statement;
The link in my sig has some tips about naming.Java Code:if(condition) statement; statement; // looks like part of the conditional, but it's notGet in the habit of using standard Java naming conventions!
- 11-07-2011, 06:29 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
-
[SOLVED] Presentation of code, to indent or not to indent
By dbashby in forum New To JavaReplies: 8Last Post: 04-06-2009, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks