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

Thread: Problem with Return Function

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Return Function

    Can someone please tell me how to keep this program the same with the 3 functions but instead of the sum of the 2 numbers I need it to tell me if the 1st number is <> or equal to the second number? I cant figure out how to change it in the 2nd and 3rd function so it returns it and displays it. I've been stuck for hours on something that is probaly simple.
    public static void main(String[] args) {
     
    		int num3;
    		int num4;	
    		int sum;
     
    		//get the first number
    		num3 = getNum(); 
    		//get the second number
    		num4 = getNum(); 
     
    		//get the sum of the two numbers
    		sum = getSum(num3, num4);
     
    		//Show the result
    		showResult(sum); 		
    	}
     
    	//Read a number from the keyboard and return it.
    	public static int getNum()
    	{
    		//for getting an input from the keyboard
    		Scanner input = new Scanner(System.in);
     
    		//An inputted number will be stored in this variable	
    		int num;
     
    		//prompt the user to enter a number
    		System.out.println("Enter a number: ");
     
    		//get a number from the keyboard and it is stored in num
    		num = input.nextInt();
     
    		//send back the inputted number
    		return num;	
    	}
     
    	//
    	public static int getSum(int num3, int num4)
    	{
    		//the sum will be stored in this variable
    		int sum;
     
    		//add num1 and num2 and the result is stored in sum
    		sum = num3 + num4;
     
    		//send back the sum
    		return sum;	
    	}
     
    	//Show the result	
    	public static void showResult(int sum)
    	{
    		System.out.println("The sum of the two numbers is " + sum);
    		System.out.println("Thank you");
    	}
    Last edited by Tracy22; October 25th, 2010 at 11:27 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with Return Function

    Please surround your code with highlight tags. See my signature for how to do this.

    Think about this mathematically:

    given two numbers a and b, you can determine which how the two numbers are related to each other by subtracting them. If you know which number is the first one, you are relating the second number to the first one. If the result is greater than 0, then that means the first number is larger than the second number. If it's lower than 0, then that means the second number is bigger than the first number. If the result is exactly 0, then the two numbers are equal.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with Return Function

    Hi yes I know the mathematical part, but how can i put it in the return function. All I need is to know how to separate the functions so that one will prompt the user to enter number, read a number from the keyboard and return it. The last function will display the result. I only know how to do it all under one function instead of separate. This example I also only did one number but the user will enter 2 numbers and have them compared to see which is greater or less.
    If(num < 0)
    	{
    		System.out.println(num + " is negative");
    	}
    else if(num == 0)
    	{
    		System.out.println(num + " is zero");	
    	}
    else //Any other numbers should be positive
    	{
    		System.out.println(num + " is positive");	
    	}
    .
    Last edited by Tracy22; October 25th, 2010 at 11:37 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with Return Function

    Have a compareTo method, which will return the difference of the two numbers. Then use the above logic in your showResult method. Your getNum method doesn't need to be changed.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with Return Function

    Quote Originally Posted by Tracy22 View Post
    Can someone please tell me how to keep this program the same with the 3 functions but instead of the sum of the 2 numbers I need it to tell me if the 1st number is <> or equal to the second number? I cant figure out how to change it in the 2nd and 3rd function so it returns it and displays it. I've been stuck for hours on something that is probaly simple.
    public static void main(String[] args) {
     
    		int num3;
    		int num4;	
    		int sum;
     
    		//get the first number
    		num3 = getNum(); 
    		//get the second number
    		num4 = getNum(); 
     
    		//get the sum of the two numbers
    		sum = getSum(num3, num4);
     
    		//Show the result
    		showResult(sum); 		
    	}
     
    	//Read a number from the keyboard and return it.
    	public static int getNum()
    	{
    		//for getting an input from the keyboard
    		Scanner input = new Scanner(System.in);
     
    		//An inputted number will be stored in this variable	
    		int num;
     
    		//prompt the user to enter a number
    		System.out.println("Enter a number: ");
     
    		//get a number from the keyboard and it is stored in num
    		num = input.nextInt();
     
    		//send back the inputted number
    		return num;	
    	}
     
    	//
    	public static int getSum(int num3, int num4)
    	{
    		//the sum will be stored in this variable
    		int sum;
     
    		//add num1 and num2 and the result is stored in sum
    		sum = num3 + num4;
     
    		//send back the sum
    		return sum;	
    	}
     
    	//Show the result	
    	public static void showResult(int sum)
    	{
    		System.out.println("The sum of the two numbers is " + sum);
    		System.out.println("Thank you");
    	}
    Actually, I'm not sure if you're trying to get the same sum that you got in method getSum(int i, int j) and showing it in show result. If so, you'll need to have the two values that you gave it as parameters when you put, getSum(), which you should do if the sum you're passing it is supposed to be the same sum that you're returning from getSum().

    If so, you could do this:
    public static void showResult(int num1, int num2)
    	{
    		System.out.println("The sum of " + num1 + " and " + num2 + "  is " + getSum(num1,num2));
    		System.out.println("Thank you");
    	}

Similar Threads

  1. Need Help on the Interface Function
    By yel_hiei in forum Object Oriented Programming
    Replies: 12
    Last Post: July 29th, 2010, 07:27 AM
  2. [SOLVED] Problem accessing specific data in an array and getting it to return properly
    By Universalsoldja in forum Collections and Generics
    Replies: 3
    Last Post: February 4th, 2010, 04:26 PM
  3. Substring function
    By bristol580 in forum Java SE APIs
    Replies: 2
    Last Post: November 12th, 2009, 11:29 AM
  4. Function of difference between two numbers
    By uplink600 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 13th, 2009, 05:57 AM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM