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: Sorting and returning a method.. Help.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Sorting and returning a method.. Help.

    My assignment is to be able to input 3 numbers and sort them from lowest to highest, but I'm not quite sure if I'm doing this right and no idea how to return the method. Please help and thank you in advance!
    //sort 3 numbers using a method (lowest to highest)
    import java.util.Scanner;
     
    public class Sort_Numbers {
    	public static void main(String[] args) {
     
    	Scanner input = new Scanner(System.in);
     
    	//User enters numbers
    	System.out.print("The three numbers from lowest to highest");
    	displaySortedNumbers(0.0);
    	}
     
    //method
    	public static double displaySortedNumbers (double num1, double num2, double num3) {
     
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter three numbers: ");
    			num1 = input.nextDouble();
    			num2 = input.nextDouble();
    			num3 = input.nextDouble();		
     
    		if (num1 > num2 && num1 > num3) {
    			System.out.println(num1);
    		}
    		else if (num2 > num1 && num2 > num3) {
    			System.out.println(num2);
    		}
    		else if (num3 > num1 && num3 > num2) {
    			System.out.println(num3);
    		}
    		return 0.0;
    	}
    }


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

    Default Re: Sorting and returning a method.. Help.

    how to return the method
    There is no way to return a method. You can return an object of a class that contains that method.

    not quite sure if I'm doing this right
    Test the code with different combinations of 3 numbers and see if it does what you want.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and returning a method.. Help.

    Alright, so I input three numbers like it shows in a video, but all it does is show the System.out that I wrote in before that
    	//User enters numbers
    	System.out.print("The three numbers from lowest to highest are: ");
    	displaySortedNumbers(15, 62, 1);
    but I also changed my code around a little, so here's the rest
    //sort 3 numbers using a method (lowest to highest)
    import java.util.Scanner;
     
    public class Sort_Numbers {
    	public static void main(String[] args) {
     
    	Scanner input = new Scanner(System.in);
    	//User enters numbers
    	System.out.print("The three numbers from lowest to highest are: ");
    	displaySortedNumbers(15, 62, 1);
    	}
     
    //non-return method
    	public static void displaySortedNumbers (double num1, double num2, double num3) {	
     
    		if (num1 > num2) {
    			double temp = num1;
    			num1 = num2;
    			num2 = temp;			
    		}
    		if (num2 > num3) {
    			double temp = num2;
    			num2 = num3;
    			num3 = temp;
    		}
    		if (num1 > num2) {
    			double temp = num1;
    			num1 = num2;
    			num2 = temp;			
    		}
     
    	}
    }

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

    Default Re: Sorting and returning a method.. Help.

    Where is there any print statements in that code to print out the results?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Elyril (March 6th, 2014)

  6. #5
    Member
    Join Date
    Feb 2014
    Posts
    58
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default Re: Sorting and returning a method.. Help.

    Thank you so much! I totally missed that..

Similar Threads

  1. Returning value from method? Arrays-
    By TSSF44 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 27th, 2013, 10:25 PM
  2. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  3. [SOLVED] Help with method returning a double
    By Mike_Chase in forum What's Wrong With My Code?
    Replies: 9
    Last Post: July 22nd, 2011, 01:09 AM
  4. Method returning 0 for everything
    By JJTierney in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2010, 08:51 PM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM

Tags for this Thread