Why the following is not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the following is not working

import java.util.Scanner; class Program { public static void main(String[ ] args) { Scanner number = new Scanner(System.in); int a,b; System.out.println("enter a"); a = number.nextint; System.out.println("enter b"); b = number.nextint; static max(int a , int b){ if(a > b) { return a; } else { return b; } } }

25th Feb 2017, 12:16 AM
keshav Tangri
keshav Tangri - avatar
15 Answers
+ 2
I wrote the code on here and have it working.. check my codes for one I named "Yours"
25th Feb 2017, 4:31 PM
LordHill
LordHill - avatar
+ 1
A number of things. try this in your main method.. Scanner number = new Scanner(System.in); System.out.println("enter a"); int a = number.nextInt(); System.out.println("enter b"); int b = number.nextInt(); max(a,b); and your method should go outside of your main method. try putting this either before or after your main method. public static int max(int x, int y){ if(x > y) { return x; } else { return y; } }
25th Feb 2017, 12:22 AM
LordHill
LordHill - avatar
+ 1
What you call the parameters in your max function doesn't matter. Leaky Egg renamed them for readability: If the names are the same, one might think they had something to do with a and b. They do not. If you had another pair of int, you could put them in as well.
25th Feb 2017, 1:29 AM
1of3
1of3 - avatar
+ 1
x and y are just arguments for your method. What variable names you use for your method arguments don't matter, but it's best to not use names you already use for variables. Since you already have a variable of a and b, I just used 2 different variable names of x and y. when you call your method, you give value to x and y. here we called the method and gave x the value of a and y the value of b max(a,b); or you could just assign x and y numbers. max(3,5) Main is a method. The method that runs when your program starts. you can put your scanner and your inputs here if you want, or you could write another method to handle that.
25th Feb 2017, 1:29 AM
LordHill
LordHill - avatar
0
bro please explain y the scanner and stuff in main and after that we write our method and in our method we are comparing x and y and not a and b ... ??? I didn't get it
25th Feb 2017, 1:17 AM
keshav Tangri
keshav Tangri - avatar
0
okay thank you very much I got some idea about it ... I ask again after trying again on pc :)
25th Feb 2017, 1:37 AM
keshav Tangri
keshav Tangri - avatar
0
I did as I wrote leaky but it's not proceeding
25th Feb 2017, 7:45 AM
keshav Tangri
keshav Tangri - avatar
0
what error is it throwing?
25th Feb 2017, 10:42 AM
LordHill
LordHill - avatar
0
duplicate local variable .... a and same for b illegal modifier for parameter Max; only final is permitted syntax error on token "(" , ; expected syntax error on token "," , ; expected void methods cannot return a value at Mymethods.main(Mymethods.java:7)
25th Feb 2017, 1:33 PM
keshav Tangri
keshav Tangri - avatar
0
I'm not sure on all of it, but duplicate local variable means you try to initiate the a and b variables multiple times. int a .. this initiates variable a .. when you want to use that variable either to get a value or set a value, you no longer need to put int before it. it is already declared. putting int in front of a again trys to declare it again. int a; a=5; a=a+a; notice I declared it only once then I can use it as much as I want Void.. when you are writing a method, and you name it something like public void max() ... it does something, but it doesn't return a value. that's essentially what void means. if you write something like public static int max() ... calling int in the method name tells the method is is going to return an integer. at the end of the method it looks for a return statement with an integer. what IDE are you using? ones like Eclipse can help keep some of this straight for you
25th Feb 2017, 2:05 PM
LordHill
LordHill - avatar
0
@LeakyEgg okay I got that point not to use int again and again ... and IDE I am using is eclipse only brother ... it displays crosses or tip at the left side but since I have no professor to rely on I am relying on this app and you guys for my doubts .... well coming back to the code after changing to only a crosses got removed then to return a value it asked me to change void to int that is public static int main(string[]args){ and now only cross remains on the int max line ..... it says illegal modifier for parameter max only final is permitted .... 2 syntax error .... and after running with error ... the output displays main method must return a value of type void in class my methods , please define main method as public static void main(string[]args) ........ I am confused
25th Feb 2017, 2:53 PM
keshav Tangri
keshav Tangri - avatar
0
public static void main (String[] args) { } this is the standard main.. do you have return; anywhere in your main function? main can't be used to return values, only to run code or call methods.
25th Feb 2017, 4:15 PM
LordHill
LordHill - avatar
0
I have no where return in main I just wrote the same codes what u told at first all issues are resolved except the last one I told u
25th Feb 2017, 4:17 PM
keshav Tangri
keshav Tangri - avatar
0
I am at work at the moment, in a couple hours when I get home if you haven't solved the issues, I will have you email me your code and I will put it in my eclipse and see what's up
25th Feb 2017, 4:18 PM
LordHill
LordHill - avatar
- 1
thank you brother that will help me a lot
25th Feb 2017, 4:20 PM
keshav Tangri
keshav Tangri - avatar