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

• June 10th, 2009, 09:05 AM
chronoz13
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!!
• June 10th, 2009, 10:48 AM
JavaPF
Re: another problem with algorithms
Hello chronoz13,

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

Code :

```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.
• June 11th, 2009, 03:53 AM
chronoz13
Re: another problem with algorithms
sorry for not posting my problem briefly,actually sir. its not my only problem..
ill post the code

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 : "); number1=br.readLine(); num1=Integer.parseInt(number1);   System.out.print("Enter the second number : "); number2=br.readLine(); num2=Integer.parseInt(number2);   System.out.print("Enter the third number : "); number3=br.readLine(); 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 !!