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

Thread: Can't find solution for error in my code..

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Can't find solution for error in my code..

    Here is my code

    /**
     * Write a description of class Student here.
     * 
     * @author ...
     * @version ...
     */
    import java.util.Scanner;
     
    public class Student
    {
        //declare instance data
        private String name;
        private int test1;
        private int test2; 
        private int average;
     
        //constructor
        //-----------------------------------------------
        public Student(String studentName)
        {
            //add body of constructor
            studentName = name;               
        }
        //-----------------------------------------------
        //inputGrades: prompt for and read in student's grades for test1
        //and test2.
        //Use name in prompts, e.g., "Enter's Joe's score for test1".
        //-----------------------------------------------
        public void inputGrades()
        {
            //add body of inputGrades
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the score for test 1: ");
            test1 = in.nextInt();
     
            System.out.println("Enter the score for test 2: ");
            test2 = in.nextInt();
        }
        //-----------------------------------------------
        //getAverage: compute and return the student's test average
        //-----------------------------------------------
        //add header for getAverage
        public double getAverage()
        {
            //add body of getAverage
            average = (test1 + test2) / 2.0;
            return average;
        }
        //-----------------------------------------------
        //getName: return the student's name
        //-----------------------------------------------
        //add header for getName
        public String getName()
        {    
            //add body of getName
            return name;        
        }
     
        public void getGrade()
        {
            if(average <= 100)  {System.out.println("The grade for" + name + "is A!");}
            else if(average <= 89) {System.out.println("The grade for" + name + "is B!");}
            else if(average <= 79) {System.out.println("The grade for" + name + "is C!");}
            else if(average <= 69 && average >= 60) {System.out.println("The grade for" + name + "is D!");}
            else {System.out.println("The grade for" + name + "is F!");}
        }
     
    }


    The problem is with the method getAverage.. Assignment says that I gotta use double or float instead of int variable to get a decimal number, but when I use double or float compiler says that it is precision error, and that it is better to use int. So, what to do here?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't find solution for error in my code..

    compiler says that it is precision error,
    Can you copy the full text of the error message and post it here.

    If you understand what precision may be lost and don't care, you can cast the value to tell the compiler that you know what you are doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't find solution for error in my code..

    possible loss of precision
    required: int; found: double

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't find solution for error in my code..

    Where is the rest of the error message showing the line of source code where the error happens?
    Did you try using a cast?
    int x = (int)1.234;
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Can't find solution for error in my code..

    public double getAverage()
        {
            //add body of getAverage
            average = (test1 + test2) / 2.0;
            return average;
        }

    It shows that error happens average = (test1 + test2) / 2.0

    I didn't try that, and actually don't understand what is that hehe

    --- Update ---

    I got it.. Mistake was in declaration.. I declare it as int so it couldn't recognize it.. Thank you anyways

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't find solution for error in my code..

    I got it.
    I've marked this as solved.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Cannot find symbol error in my java code
    By haliza hadi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 07:20 PM
  2. Cannot find Error in Beginner code!
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 2nd, 2012, 08:44 PM
  3. Code won't compile, but can't find the error....
    By RockDoc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 02:09 AM
  4. Solution for error message: Bad operand for binary operator '-'
    By MostinCredible21 in forum AWT / Java Swing
    Replies: 8
    Last Post: September 7th, 2011, 04:28 PM
  5. Need help, cannot find error
    By Imeri0n in forum What's Wrong With My Code?
    Replies: 13
    Last Post: December 5th, 2010, 05:26 PM