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: Guidance/Help with Code CleanUp

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Smile Guidance/Help with Code CleanUp

    First of all, yes this is homework and I already solved the problem, which required the programmer to write a program to determine if the digits in a three-digit number supplied by a user are all odd, all even, or mixed. My question is the following: Is there a way to make this code look prettier? I'm a chick and I'm new to Java. I want to keep my program functional, but I would also like it to look nice as well. Any suggestions would be greatly appreciated.

    import java.util.Scanner;//program uses class Scanner
     
    public class Exe_Two 
    {
    	//main method begins execution of Java application
    	public static void main( String[] args)
    	{ 
    		//create a Scanner to obtain input from the command window
    		Scanner in =new Scanner(System.in);
     
    		System.out.println( "Input a three-digit number:");//prompt
     
    		int num =in.nextInt(); //read number from user
    		Boolean number1=false;
    		Boolean number2=false;
    		Boolean number3=false;
     
     
    	number1 = num % 2 == 0;
     
    	number2 = num % 2 == 0;
    	num/=10;
     
    	number3 = num % 2 == 0;
     
    	if(number1&&number2&&number3==Boolean.TRUE)
     
    		System.out.println("This number contains all even digits.");
     
    	else 
     
    	if(number1==Boolean.TRUE||number2==Boolean.TRUE||number3==Boolean.TRUE)
     
    	System.out.println("This number contains both odd and even digits.");
     
    	else 
    		System.out.println("This number contains all odd digits.");
     
    	in.close();
     
    	} // end method main
     
    } // end class Exe_Two
    Last edited by helloworld922; January 5th, 2013 at 11:47 PM. Reason: please use [code] tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Guidance/Help with Code CleanUp

    First thing is to use [code] tags. This formats source code in a nice and readable format. I've modified your post for you.

    A few comments about the actual code:

    1. Parts of your code aren't tabbed properly. This could be a copy/paste error. In general you want to tab in the contents of a block (anything after an opening curly brace, the statements which belong to a control statement like if, while, else, etc.). This is a stylistic issue and your program will execute the same regardless, but it's good practice to make sure your code is tabbed correctly. It makes it a lot easier for others to read your code and follow the logic.

    2. Personal preference, I don't like the shortcut control statements which don't use curly braces for single statements. It's perfectly valid and a lot of people do it, I just don't like it.

    if(some_condition == true)
    {
        System.out.println("this has curly braces! Yay!");
    }
    else
        System.out.println("this doesn't have curly braces. Perfectly valid still, but I don't like it.");

    3. I think you're missing a line of code in there (possibly copy/past error?). Try your program with the number 212 or 121 and see if you can't figure out what's wrong.

    4. don't use Boolean.TRUE. Just use the true keyword.

    5. Along with #4, I would use a primitive boolean instead of the object variant Boolean (notice the capitalized B). I generally avoid using the object variants unless I have to (usually only comes into play when I want to take advantage of polymorphism or generics).

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    sternfox (January 6th, 2013)

  4. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Guidance/Help with Code CleanUp

    Also

    if(someCondition == true)
    {
        // some code here
    }

    is more neatly (and readably) written as

    if(someCondition)
    {
        // some code here
    }

    Sometimes you write conditions this way, sometimes not, so there might be confusion about this. If so, ask.

  5. The Following User Says Thank You to pbrockway2 For This Useful Post:

    sternfox (January 6th, 2013)

  6. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,579
    Thanks
    49
    Thanked 2,233 Times in 2,205 Posts

    Default Re: Guidance/Help with Code CleanUp

    Another suggestion would be to use variable names that describe what the variable holds.
    number1 looks like it should hold a number.
    digit1IsEven looks like it would holds a boolean that is true if a digit is even
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    sternfox (January 6th, 2013)

  8. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    San Francisco
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Guidance/Help with Code CleanUp

    Thank you and everyone else for all of your helpful suggestions and comments.

Similar Threads

  1. Java Program help cleanup
    By egingras in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 5th, 2012, 09:33 AM
  2. Hi Every One Sahaja Here!! Need Guidance
    By sahaja in forum Member Introductions
    Replies: 6
    Last Post: July 3rd, 2012, 09:52 AM
  3. Dice game help, a little cleanup
    By SnarkKnuckle in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 28th, 2011, 11:28 AM
  4. Looking for guidance and instructions
    By coyboss in forum Java Theory & Questions
    Replies: 4
    Last Post: February 12th, 2011, 09:57 AM
  5. [SOLVED] Medication of ArrayDemo.java to include iterative menu
    By xyldon27 in forum Loops & Control Statements
    Replies: 8
    Last Post: June 30th, 2009, 02:10 AM