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 8 of 8

Thread: Code only outputs 1's

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Code only outputs 1's

    EDIT: All Fixed
    Last edited by LoganC; September 27th, 2012 at 11:38 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Code only outputs 1's

    Quote Originally Posted by LoganC View Post
    The area is only showing up as zero even when different coordinates are used.
    Are you sure? What does your test run show? A sample run would show: Area of Circle is 1.0

    For your code sample take the role of the computer running your program, and go through it.

    (init happens)...
    main is called..
    (some variabels declared)...
    double circOutput; //declared
    (more variable work)...
    circOutput = 1; //value set
    (more work, 4 numbers input)...
    System.out.print("Area of Circle is "); // Area of Circle is
    System.out.println(circOutput); // 1.0 //value read

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code only outputs 1's

    Yes I mistyped that. It is showing up as Area of circle is 1.0 but that is not the correct answer, i have circOutput set to an equation that shouldn't equal zero. Im confused on what to do and why circOutput is equalling zero.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Code only outputs 1's

    Quote Originally Posted by LoganC View Post
    i have circOutput set to an equation that shouldn't equal zero.
    You have a parameter named circOutput in a method that is never called and never used. But that localized variable is only visible within the method and shadows the visibility of your instance variable with the same name.





    Quote Originally Posted by LoganC View Post
    Im confused on what to do and why circOutput is equalling zero.
    Read my last post. Do like it says and play the role of a computer running the program. Step through the program one line at a time and see what takes place.

    In my previous post I stepped through your program and made a list of notes as I went showing you what happens and why 1.0 is printed every time. I did not explain why, but that does show you the problem.

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Code only outputs 1's

    I'd like to recommend looking into scope and visibility. I think you should try looking at this website and hopefully it'll solve your problem. Scope and Visibility in Java

    *edit* Well I guess I took too long trying to type up a response. Pretty much listen to jps

  6. #6
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code only outputs 1's

    I believe I am close, now I just cant get main to read what theAnswer is being computed as. The calculations work, but the area is dependant on what theAnswer is set to instead of what the user inputs.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package exercisefiveone;
     
    /**
     *
     * @author student
     */
    import java.util.Scanner;
    public class Exercisefiveone {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            double dist = distance (1.0, 2.0, 4.0, 6.0);
            double area = area (1.0, 2.0, 4.0, 6.0);
            double radius;
            double theAnswer;
            theAnswer = 1;
     
     
            Scanner reader = new Scanner (System.in);
     
            System.out.println("Radius of Circle?");
            radius = reader.nextInt();
     
     
     
            System.out.println(area(theAnswer));
     
            area (radius);
     
        }
        public static double distance (double x1, double y1, double x2, double y2)
        {
         double dx = x2 - x1;
         double dy = y2 - y1;
         double dsquared = dx*dx + dy*dy;
         double result = Math.sqrt (dsquared);
     
            return result;
        }
        public static double area (double dx, double yc, double xp, double yp)
        {
            double radius = distance (dx, yc, xp, yp);
            double circArea = Math.PI*radius; 
            return circArea;
     
        }
            public static double area (double radius)
        {
            double theAnswer = Math.PI*radius;
            return theAnswer;
     
        }
     
     
    }

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: Code only outputs 1's

    Why exactly are you using theAnswer in the main? Think about what the input is for your area method. When you tested for your other methods, how did you test for them? Did you try printing out area(circArea)? or did you try something different?

  8. #8
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Code only outputs 1's

    I am using theAnswer in main for the output at the bottom of main. Which area method are you referring to? I have two.

Similar Threads

  1. Program that selectively outputs data from a text file.
    By MJjavapf in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 23rd, 2012, 05:26 PM
  2. Java program is running .. But I am not getting my outputs in the JSP
    By sankarravi6 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 30th, 2012, 08:01 PM
  3. Is there a way I could create/get a program that outputs a form in BBC?
    By Elite Cow in forum Java Theory & Questions
    Replies: 5
    Last Post: October 11th, 2011, 02:01 PM
  4. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 05:57 AM
  5. New to Java Can not figure out how to create a table with my codes outputs!
    By shoppa028 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 10:21 AM