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.


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

Results 1 to 3 of 3

Thread: Logic to implement a program to display decimal place of a number

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Thanked 30 Times in 28 Posts

    Smile Logic to implement a program to display decimal place of a number

    my recent post was already solved tnx for all the help guys

    but im getting stuck again regarding on that the same problem,the program must display how many DECIMAL PLACES a number has. (e.g I entered number20 on num1,so it should display that " THE FIRST NUMBER HAS TWO DECIMAL PLACES and so on...)

    "BUT" if i entered negative it should display
    i should only type numbers who are not negative.

    I used if-else statement:

    if (num1<=9) // this would tell that the numbers from 9 below have only 1 decimal place.

    else if (num1<=99) // also, this would tell that numbers from 99 below have 2 decimal places

    but what am i going to do if i entered negative numbers
    i tried this

    if (num1<0) and i tried this one (num1<= -1)
    i doesnt work.

    anyway my problem is not SYNTAX ERROR.
    im stuck with some LOGICAL ERRORS.

    hope anyone can help me again here

    tnx guys!!

  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    United Kingdom
    My Mood
    Thanked 293 Times in 226 Posts
    Blog Entries

    Default Re: another problem with algorithms

    Hello chronoz13,

    if (num1 < 0) should work. See this example:

    public class Chronoz13 {
         * JavaProgrammingForums.com
        public static void main(String[] args) {
            int myInt = -5;
            if(myInt < 0){
                System.out.println("Oi! No negative numbers!");

    As expected, because the number is below 0, the Oi! No negative numbers! String is printed.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Thanked 30 Times in 28 Posts

    Smile Re: another problem with algorithms

    sorry for not posting my problem briefly,actually sir. its not my only problem..
    ill post the code

    import java.io.*;
    public class Main
        private static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        public static void main(String[] args)throws IOException
            String number1,number2,number3;
            int num1,num2,num3,decimalPlaces=0;
            System.out.print("Enter a number : ");
            System.out.print("Enter the second number : ");
            System.out.print("Enter the third number : ");
                         (num<=9)                                              /* this part is running perfectly down to the
                          decimalPlaces=decimalPlaces+1;          third condition,if the number is <=999
            else if
            else if 
                    System.out.println("Cannot proceed");
                    System.out.print("The first number has "+decimalPlaces+" decimalplaces");

    the program is totally running it doesnt have syntax erros

    but the problem is ,its not running the way i expect to.

    the first three condition is running perfectly as i want to.. but in the last condition,
    if i entered number below 0 (negative numbers), it prints the condition ("CANNOT PROCEED")

    BUT IT ALSO PRINTS THE OTHER STRING ("the first number has " +decimalPlaces + " ");

    the only thing that i want to happen is this
    if i entered positive numbers then it will print the decimal places according to the condition that i make ,and it will print the string ("The first number has " +decimalPlaces+ " son on...");

    but if i entered negative numbers , the last condition will automatically do its job and print ONLY the string ("CANNOT PROCEED");

    just having some structure problem here..

    anyway sir.. tnx !!
    Last edited by chronoz13; June 11th, 2009 at 04:07 AM.

Similar Threads

  1. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 12:34 PM