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

Thread: Help! Whats wrong with my code?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Help! Whats wrong with my code?

    Hello I'm a beginner java student and can't figure out what I'm doing wrong.

    Write the following methods and provide a program to test them

    A. double smallest(double x, double y, double z), returning the smallest of the arguments
    B. double average(double x, double y, double z), returning the average of the arguments

    this is what I have...

     
    import java.util.Scanner;
    public class SmallestAndAverageCh5
    {
     
       public static main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          System.put.print("Please enter three different numbers between 1 and 10:");
          double x = in.nextDouble();
          double y = in.nextDouble();
          double z = in.nextDouble();
     
          double smResult = smallest;
          double avgResult = average;
          System.out.print("The smallest value you entered is " + smResult);
          System.out.print("The average value of the numvers you entered is " + avgResult);
     
       } 
     
       public static double smallest(double x, double y, double z)   
       {
          if (x < y && x < z)
           {
             return x;
     
             else if (y < x && y < z)
              {return y;
              }
     
             else (z < x && z < y)
              {return z;
              }
     
           }
       }
       public static double average(double x, double y, double z)
       {
          double average = (x + y + z)/3;
          return average;
       }
     
    }
    Last edited by Carolineawood; October 5th, 2014 at 06:38 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help! Whats wrong with my code?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    what I'm doing wrong.
    Please explain why you think you are doing something wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help! Whats wrong with my code?

    Firstly, the 'main' method declaration is wrong. It has to be public static void main. You missed void

    --- Update ---

    else should not have condition, else (z < x && z < y)
    {return z;
    }

    It should be just else { }

  4. The Following User Says Thank You to divdat For This Useful Post:

    Carolineawood (October 5th, 2014)

  5. #4
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Whats wrong with my code?

    Thank you! I now only have one error and its with the else if this is what its saying...

    ----jGRASP exec: javac -g SmallestAndAverageCh5.java
    SmallestAndAverageCh5.java:26: error: 'else' without 'if'
    else if (y < x && y < z)
    ^
    1 error

    ----jGRASP wedge: exit code for process is 1.
    ----jGRASP: operation complete.

    _______

    Quote Originally Posted by divdat View Post
    Firstly, the 'main' method declaration is wrong. It has to be public static void main. You missed void

    --- Update ---

    else should not have condition, else (z < x && z < y)
    {return z;
    }

    It should be just else { }

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help! Whats wrong with my code?

    'else' without 'if'
    The compiler can't find the if that goes with the else statement shown in the error message.

    The posted code needs to be enclosed in code tags to preserve its formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Whats wrong with my code?

    I think I am doing something wrong because I keep getting the same error message for my else if statement

     
    public static double smallest(double x, double y, double z) 
    {
    if (x < y && x < z)
    {
    return x;
     
    else if (y < x && y < z)
    {return y;
    }
     
    else
    {return z;
    }
     
    }


    the compiler is saying i have a problem with my else if (y < x && y < z) statement

  8. #7
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help! Whats wrong with my code?

    That might be because the 'if' is not having an ending '}'

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Help! Whats wrong with my code?

    The formatting of the posted code needs work. Properly formatted code makes it easier to see these kinds of problems.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help! Whats wrong with my code?

    thank you!
    i know have this

     
    import java.util.Scanner;
    public class SmallestAndAverageCh5
    {
     
       public static double smallest(double x, double y, double z) 
    {
       if (x < y && x < z)
       {
          return x;
       }
     
       else if (y < x && y < z)
       {
          return y;
       }
     
       else
       {
          return z;
       }
     
    }
     
     
    public static double average(double x, double y, double z)
    {
       double average = (x + y + z)/3;
       return average;
    }
     
     
       public static void main(String[] args)
       {
          Scanner in = new Scanner(System.in);
          System.out.print("Please enter three different numbers between 1 and 10:");
          double x = in.nextDouble();
          double y = in.nextDouble();
          double z = in.nextDouble();
     
          double smResult = smallest;
          double avgResult = average;
          System.out.print("The smallest value you entered is " + smResult);
          System.out.print("The average value of the numvers you entered is " + avgResult);
     
       } 
     
     
    }

    but now it is saying that it cannot this.... are my methods just done completely wrong?

    ----jGRASP exec: javac -g SmallestAndAverageCh5.java
    SmallestAndAverageCh5.java:40: error: cannot find symbol
    double smResult = smallest;
    ^
    symbol: variable smallest
    location: class SmallestAndAverageCh5
    SmallestAndAverageCh5.java:41: error: cannot find symbol
    double avgResult = average;
    ^
    symbol: variable average
    location: class SmallestAndAverageCh5
    2 errors

    ----jGRASP wedge: exit code for process is 1.
    ----jGRASP: operation complete.

  11. #10
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help! Whats wrong with my code?

    Your method calls are wrong, they must be
    double smResult = smallest(x,y,z);
    double avgResult = average(x,y,z);

Similar Threads

  1. [SOLVED] Help me on this code whats wrong with it?
    By Shoes Mosh in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 2nd, 2014, 09:16 AM
  2. Whats wrong with this code?????
    By bambam in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 16th, 2013, 11:22 PM
  3. Whats wrong with my code?
    By mhphucld in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2013, 08:50 PM
  4. Whats wrong with this code????
    By westberge in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 10th, 2012, 07:49 PM
  5. Whats wrong with my code??
    By mozza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 14th, 2012, 10:37 AM

Tags for this Thread