Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

In Java, how do I include a scanner when using methods? Example, method for the perimeter of x and y values. This is my code: import java.util.Scanner; class test { public static void main(String[] args) { Scanner type = new Scanner(System.in); // scanner double x, y, answer; System.out.println("Enter value for x; "); x = type.nextDouble(); System.out.println("Enter value for y; "); y = type.nextDouble(); } public void perimeter(double x, double y){ //perimeter method double ans = x + y; System.out.println("The perimeter is "+ans); } }

OpenStudy (espex):

What do you mean? Once you have read the values in you can simply pass the values to your methods and do what you need.

OpenStudy (anonymous):

looks like it should work: String str = "9.72 8.75"; // the input string Scanner type = new Scanner(str); double x = type.nextDouble(); double y = type.nextDouble(); < do something with x and y....>

OpenStudy (anonymous):

you want to enter both x and y on one line...enter x and y: 9.85 10.74

OpenStudy (anonymous):

ah.... if perimeter is being called from main, it must be declared static - public static void perimeter(double x, double y){ <stuff> } now call perimeter from main: < get x and y > perimeter(x,y); // last statement in main } (I hope i've run out of things to say now....) (and that it works....) (and i have understood...)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!