# Check for larger num problem

• March 20th, 2013, 08:53 PM
iamgonge
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
Code java:

```    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
• March 20th, 2013, 08:57 PM
Norm
Re: Check for larger num problem
[code=java]
[/code]
to get highlighting and preserve formatting.

Quote:

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.
• March 20th, 2013, 09:11 PM
iamgonge
Re: Check for larger num problem
Code java:

```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.
• March 20th, 2013, 09:29 PM
Norm
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.

Quote:

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.
• March 20th, 2013, 09:36 PM
iamgonge
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.
• March 20th, 2013, 09:51 PM
Norm
Re: Check for larger num problem
Code :

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

• March 20th, 2013, 09:57 PM
iamgonge
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:

Code java:

```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;   } }```
• March 20th, 2013, 10:01 PM
Norm
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:
• March 20th, 2013, 10:13 PM
iamgonge
Re: Check for larger num problem
Code java:

```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?
• March 20th, 2013, 10:24 PM
Norm
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.
• March 20th, 2013, 10:53 PM
iamgonge
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?
• March 21st, 2013, 07:26 AM
Norm
Re: Check for larger num problem
Quote:

neither worked