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 4 of 4

Thread: Brand new to Java and programming

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Brand new to Java and programming

    As part of my homework, actually this part is for extra credit. I am supposed to write a program that prompts the user to input a decimal number and outputs the number to the nearest integer. Using only things I have learned to this point. I couldn't figure it out. I read ahead in the book about if else statements and came up with the following, but is there a way to do it with out if, else statements?(I am only 1 week into this class, so I can't use anything like the Math round method. I am supposed to do this programmatically):

    //Chapter 2 programming exercises 6.)
     
    import java.util.*;
     
    public class Ch2_PrExercises6
    {
    	public static void main(String[] args)
    	{
    		Scanner tv = new Scanner(System.in);
     
    		int numResult = 0;
    		double numEntered;
     
    		System.out.println("Enter in a decimal number to be rounded to the nearest whole number, then press Enter: ");
    		System.out.println();
     
    		numEntered = tv.nextDouble();
     
    		if(numEntered % 1 >= (1/2))
    		{
    			numResult = (int)(numEntered) + 1;
    		}
     
    		else
    		{
    			numResult = (int)(numEntered);
    		}
     
    		System.out.println("You number rounded to the nearest whole number = " + numResult);
     
    	}
    }

    thank you in advance!


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Brand new to Java and programming

    I can't use anything like the Math round method
    Odd you can't use Math.round() :p
    If you want your current method to work, replace (1/2) with 0.5;

    Edit: Rewrote post
    Last edited by newbie; August 26th, 2011 at 05:56 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,061
    Thanks
    52
    Thanked 2,282 Times in 2,253 Posts

    Default Re: Brand new to Java and programming

    replace (1/2) with 0.5 because 1/2 will be computed using integer arithmetic and gives a result of 0

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Brand new to Java and programming

    is there a way to do it with out if, else statements?
    Yes, you can do it with a little bit of arithmetic and the fact that floating->int conversion discards the fractional part. Think of simple arithmetic that converts .5 and greater fractional parts to 1.0 or greater, but not to 2.0!

Similar Threads

  1. Brand New/First Attempt
    By kevinco in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2011, 08:45 PM
  2. [PROJECT] JWebby: A brand new webserver
    By Verficon in forum Java Networking
    Replies: 2
    Last Post: March 7th, 2011, 10:49 AM
  3. Is java a power full programming language? can java do this?
    By Jhovarie in forum Java Theory & Questions
    Replies: 5
    Last Post: March 2nd, 2011, 01:02 PM
  4. Brand New
    By BigJoe in forum Member Introductions
    Replies: 1
    Last Post: December 27th, 2010, 12:26 PM
  5. [SOLVED] Brand Spanking New to Java...
    By forte in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 18th, 2010, 06:41 AM