0

My code:

import java.io.*;
public class compute_volume
{
   public static void main(String args[])throws IOException{
       InputStreamReader reader = new InputStreamReader(System.in);
       BufferedReader input = new BufferedReader(reader);
       boolean answer;
       double radius,height;
       final double pi = 3.14159;
       do{System.out.println("Enter the radius,press enter and then enter the height");
       String t = input.readLine();
       radius = Integer.parseInt(t);
       t = input.readLine();
       height = Integer.parseInt(t);
       System.out.println("The volume of a cylinder with radius " + radius + " and height " + height + " is " + (pi*pi*height) + "\n Thanks for using my cylinder volume calculator.Enter \"yes\" to use again or \"no\" to stop.");
       t = input.readLine();
       System.out.println(t);
       if ( t == "yes"){
           answer = true;
        }else{ 
            answer= false;
        }
    }while(answer);
    }
}

Problem:

The user inputs yes but the calculator doesn't restart.

Solution:

That's what I don't know ,and hope to know by posting this here.

Getz
  • 3,983
  • 6
  • 35
  • 52
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84

6 Answers6

6

use

if ( "yes".equalsIgnoreCase(t))

instead of

  if ( t == "yes")

equals method checks the content of strings while == checks for object equality.

Read related post for your understanding :

Java String.equals versus ==

Community
  • 1
  • 1
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 1
    +1...I have been facing the same problem with string comparison some days ago. I did `String.contentEquals("")` to solve it. At first I had thought thought that the `==` operator would work as it does in C#. Good to know the answer I found was correct. :) – Writwick Jun 27 '13 at 08:31
6

Incorrect String comparison, instead of:

if ( t == "yes"){

you should have

if ("yes".equalsIgnoreCase(t)) {
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
1

Use equals() instead of == to compare Strings.

"yes".equals(t)

Read this thread for more information.

Community
  • 1
  • 1
LaurentG
  • 11,128
  • 9
  • 51
  • 66
1

In Java, for String, remember to use "equals()" but not "==".

answer="yes".equalsIgnoreCase(t);

replace the code:

    if ( t == "yes"){
       answer = true;
    }else{ 
        answer= false;
    }
Anderson
  • 2,496
  • 1
  • 27
  • 41
1

Correct this:

if ( "yes".equalsIgnoreCase(t))

rather then

  if ( t == "yes")

If we not override Equals() then by default Equals() of Object class is called.So it will compare contents rather than object.

Manish Doshi
  • 1,205
  • 1
  • 9
  • 17
1

It is better to use as follows

 import java.io.*;
 import java.util.Scanner;

 public class compute_volume {
    public static void main(String args[])throws IOException{
    Scanner sc = new Scanner(System.in);
    boolean answer;
    double radius,height;
    final double pi = 3.14159;
    do{System.out.println("Enter the radius,press enter and then enter the height");
        String t = sc.nextLine();
        radius = Double.parseDouble(t);
        t = sc.nextLine();
        height = Double.parseDouble(t);
        System.out.println("The volume of a cylinder with radius " + radius + " and height " + height + " is " + (pi*pi*height) + "\n Thanks for using my cylinder volume calculator.Enter \"yes\" to use again or \"no\" to stop.");
        t = sc.nextLine();
        System.out.println(t);
        if (t.equalsIgnoreCase("yes")){
            answer = true;
        }else{
            answer= false;
        }
    }while(answer);
}
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115