+ 1

How to use scanner for taking inputs in functions

i.e in Java we follow pass by value so,, how will we pass values in calling function parameters via Scanner ....... why below program don't run..what r the errors?? import java.util.Scanner; public class MyClass { public static void main(String[ ] args) { Scanner myVar = new Scanner(System.in); MyClass obj=new MyClass(); int n1=myVar.nextInt(); int n2=myVar.nextInt(); int i=obj.blackJack(n1,n2); System.out.println(i); } public int blackJack(int n1,int n2){ if(n1<=21&&n2<n1) return n1; if(n2<=21&&n1<n2) return n2; if(n1>21&&n2>21) return -1; if(n1==n2&&n1<=21) return -2; if(n1<=21) return n1; if(n2<=21) return n2; return 0; } }

1st Aug 2016, 3:58 PM
ayush
1 Answer
+ 1
you can use Scanner class to first create an object of Scanner on System.in. Then by using this object you can read input from console. for example: Scanner obj = new Scanner(System.in); int input = obj.nextInt( ); Here 'input' variable will have the integer value given by user. Then you can call a function using this as parameter. functionName(input); or, you can directly write functionName(obj.nextInt( )); But, before using these methods you should read more about Scanner class and it's various methods. It is good to have knowledge about these.
2nd Aug 2016, 10:39 PM
Rahul Kumar
Rahul Kumar - avatar