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

Thread: int from main method to non-static method

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default int from main method to non-static method

    I'm working on a school assignment, and I've hit a wall in my code where I can't get my Int and double from my main method to my nonstatic method. in the one instance where I did manage to get no errors and everything looked correct, nothing happened and it was as if I had done nothing at all.
    My code:
    ```
    import java.util.Scanner;
    public class VendingChange {
    /**
    * Vending extra credit
    * by:
    *
    * Requirements:
    * 1) The program must use Scanner in order to grab what the user types in
    * 2) You must have at least 2 methods, one static, and one non-static
    * 3) The main method should be used to ask the user for the price of the item and the non-static method should be used to calculate the amount of change and to print the results.
    * 4) Make sure you have a javadoc and single line comments throughout the program
    *
    */
    public static void main (String[] args){
    Scanner check = new Scanner(System.in);// import of the scanner
    char cents = '\u00a2';//Unicode of cent symbol
    System.out.print("Your item costs (25" + cents + " minimum, Increments of 5" + cents + "): ");
    Double price = check.nextDouble(); //price of the item
    System.out.println("You paid (whole dollars only): ");
    int paid = check.nextInt(); //amount paid
    VendingChange newVend = new VendingChange();//creates a copy of the class
    newVend.Secondary();// calls the nonstatic
    }
    public static void trianary(){
    main(null);

    }
    public void Secondary () {
    System.out.println("your change is " );
    }
    }
    ```
    I can't figure out how I'm supposed to connect my methods.

  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: int from main method to non-static method

    Are you getting compiler error messages? Please copy the full text and paste it here.

    how I'm supposed to connect my methods.
    To call a method in a class, create an instance of the class and use the reference to that instance to call the method.
    Scanner check = new Scanner(System.in);// create instance of Scanner class
     ...
    Double price = check.nextDouble(); // call a method in the Scanner class


    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: int from main method to non-static method

    learn about method parameters

        newVend.Secondary( price, paid);
      }
      public void Secondary( Double price, int paid) {
        System.out.println("price is " +price);
        System.out.println("paid "  +paid);
      }
    Last edited by zemiak; January 5th, 2022 at 08:43 AM. Reason: typo

Similar Threads

  1. Replies: 3
    Last Post: March 30th, 2019, 09:06 AM
  2. Replies: 2
    Last Post: May 27th, 2014, 12:36 PM
  3. Calling A Public Static Void Method from Main
    By Clubs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2013, 02:29 AM
  4. How to call a static method within a static method in the same class?
    By EDale in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2013, 04:13 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM