Ask coding questions

← Back to all posts
what is wrong in this code please explain
Abhisheksharm28 (0)

https://repl.it/@Abhisheksharm28/BestKnowingCores whT IS WRONG IN THIS CODE iHAVE SOME ISSSUES WITH variable declarations and its usses please do help!!!

Commentshotnewtop
heyitsmarcus (287)

It looks like you at least need some refactoring in your code. Just guessing based on the structure of your code, you are calling takeInt() which is supposed to be passed in a number, that is immediately returned to the caller variable. And then you want to output the caller variable to the screen.

You need to make sure you have public static before the takeInt function name. You can use private static as well, but the takeInt method will only be exposed in the Main class.

Also, keep in mind that any time the Java compiler hits a return statement within a function, it immediately ceases execution within that function and returns the value specified. So, in your code, the System.out.println() would never execute because the return was on the line before.

And you want to return the number passed in, at least, and not an arbitrary number 6. Otherwise, your function will only ever return the number 6 no matter what you pass in.

I'm sure there's more to this than what you've elaborated on. If you need any further help, I'm happy to help.

And here is a forked repl of your code: https://repl.it/@heyitsmarcus/Java-takeInt

 public class Main {
	public static int takeInt(int x) {
		return x;
	}
	public static void main(String[] args) {
		int b = takeInt(7);
		System.out.println("result: " + b);
  	}
}
heyitsmarcus (287)

@Abhisheksharm28 You are very welcome! Did that solve your problem?

mwilki7 (286)

Are you trying to call the function takeInt() ?