+ 1

How can i set length of any strings int to parameters of any return method ?

class Program { public static void main(String[ ] args) { int res = max(x.length(),y.length()); System.out.println(res); String x = "Hi Hello Bolo"; String y = "Hi Hello Bolo yaar"; int n =x.length() ; int m = y.length(); } static int max(int m, int n) { if(m > n) { return m; } else { return n; } } }

19th Oct 2019, 7:22 PM
VikaSh YadaV
VikaSh YadaV - avatar
3 ответов
+ 3
VikaS YadaV You should first declare variable then do other work. Like in your case you are calling method first and declaring variable after that which is wrong. You can write like this. class Program { public static void main(String[ ] args) { String x = "Hi Hello Bolo"; String y = "Hi Hello Bolo yaar"; int n = x.length() ; int m = y.length(); int res = max(m, n); System.out.println(res); } static int max(int m, int n) { if(m > n) { return m; } else { return n; } } }
19th Oct 2019, 8:19 PM
A͢J
A͢J - avatar
+ 2
Thanks it is really helpful I like these answers👍👍
20th Oct 2019, 3:10 AM
VikaSh YadaV
VikaSh YadaV - avatar
+ 1
Put int res = max(x.length(),y.length()); System.out.println(res); after string x and string y Also delete int n = x.length(); and int m = y.length(); Java executes code from top to bottom, whats happening is that when you call the max method at the top and are using x.length() and y.length(), you are trying to use string objects that aren't made yet.
19th Oct 2019, 7:30 PM
Odyel
Odyel - avatar