Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6651721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:02:42+00:00 2026-05-26T01:02:42+00:00

How would I return my x and y variable into main in order to

  • 0

How would I return my x and y variable into main in order to perform the addition?
Thanks in advance for the help!

import java.util.Scanner;

public class calling {

    public static int x;
    public static int y;

    public static void num1() {
        Scanner scanner = new Scanner (System.in);
        System.out.println("Please enter a number: ");
        x=scanner.nextInt();    
    }

    public static void num2() {
        Scanner scanner = new Scanner (System.in);
        System.out.println("Please enter a second number: ");
        y=scanner.nextInt();    
    }

    public static void main(String[] args){     **//place to return variables.**

        num1();
        num2();
        System.out.print("The sum of the two numbers is: " + (x+y));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:02:42+00:00Added an answer on May 26, 2026 at 1:02 am

    You shouldn’t be using static data members or methods – those should only be used when the member of method applies to all instances of a class.

    In this case the only static method should be main and that should only create a single instance of the current class. main should almost never do any real work – although in this trivial example I suppose an exception could be made.

    You should ideally also only create a single Scanner object – the Scanner class is perfectly capable of reading a continuous stream of numbers without you needing to create a new one over and over for each number to be read.

    In the code below I just create one and use it twice directly.

    import java.util.Scanner;
    
    public class calling {
    
        public calling() {
            Scanner scanner = new Scanner (System.in);
    
            System.out.print("Please enter a number: ");
            int x = scanner.nextInt();
    
            System.out.print("Please enter a second number: ");
            int y = scanner.nextInt();
    
            System.out.println("The sum of the two numbers is: " + (x + y));
        }
    
        public static void main(String[] args) {
            new calling();
        }
    }
    

    Alternatively the scanner object could have been stored as a member variable, as in this example:

    import java.util.Scanner;
    
    public class calling {
    
        private Scanner scanner = null;
    
        private int getInt(String prompt) {
            System.out.print(prompt);
            return scanner.nextInt();
        }
    
        public calling() {
            scanner = new Scanner(System.in)
            int x = getInt("Please enter a number: ");
            int y = getInt("Please enter a second number: ");
            System.out.println("The sum of the two numbers is: " + (x + y));
        }
    
        public static void main(String[] args) {
            new calling();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I do a join that would return me results similar to- SELECT
I need an xpath expression that would return the value of I need to
I was wondering what the parameters from this method would return. - (void) observeValueForKeyPath:(NSString
How can I build a function slice(x, n) which would return a list of
It has always seemed strange to me that random.randint(a, b) would return an integer
Not sure this is possible, but looking to write a script that would return
In python, you can concatenate boolean values, and it would return an integer. Example:
How would I return multiple values (say, a number and a string) from a
Anyone have any idea why shuffle() would only return 1 item? when using: $array2
Given this example, how would I return the result of the equation rather than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.