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

Thread: Help with a varibles!

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Help with a varibles!

    Hi all,
    I need help in this i have a mail class and a varable called commision( commision is calculated value). i need move this value to another class to print it. but i no have the form.
    Main class code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Neowolf
     */
    import java.util.Scanner;
    public class Week2 {
     
        /**
         * @param args the command line arguments
         *
         */
        double link;
        public double showInfo()
        {
               	   return link;
       	}    
        public static void main(String[] args) {
            // TODO code application logic here
            Week2class myWeek2class = new Week2class();//call the class Week2class
            Scanner input = new Scanner(System.in);//create the input variable from the Scanner
            int AnnualSales;//declare variables
            int Salary;
            double Commision;
     
            // Validate the input is a integer and a positive value
            do {
            System.out.println("Please enter a Annual Sales");
            while (!input.hasNextInt()) {
                System.out.println("That's not a valid value!");
                input.next(); // this is important!
            }
            AnnualSales = input.nextInt();
        } while (AnnualSales <= 0);
     // Validate the input is a integer and a positive value           
         do {
            System.out.println("Please enter a Salary");
            while (!input.hasNextInt()) {
                System.out.println("That's not a valid value!");
               input.next(); // this is important!
            }
            Salary = input.nextInt();
        } while (Salary <= 0);
                Commision = Salary + (AnnualSales * .08);
                System.out.print(Commision);
                        myWeek2class.displayMessage();
        }
    }

    And the subclass:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Neowolf
     */
    public class Week2class {
      public void displayMessage()
      {
     
        Week2 Commision = new Week2();
       System.out.print("Commision and Salary Total is $");
            System.out.println(Commision.showInfo());   
      }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with a varibles!

    What you have coded, you should instantiate the "link" variable in your constructor of the Week2 class. The way you have it set up now, it is just returning a null value because you never assigned link a value.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    oka how i can properly link ?
    you can write me the example code?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    Something like this:
    link = <The value your want showInfo to return>;
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: Help with a varibles!

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/60378-pleas-anyone-can-help-me.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    Something like this:
    link = <The value your want showInfo to return>;
    if i use link = Commision i get a error

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    i get a error
    Please post the full text of the error message.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by copeg View Post
    This thread has been cross posted here:

    http://www.java-forums.org/new-java/60378-pleas-anyone-can-help-me.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    javaforum and java programing forums are the same?

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    Please post the full text of the error message.
    non-static variable link cannot be referenced from a static context
    and Norm thanks for help me!!!!
    thanks!!

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    non-static variable link cannot be referenced from a static context
    Move the code out of the static method to a non-static method and there will not be a problem.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    Move the code out of the static method to a non-static method and there will not be a problem.
    but i not get the value!

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    Move all the code out of the main() method to the class's constructor. Have the main() method call the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    Move all the code out of the main() method to the class's constructor. Have the main() method call the constructor.
    You can show me where and how? Please

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    Can you find the main() method?
    Can you find the constructor for the class? Add one if needed.
    Select and cut all the code inside of the main() method.
    Paste the code into the constructor.
    Add a call to the constructor in the main() method: new Week2();
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    Can you find the main() method?
    Can you find the constructor for the class? Add one if needed.
    Select and cut all the code inside of the main() method.
    Paste the code into the constructor.
    Add a call to the constructor in the main() method: new Week2();
    oka, i am to newbie to understand you, however thanks to try to help me bro is too appreciated!!!!

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    If you can not answer the questions I asked you need to spend some more time reading the java basics.
    Which of the questions I asked don't you understand?
    Do you know what a method is?
    Can you find the main() method in your code?

    Do you know what a class's constructor is?
    Can you find the constructor in your class?

    I assume you know how to select, cut and paste text.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    If you can not answer the questions I asked you need to spend some more time reading the java basics.
    Which of the questions I asked don't you understand?
    Do you know what a method is?
    Can you find the main() method in your code?

    Do you know what a class's constructor is?
    Can you find the constructor in your class?

    I assume you know how to select, cut and paste text.
    yep i select and cut all and when paste out of the public static void main(String[] args) to other site i get 200 errors . i not understand where i need move or how i can create a constructor. I am beginner and is my first class of java in the university. for the moment i understand class and objects. Is my fault !!!!

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,242
    Thanks
    52
    Thanked 2,298 Times in 2,269 Posts

    Default Re: Help with a varibles!

    how i can create a constructor.
    That is very important to know. All classes have one or more constructors.
    for the moment i understand class
    Go back to your lessons about classes and find where it tells you how to write a constructor for a class.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with a varibles!

    Quote Originally Posted by Norm View Post
    That is very important to know. All classes have one or more constructors.

    Go back to your lessons about classes and find where it tells you how to write a constructor for a class.
    Oka thanks

Similar Threads

  1. adding varibles to array and using Jlist
    By Fantasy in forum Collections and Generics
    Replies: 1
    Last Post: November 24th, 2011, 05:19 AM
  2. Java operaton on boolean varibles
    By big_c in forum Java Theory & Questions
    Replies: 5
    Last Post: May 12th, 2009, 04:40 AM