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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: my program for calculating the circumference and area isn't working

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my program for calculating the circumference and area isn't working

    Here is the program I wrote for calculating the circumference of a circle

    //circumference.java
    //program that calculates the area and circumference of a circle

    import java.util.Scanner; //program uses class scanner

    public class circumference{

    //main method begins execution of Java application

    public static void main(String args[]){

    //create scanner to obtain input from the command line

    Scanner input = new Scanner(System.in);

    double a; //declares Area
    double c; //declares circumference

    double r; //declares the radius

    System.out.printf("Enter the radius"); //prompt
    r=input.nextInt(); //read first number the user

    c=2*3.14*r;

    System.out.printf("The circumference is",c); //display circumference;

    }//end method main

    }//end class circumference
    Even though my code compiles and runs without any errors, the program doesn't calculate the circumference.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: my program for calculating the circumference and area isn't working

    System.out.printf("The circumference is",c); //display circumference;
    Where do you print the circumference? Perhaps read the API for this method...
    PrintStream (Java Platform SE 7 )
    ...or use System.out.print to print out the value.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program for calculating the circumference and area isn't working

    Actually, I wanted to add when I add %d\n in the line of my code that displays the circumference, I get a large error message when I run the code.

    --- Update ---

    Here is what my code now looks like :"
    //circumference.java
    //program that calculates the area and circumference of a circle

    import java.util.Scanner; //program uses class scanner

    public class circumference{

    //main method begins execution of Java application

    public static void main(String args[]){

    //create scanner to obtain input from the command line

    Scanner input = new Scanner(System.in);

    double a; //declares Area
    double c; //declares circumference

    double r; //declares the radius

    System.out.print("Enter the radius"); //prompt
    r=input.nextInt(); //read first number the user

    c=2*3.14*r;

    System.out.printf("The circumference is %d\n",c); //display circumference;

    }//end method main

    }//end class circumference
    Even though my code compiles correctly, when I run it and it ask me to enter a value for the radius, I get a large error message. I can't displayed the error message here because the windows command line will not allow me to copy my error message

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: my program for calculating the circumference and area isn't working

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code in code tags per the above link.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program for calculating the circumference and area isn't working

    Quote Originally Posted by GregBrannon View Post
    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code in code tags per the above link.
    Okay I will repost my last post :


    Actually, I wanted to add when I add %d\n in the line of my code that displays the circumference, I get a large error message when I run the code.

    --- Update ---

    Here is what my code now looks like :

     
    //circumference.java
    //program that calculates the area and circumference of a circle
     
    import java.util.Scanner; //program uses class scanner
     
    public class circumference{
     
    //main method begins execution of Java application
     
    public static void main(String args[]){
     
    //create scanner to obtain input from the command line
     
    Scanner input = new Scanner(System.in);
     
    double a; //declares Area
    double c; //declares circumference
     
    double r; //declares the radius
     
    System.out.print("Enter the radius"); //prompt
    r=input.nextInt(); //read first number the user
     
    c=2*3.14*r;
     
    System.out.printf("The circumference is %d\n",c); //display circumference;
     
    }//end method main
     
    }//end class circumference

    Even though my code compiles correctly, when I run it and it ask me to enter a value for the radius, I get a large error message. I can't displayed the error message here because the windows command line will not allow me to copy my error message

  6. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: my program for calculating the circumference and area isn't working

    System.out.printf("The circumference is %d\n",c); //display circumference;
    You are using the wrong formatter in the printf method. %d outputs integer types - not doubles.
    Read up on the printf formatter types.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: my program for calculating the circumference and area isn't working

    I can't displayed the error message here because the windows command line will not allow me to copy my error message
    See https://www.microsoft.com/resources/....mspx?mfr=true

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program for calculating the circumference and area isn't working

    Thanks here is the output of my program after entered the radius:


    Enter the radius8.90
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at circumference.main(circumference.java:22)
    Press any key to continue . . .

  9. #9
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: my program for calculating the circumference and area isn't working

    Quote Originally Posted by noblegas View Post
    Thanks here is the output of my program after entered the radius:
    Read the error message. It clearly says "Input Mismatch", so there is something wrong with the way you retrieve input from the user.
    Further down it tells us you use:
    java.util.Scanner.nextInt()
    But in the example you have shown us at the top you have inputted a double. That is clearly not compatible.

  10. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program for calculating the circumference and area isn't working

    You are using the wrong formatter in the printf method. %d outputs integer types - not doubles.
    Read up on the printf formatter types.
    Thanks Ada, for coming back to life just to help me out with my java program error. What is the printf formatter type for a double?

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: my program for calculating the circumference and area isn't working

    What did you find when you Googled 'java formatter'?

  12. #12
    Junior Member
    Join Date
    Apr 2014
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my program for calculating the circumference and area isn't working

    When I google 'java formatter' the 1st website that pops up is oracle . (Formatter (Java Platform SE 7 ))

    the formatter for 'double' is 'e' so I modify my code

    Here is what my code looks like now :

    /circumference.java
    //program that calculates the area and circumference of a circle
     
    import java.util.Scanner; //program uses class scanner
     
    public class circumference{
     
    	//main method begins execution of Java application
     
    	public static void main(String args[]){
     
    	//create scanner to obtain input from the command line
     
    	Scanner input = new Scanner(System.in);
     
    	double a; //declares Area
    	double c; //declares circumferenceFunny How Time Slips Away
     
    	double r; //declares the radius
     
    	System.out.print("Enter the radius"); //prompt
    	r=input.nextdouble(); //read first number the user
     
    	c=2*3.14*r;
     
    	System.out.printf("The circumference is %e\n",c); //display circumference;
     
    }//end method main
     
    }//end class circumference

    the code still won't compile correctly. What did I do wrong?

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: my program for calculating the circumference and area isn't working

    What compiler errors are you getting? Always post error messages in their entirety that you want help with.

  14. #14
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: my program for calculating the circumference and area isn't working

    I am pretty sure the %e would not cause a compiler error as it is a legal
    formatter type. Is that really the one you want to use? What do you
    think the 'e' actually stands for?

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Replies: 1
    Last Post: April 21st, 2014, 04:58 AM
  2. Don't know why this isn't working
    By Eucibous in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 24th, 2013, 04:35 PM
  3. Why isn't my collision working?
    By dan0194 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 5th, 2013, 01:57 AM
  4. Replies: 1
    Last Post: February 10th, 2012, 07:01 AM
  5. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM