Results 1 to 8 of 8
  1. #1
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default Trying to write a circle program

    I've been trying to teach myself some Java language as part of a computer literacy regimen, and for the life of me I cannot get this circle identifier program debugged or working. The goal is that the user inputs what type of data they have about a circle, and working out the area from that.
    Java Code:
    import java.util.Scanner;
    public class IdentifyCircle
    {
      public static void main(String[] args)
      {
     
      final double pi = 3.14159;
      double areafromradius = pi * radiusdata * radiusdata;
      double areafromdiameter = pi * diameterdata;
      double areafromcircumference = circumferencedata / pi * 2;
        
      Scanner in = new Scanner(System.in);
      System.out.println("Radius, diameter, or circumference?");
      String type = in.nextLine();
      
      if (type == "Radius");
        {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    
      if (type == "Diameter");
        {
        System.out.println("What is your diameter?");
        double diameterdata = in.nextLine();
        System.out.println("The area of a circle with diameter " + diameterdata + " is " + areafromdiameter);
        }
    
      if (type == "Circumference");
        {
        System.out.println("What is your circumference?");
        double circumferencedata = in.nextLine();
        System.out.println("The area of a circle with circumference " + circumferencedata + " is " + areafromcircumference);
        }
    
      }
    }
    I also cannot work out a formula to find area from circumference, and thus am just using a dummy working one until I can. There is some conflict between using the "double" for my data and "String" for my type... I cannot get my head around it. Furthermore, should I temporarily and superficially resolve these, the whole program runs irrespective of what the user inputs... This code project has proven the most difficult, although it is my most ambitious to date. I know, right, this is... complicated stuff... But regardless, one need start somewhere. Any suggestions, adjustments, or corrections would be much appreciated!
    Last edited by paulmmj; 10-12-2010 at 02:44 AM. Reason: spelling

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by paulmmj View Post
    I've been trying to teach myself some Java language as part of a computer literacy regimen,...
    Welcome to Java and welcome to the Java-Forums.org!

    and for the life of me I cannot get this circle identifier program debugged or working. The goal is that the user inputs what type of data they have about a circle, and working out the area from that.
    You need to tell us more about just how your program is not working -- details are important. Any errors messages? What line?
    What debugging steps have you taken? Do you use println statements to check program flow and to query the state of variables as the program runs? What is the result of this?

    I also cannot work out a formula to find area from circumference, and thus am just using a dummy working one until I can.
    This is less a Java problem and more a basic math problem. You know the formula to calculate circumference from radius and how to calculate area from radius, well if you know circumference, you can get the radius from the first formula and with the radius, you can calculate the area, right? If you're still stuck, again tell us specifically what you've tried and how it's not working.

    There is some conflict between using the "double" for my data and "String" for my type... I cannot get my head around it.
    Again, details, details.

  3. #3
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Welcome to Java and welcome to the Java-Forums.org!
    Thank you!

    Quote Originally Posted by Fubarable View Post
    You need to tell us more about just how your program is not working -- details are important. Any errors messages? What line?
    Java Code:
    7 errors found:
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 8]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:8: cannot find symbol
    symbol  : variable radiusdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 8]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:8: cannot find symbol
    symbol  : variable radiusdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 9]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:9: cannot find symbol
    symbol  : variable diameterdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 10]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:10: cannot find symbol
    symbol  : variable circumferencedata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 19]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:19: incompatible types
    found   : java.lang.String
    required: double
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 26]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:26: incompatible types
    found   : java.lang.String
    required: double
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 33]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:33: incompatible types
    found   : java.lang.String
    required: double
    Quote Originally Posted by Fubarable View Post
    What debugging steps have you taken? Do you use println statements to check program flow and to query the state of variables as the program runs? What is the result of this?
    I'm afraid I've only just tried to patch up holes by looking at the error messages, and I don't really know what the second one means :(

    I suppose my questions is rather general - I would like to know what I've done wrong and how to fix it. I know the onus should be on me to help myself, but I am afraid I do not know what specifics one would need to help me help myself, although if anyone has any direct questions I would certainly answer to the best of my ability. And yeah! That's a good thought on circumference, I'll get on writing that now and edit it when I've figured it out, hahah. This is a rather arduous thing for me, but I'm trying. Thank you for your help! :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK:

    cannot find symbol: radiusdata, diameterdata, circumferencedata...

    Look at where these errors occur and you'll see that you're trying to use variables before they've been declared, and so they don't exist. Shoot you're trying to use them in calculations before they've been given any data.

    solution: read your texts to see how to declare variables and fill with data before trying to use them.

    Next, you've got some other big problems here:
    Java Code:
      if (type == "Radius");
        {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    For one you shouldn't use == to compare Strings because this tests to see if one variable refers to the same object as another variable and you aren't interested in this. Rather, you want to know if one String variable contains the same letters as another and for this you should use the String equals(...) or equalsIgnoreCase(...)
    method.

    Also, that semicolon at the end of your if block's boolean condition effectively short-ciruits the if block. What the if block above really does is this:
    Java Code:
      if (type == "Radius")
               ; // this empty statement will be conditionally peformed.
    
    // the block below is not part of your if block and so it will always be performed.
    {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
    }
    Solution, again use the equals method and get rid of the semicolon

  5. #5
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    OK:

    cannot find symbol: radiusdata, diameterdata, circumferencedata...

    Look at where these errors occur and you'll see that you're trying to use variables before they've been declared, and so they don't exist. Shoot you're trying to use them in calculations before they've been given any data.

    solution: read your texts to see how to declare variables and fill with data before trying to use them.
    Hmmm, I'm working on this one. I'm afraid I do not see where I've erred yet, but I am searching for how to declare these variable and have primitively come up with a non-solution... hahaha.
    Quote Originally Posted by Fubarable View Post
    Next, you've got some other big problems here:
    Java Code:
      if (type == "Radius");
        {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    For one you shouldn't use == to compare Strings because this tests to see if one variable refers to the same object as another variable and you aren't interested in this. Rather, you want to know if one String variable contains the same letters as another and for this you should use the String equals(...) or equalsIgnoreCase(...)
    method.
    This I very much understood and have taken it into consideration with my code! I temporarily alleviated some of the error messages with this in mind! Thank you so very much :) You'll notice that to fix this I declared another string and made the parameters "radius" or "diameter" or the like and then asked whether the user input was == to them in order to deduce which if statement to use - is this more kosher?

    Quote Originally Posted by Fubarable View Post
    Also, that semicolon at the end of your if block's boolean condition effectively short-ciruits the if block. What the if block above really does is this:
    Java Code:
      if (type == "Radius")
               ; // this empty statement will be conditionally peformed.
    
    // the block below is not part of your if block and so it will always be performed.
    {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
    }
    Solution, again use the equals method and get rid of the semicolon
    [/QUOTE]

    Thank you yet again!

    My present code:

    Java Code:
    import java.util.Scanner;
    public class IdentifyCircle
    {
      public static void main(String[] args)
      {
     
      final double pi = 3.14159;
      
      String radiustest = new String ("Radius");
      String diametertest = new String ("Diameter");
      String circumferencetest = new String ("Circumference");
      
      double areafromradius = pi * radiusdata * radiusdata;
      double areafromdiameter = pi * diameterdata;
      double areafromcircumference = circumferencedata / pi * 2;
      
      double radiusdata;
      double diameterdata;
      double circumferencedata;
        
      Scanner in = new Scanner(System.in);
      System.out.println("Radius, diameter, or circumference?");
      String type = in.nextLine();
      
      if (type == radiustest)
        {
        System.out.println("What is your radius?");
        double radiusdata = in.nextLine();
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    
      if (type == diametertest)
        {
        System.out.println("What is your diameter?");
        double diameterdata = in.nextLine();
        System.out.println("The area of a circle with radius " + diameterdata + " is " + areafromdiameter);
        }
    
      if (type == circumferencetest)
        {
        System.out.println("What is your circumference?");
        double circumferencedata = in.nextLine();
        System.out.println("The area of a circle with radius " + circumferencedata + " is " + areafromcircumference);
        }
    
      }
    }
    New errors :/ :
    Java Code:
    10 errors found:
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 13]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:13: cannot find symbol
    symbol  : variable radiusdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 13]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:13: cannot find symbol
    symbol  : variable radiusdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 14]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:14: cannot find symbol
    symbol  : variable diameterdata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 15]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:15: cannot find symbol
    symbol  : variable circumferencedata
    location: class IdentifyCircle
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 28]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:28: radiusdata is already defined in main(java.lang.String[])
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 28]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:28: incompatible types
    found   : java.lang.String
    required: double
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 35]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:35: diameterdata is already defined in main(java.lang.String[])
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 35]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:35: incompatible types
    found   : java.lang.String
    required: double
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 42]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:42: circumferencedata is already defined in main(java.lang.String[])
    File: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java  [line: 42]
    Error: /Users/pauljones/Documents/Education/2011/AP Computer Science/Java Files/IdentifyCircle.java:42: incompatible types
    found   : java.lang.String
    required: double
    I'm sorry if it's a little like talking with a brick wall :O, hahah.

  6. #6
    Join Date
    Aug 2010
    Posts
    16
    Rep Power
    0

    Default

    Use Math.PI to get the pi.
    Java Code:
    if (type == diametertest)
    He keeps saying use

    Java Code:
    if (type.equals(diametertest))
    You should make each word in a variable a capital unless it is the first.

    Java Code:
    String radiusTest = new String ("Radius");
    String diameterTest = new String ("Diameter");
    String circumferenceTest = new String ("Circumference");
    Couldn't you just have gone type.equals("Radius")? Infact as suggested above using type.equalsIgnoreCase("radius") would be much better in fact.

    About the diamater math....
    area != pi * d
    area = pi * (d*d)/(4)

    And isn't this right?
    area = (circum) * r/2 ?

    Also using shorthand as circumTest and diameterTest is alot easier to write (may not always be best to do though).

  7. #7
    paulmmj is offline Member
    Join Date
    Oct 2010
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Rectal Exambot View Post
    Use Math.PI to get the pi.
    Thank you for that information, as it is much cleaner method of using pi.

    Quote Originally Posted by Rectal Exambot View Post
    Java Code:
    if (type == diametertest)
    He keeps saying use

    Java Code:
    if (type.equals(diametertest))
    You should make each word in a variable a capital unless it is the first.

    Java Code:
    String radiusTest = new String ("Radius");
    String diameterTest = new String ("Diameter");
    String circumferenceTest = new String ("Circumference");
    Couldn't you just have gone type.equals("Radius")? Infact as suggested above using type.equalsIgnoreCase("radius") would be much better in fact.
    Thank you! My method of declaring another string and such assumed the user was going to capitalize, but with my limited knowlegde, I considered it a sacrifice I had to make. But this command is certainly more prudent.


    Quote Originally Posted by Rectal Exambot View Post
    About the diamater math....
    area != pi * d
    area = pi * (d*d)/(4)

    And isn't this right?
    area = (circum) * r/2 ?

    Also using shorthand as circumTest and diameterTest is alot easier to write (may not always be best to do though).
    [/quote]

    Yeah... I'm still working on that math :O

    Java Code:
    import java.util.Scanner;
    public class IdentifyCircle
    {
      public static void main(String[] args)
      {
      
      double radiusdata;
      double diameterdata;
      double circumferencedata;
      
      double areafromradius = Math.PI * radiusdata * radiusdata;
      double areafromdiameter = Math.PI * diameterdata;
      double areafromcircumference = circumferencedata / Math.PI * 2;
        
      Scanner in = new Scanner(System.in);
      System.out.println("Radius, diameter, or circumference?");
      String type = in.nextLine();
      
      if (type.equalsIgnoreCase("radius"))
        {
        System.out.println("What is your radius?");
        radiusdata = in.nextLine();    
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    
      if (type.equalsIgnoreCase("diameter"))
        {
        System.out.println("What is your diameter?");
        diameterdata = in.nextLine();
        System.out.println("The area of a circle with radius " + diameterdata + " is " + areafromdiameter);
        }
    
      if (type.equalsIgnoreCase("circumference"))
        {
        System.out.println("What is your circumference?");
        circumferencedata = in.nextLine();
        System.out.println("The area of a circle with radius " + circumferencedata + " is " + areafromcircumference);
        }
    
      }
    }
    Only three errors left! :)

    Java Code:
    3 errors found:
    File: E:\IdentifyCircle.java  [line: 22]
    Error: Type mismatch: cannot convert from java.lang.String to double
    File: E:\IdentifyCircle.java  [line: 29]
    Error: Type mismatch: cannot convert from java.lang.String to double
    File: E:\IdentifyCircle.java  [line: 36]
    Error: Type mismatch: cannot convert from java.lang.String to double
    Why does the compiler think I am using a String? I declared those variables as doubles... I feel so close :D
    Last edited by paulmmj; 10-12-2010 at 04:35 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're still trying to calculate things before the user has given you any data. For example:

    Java Code:
      double radiusdata;
      double diameterdata;
      double circumferencedata;
      
      // ***** [color="red"]here you do your calculations [/color]*****
      double areafromradius = Math.PI * radiusdata * radiusdata;
      double areafromdiameter = Math.PI * diameterdata;
      double areafromcircumference = circumferencedata / Math.PI * 2;
    
      // ***** [color="red"]and here you get the user input  [/color]*****
      Scanner in = new Scanner(System.in);
      System.out.println("Radius, diameter, or circumference?");
      String type = in.nextLine();
    
      if (type.equalsIgnoreCase("radius"))
        {
        System.out.println("What is your radius?");
        radiusdata = in.nextLine();    
        System.out.println("The area of a circle with radius " + radiusdata + " is " + areafromradius);
        }
    It should logically make sense that you have to have the numbers first before you do your calculations, right? Programming is all about logic, about planning what you want your program to do before typing it in. If it doesn't make sense, it probably isn't going to work.

    Also to parse a double from a String, use the Double.parseDouble(myString) method.

Similar Threads

  1. Trying to write my own program : /
    By jdicerch in forum New To Java
    Replies: 50
    Last Post: 09-26-2010, 02:44 AM
  2. How to write numbers around a circle
    By pheonix in forum New To Java
    Replies: 8
    Last Post: 06-11-2009, 11:20 AM
  3. java program help. find the radius of a circle
    By megironi in forum New To Java
    Replies: 1
    Last Post: 03-27-2009, 08:09 AM
  4. is it possible to write program with out thread
    By makpandian in forum Threads and Synchronization
    Replies: 3
    Last Post: 12-21-2008, 06:41 PM
  5. need help to write Program in JAVA
    By maneibr in forum New To Java
    Replies: 1
    Last Post: 03-13-2008, 02:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •