Ask your own question, for FREE!
Computer Science 16 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!
Latest Questions
Mari103: How to pop out like a Jacc In the box
52 minutes ago 0 Replies 0 Medals
Breathless: Spooky witch but cute
7 hours ago 3 Replies 0 Medals
Arriyanalol: help
7 hours ago 10 Replies 2 Medals
Arriyanalol: @tinydinoUwU stop trying to find a argument u blad lil boy
1 day ago 5 Replies 4 Medals
Jaded012023: Please tell me what you all think of this song
10 hours ago 6 Replies 1 Medal
Arriyanalol: bro how
10 hours ago 2 Replies 3 Medals
Arriyanalol: cant wait for the new bluey movie in 2027
1 day ago 12 Replies 2 Medals
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!