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

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

1. ## 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
"CANNOT PROCEED"
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!!  Reply With Quote

3. ## 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.  Reply With Quote

4. ## 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

{

public static void main(String[] args)throws IOException

{

String number1,number2,number3;
int num1,num2,num3,decimalPlaces=0;

System.out.print("Enter a number : ");
num1=Integer.parseInt(number1);

System.out.print("Enter the second number : ");
num2=Integer.parseInt(number2);

System.out.print("Enter the third number : ");
num3=Integer.parseInt(number3);

if
(num<=9)                                              /* this part is running perfectly down to the
decimalPlaces=decimalPlaces+1;          third condition,if the number is <=999
*/
else if
(num1<=99)
decimalPlaces=decimalPlaces+1;

else if
(num1<=999)
decimalPlaces=decimalPlaces+2;

else

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 !!  Reply With Quote