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

Thread: Check for larger num problem

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Check for larger num problem

    the problem is as follows:

    Create a main class file. (1) Inside create a method max() that has two floating point
    parameters x and y and returns the larger of x and y; (2) In the main method, input two
    numbers from the keyboard, and then use the max() method you created to compute the
    larger value and output it.

    this is what I have so far but am confused on how to get my max method to return the larger sum
     
     
    import java.util.Scanner;
    public class LargerNum {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter two values: ");
    		double FirstNum = input.nextDouble();
    		double SecondNum = input.nextDouble();
    		double Final = MaxMethod(FirstNum, SecondNum);
     
    		System.out.println(Final);
     
    	}
    	public static double MaxMethod(double L, double M){
    		double Temp = L;
    		double TempTwo = M;
    		double Large;
    		if (Temp >= TempTwo);
    			Large = Temp;
    		if (TempTwo >=Temp);
    			Large = TempTwo;
     
    			return Large;
    }}
    I want the larger of the two to be assigned to Large but then how do i pass that back to the top so i can print it[COLOR="Silver"]
     
    --- Update ---
     
    [/COLOR]import java.util.Scanner;
    public class LargerNum {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter two values: ");
    		double FirstNum = input.nextDouble();
    		double SecondNum = input.nextDouble();
    		double Final = MaxMethod(FirstNum, SecondNum);
     
     
     
    	}
    	public static double MaxMethod(double L, double M){
    		double Temp = L;
    		double TempTwo = M;
    		double Large;
    		if (Temp >= TempTwo){
    			Large = Temp;}
    		else;
     
    			Large = TempTwo;
    			System.out.println(Large);
    			return Large;
     
    }}


    closer but not there yet please help the second number entered is printed needs to be the larger of the two


    in output your asked to enter 2 numbers so say: 9 and 7, 7 will be returned whereas 9 should be returned


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    how to get my max method to return the larger sum
    Use some logic like an if statement to find the larger sum and return it.

    Post the contents of the command prompt window that shows what the code does when executed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for larger num problem

    import java.util.Scanner;
    public class LargerNum {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter two values: ");
    		double FirstNum = input.nextDouble();
    		double SecondNum = input.nextDouble();
    		double Final = MaxMethod(FirstNum, SecondNum);
     
     
     
    	}
    	public static double MaxMethod(double L, double M){
    		double Temp = L;
    		double TempTwo = M;
    		double Large;
    		if (Temp >= TempTwo){
    			Large = Temp;}
    		else
    			Large = TempTwo;
    			System.out.println(Large);
    			return Large;
     
    }}

    So it works, but its not doing what it needs to.right now the println is in the max method but it should be in the main method. I dont know how to send the larger value determined from the maxmethod back to the main method so that it can be printed. I have in the max method the final number is to be assigned to the Larger variable, but how do i sent the value of the Larger variable back to the main method.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    A problem I see is that the code does not enclose the statements following the else statement in{}s.
    Another problem is the hidden } at the end of a statement. Hiding }s makes the code hard to read and understand.
    Put the }s on a line by themselves.

    how do i sent the value of the Larger variable back to the main method.
    Use the return statement with the value to be returned.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for larger num problem

    at the end of the else statement I have return Large which is the larger of the two numbers. So the return Large; line should send the value back to the main method right? so how do i use the value of Large in the main method it wont let me use that variable.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    double Final = MaxMethod(FirstNum, SecondNum);
    The value returned by the method is saved in the variable: Final

    BTW method names and variable names should start with lowercase letters
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for larger num problem

    hah! thanks Norm for pointing that out, looks like I was closer than I thought! so here is final code please check:


    import java.util.Scanner;
    public class LargerNum {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter two values: ");
    		double FirstNum = input.nextDouble();
    		double SecondNum = input.nextDouble();
    		double Final = MaxMethod(FirstNum, SecondNum);
    		System.out.println( (int)Final + " is the larger of the two numbers.");
     
     
    	}
    	public static double MaxMethod(double L, double M){
    		double Temp = L;
    		double TempTwo = M;
    		double Large;
    		if (Temp >= TempTwo){
    			Large = Temp;}
    		else
    			Large = TempTwo;
     
    			return Large;
     
    }
    	}

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    Does it compile, execute and give the right answer?
    Copying values from L to temp is not necessary. Use the variables: L & M in the following if statements.
    The variable names should be changed from L & M to something like: first & second

    The code doesn't follow coding conventions:
    method names and variable names should start with lowercase letters
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for larger num problem

    import java.util.Scanner;
    public class LargerNum {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter two values: ");
    		double firstNum = input.nextDouble();
    		double secondNum = input.nextDouble();
    		double finalNum = maxMethod(firstNum, secondNum);
    		System.out.println( (int)finalNum + " is the larger of the two numbers.");
     
     
    	}
    	public static double maxMethod(double varOne, double varTwo){
     
    		double large;
    		if (varOne >= varTwo){
    			large = varOne;}
    		else
    			large = varTwo;
    			return large;
     
    }
    	}

    ok changed those things does this look more conventional? I need to learn to make my code look better so any suggestions are welcomed

    btw, Its not an error but a little bubble says that on the scanner input line that there is a resource leak because input is never closed. not sure what that means. how would i close that input?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    The code is still missing the {}s following the else.
    And there is still a hidden } at the end of a statement.

    The Scanner class has a close() method that the program should call when it is finished reading input.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2013
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Check for larger num problem

    ok so i think i should use input.close(); but i cant figure out where to put it. I tried the line after scanner input as well as the line after the println neither worked any suggestions?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,318
    Thanks
    49
    Thanked 2,191 Times in 2,164 Posts

    Default Re: Check for larger num problem

    neither worked
    please explain what happened.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. I am having trouble writing code that solves this problem involving check sums.
    By uncoordinated in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2013, 07:30 PM
  2. Knapsack problem , check if matrix can fill list of smaller matrix list.
    By ofirattia in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 8th, 2012, 12:20 PM
  3. For each K, output the smallest palindrome larger than K.
    By alekkk in forum Member Introductions
    Replies: 2
    Last Post: July 23rd, 2011, 02:50 PM
  4. Problem with equality check with HashSet
    By jmegha in forum Member Introductions
    Replies: 2
    Last Post: June 15th, 2011, 07:28 AM
  5. Help: Num to words
    By shamed in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 7th, 2010, 05:55 PM