Search code examples
javado-whilebluej

Do While Issue : Program does not stop even when do while's condition is fulfilled


This program is for finding out the difference between the Compound Interest and Simple Interest.

This code should terminate when I enter time as 0(Zero) and the code should run at least once. and when I compile the code, the compiler says:

Class compiled - no syntax errors

but when I run the code,the second condition "run atleast once" is fulfilled and every thing works fine but after accepting the values, It does not stop if time is entered as 0(zero) (which according to the title it should) and it gives me the wrong answer.

Example :

p=3000
r=10
t=2

for the above values, the output should be 30.0 but when I run the program It gievs me the output as -600.0 (a negetive and a wrong Difference).

import java.io.*;
class p6
 {
  InputStreamReader i1 =new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(i1);  
// calculating the CI and SI
public void calc()throws IOException
{   int p,t,r,si;double a, ci;char k;
    do
    {
      // getting principle
      System.out.println("Enter Principle in decimal:");           
      p = Integer.parseInt(br.readLine());
      // getting Rate
      System.out.println("Enter Rate in decimal    :"); 
      t = Integer.parseInt(br.readLine());
      // getting Time
      System.out.println("Enter Time               :"); 
      r = Integer.parseInt(br.readLine());
      //calculating SI
      si = (p*t*r)/100;
      // calculating amount
      a = (double)p*(Math.pow((1+r/100),t));  
      // calculating CI
      ci = a - p;
      // printing Difference
      System.out.println("Difference between CI and SI = "+(ci-si)); 

     }while(t!=0);//end of DOWhile
  }// end of calc
}//end of p6

I'm using BlueJ to write and run my code.

I'm a beginner and just started learning java a year ago.

Please help me resolve this issue as early as possible.

Any help appreciated,

Anurag.


Solution

  • Try out this:

    import java.io.*;
    class p6
     {
      InputStreamReader i1 =new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(i1);  
    // calculating the CI and SI
    public void calc()throws IOException
    {   double p,t,r, a, si, ci;char k;//Changed all to double.
        do
        {
          // getting principle
          System.out.println("Enter Principle in decimal:");           
          p = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
          // getting Rate
          System.out.println("Enter Rate in decimal    :"); 
          r = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
          // getting Time
          System.out.println("Enter Time               :"); 
          t = Double.parseDouble(br.readLine());// changed to Double.parseDouble to store double value.
          //calculating SI
          si = (p*t*r)/100;
          // calculating amount
          a = (double)p*(Math.pow((1+r/100),t));  
          // calculating CI
          ci = a - p;
          // printing Difference
          System.out.println("Difference between CI and SI = "+(ci-si)); 
    
         }while(t!=0);//end of DOWhile
      }// end of calc
    }//end of p6