Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Made a little program with an if / else if... Any thoughts appreciated

  1. #1
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Made a little program with an if / else if... Any thoughts appreciated

    It's only small but it'd be great if anyone could just have a look and let me know anything that's a bit iffy or if there are any general practice thing's that I should be aware of (as I'm leaning on my own these things may pass me by without anyone else checking)

    I've tried to explain what it is that I'm doing through the code as well as have it fairly neat as these are obviously important...

    import java.util.Scanner;
     
    public class Deleteme2 {
     
    	public static void main(String[] args) {
     
    		// create a new scanner called Test and set it to receive the system in
    		Scanner Test = new Scanner(System.in);
     
    		// ask the user to enter a value
    		System.out.println("Enter a value : ");
     
    		// create an integer variable called Value and assign it to the Scanners
    		// input
    		int Value = Test.nextInt();
     
    		// Created an IF statement and assigned the first option - if hte text
    		// is less then 12
    		if (Value < 12) {
    			System.out.println("It's less mate");
     
    		}
    		// created an elseif that will confirm the use has entered the value 12
    		else if (Value == 12) {
    			System.out.println("Yeah its 12");
     
    		}
    		// created an else statement that will return a string for any value
    		// greater than 12
     
    		else {
    			System.out.println("It's over 12");
     
    		}
    		// Close scanner
    		Test.close();
     
    	}
     
    }

    Thank you!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,291
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Made a little program with an if / else if... Any thoughts appreciated

    Give variables names that accurately describe what they are AND that begin with lower-case letters. Class names begin with capital letters. Both class, method, and variable names are camel-cased: Deleteme2 should be DeleteMe2.

    While I'm a huge fan of comments - especially those written as part of the design process, before any code is written - your comments are a bit much (stating the obvious), AND they describe what YOU DID (why past tense?), not what the code does or, more importantly IMO, why the code is doing what it's doing.

  3. #3
    Member
    Join Date
    Dec 2013
    Posts
    51
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Made a little program with an if / else if... Any thoughts appreciated

    Cheers Greg - the comments are perhaps a bit much because I wanted to make it as easy as possible for someone (such as yourself) to see what was being done / what I did.... And to sort of "show my working". I get that perhaps this would be a bit much in a real world setting... I think that explaining what the code does and why (as you say) are important and lacking though so thanks

    Quote Originally Posted by Greg
    Give variables names that accurately describe what they are
    do you mean that the variable names should be more descriptive? eg

    Scanner Test = new Scanner(System.in);

    would be better off as

    Scanner NumberInputFromUser = new Scanner(System.in);

    Not sure if that's what you mean, or if that's a bit too wordy.

    Cheers

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,291
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: Made a little program with an if / else if... Any thoughts appreciated

    Again, whatever the Scanner object is called, it should begin with a lowercase letter.

    Test (or more correctly 'test') is inappropriate on several levels.

    Programmers often develop their own standard names for common things, like i and j for loop control variables (the only time single-letter names are appropriate and some might argue with that), and 'input' for Scanner objects that get input from the keyboard. NumberInputFromUser is a bit too wordy and specific, more suitable for the numeral variable itself. As the Scanner instance, the name suggests there'd be another one for obtaining inputs other than numbers, which would be unnecessary.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    7
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Made a little program with an if / else if... Any thoughts appreciated

    The rules and conventions for naming your variables can be summarized as follows:

    • Variable names are case-sensitive. A variable's name can be any legal identifier an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_". The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged. White space is not permitted.


    • Subsequent characters may be letters, digits, dollar signs, or underscore characters. Conventions (and common sense) apply to this rule as well. When choosing a name for your variables, use full words instead of cryptic abbreviations. Doing so will make your code easier to read and understand. In many cases it will also make your code self-documenting; fields named cadence, speed, and gear, for example, are much more intuitive than abbreviated versions, such as s, c, and g. Also keep in mind that the name you choose must not be a keyword or reserved word.


    • If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.


    --- Update ---

    In fact, your code no need too much comment like that.

Similar Threads

  1. Replies: 1
    Last Post: February 14th, 2012, 12:53 PM
  2. Replies: 9
    Last Post: December 13th, 2011, 12:50 AM
  3. Array code not producing, thoughts?
    By r19ecua in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 6th, 2011, 06:37 PM