• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Methods Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm extremely new to Java and I'm having a bit of trouble figuring out how methods work. I'm trying to create an area calculator for up to three rectangles that allows the user to input their own lengths and widths. I don't understand how to get the scanner working in this particular code. Any advice would be much appreciated. Thanks so much!

 
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Your Scanner should be working fine. The problem is that your statements need a bit of rearranging.

If you look at lines 9 to 19, you might notice that they do the same thing three times. Something like that should be done one time inside a loop that iterates three times.

The userInput method, on the other hand, does one thing N times. It should be doing one thing one time: prompting for and getting input of a number. The number entered should be returned by the method and assigned to a variable that you will use, like how the area() method returns the result of calculating the area.

So here's a story for what you want to do:

Ask the user for a number
Assign the number entered to length
Ask the user for another number
Assign the number to width
Calculate the area of a rectangle with length X width
Display the area

Repeat the above three times.

Your code has all these elements but has a different arrangement which ends up not being the story I just told. That's the bug.

Note that the double variables you declare on lines 35 and 38 only exist inside that method. They are local variables that disappear when the userInput() method finishes executing. To preserve the value, you have to return it and assign that returned value to another variable outside of userInput().
 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Note that the double variables you declare on lines 35 and 38 only exist inside that method. They are local variables that disappear when the userInput() method finishes executing. To preserve the value, you have to return it them and assign that returned value to another variable outside of userInput().

Anytime you need to return more than one value you pretty much have to use a class for that purpose. The class may be minimalist designed to hold the values to be returned, and nothing more. Or may be part of a larger more complete class. It may be a class already available in the Java library, or you may need to write it yourself.

A minimalist design might be:
At the end of your input method you could construct one of these and send it back to the caller.
 
Marshal
Posts: 65814
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . you pretty much have to use a class for that purpose. The class may be minimalist . . .

In this case, you can probably get away with a plain simple double[].
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a class or double[] is complicating things way too much. What's wrong with userinput just prompting for and returning one double at a time? That's why I wrote "return it" and not "them" in the first place.
 
Carey Brown
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Junilu Lacar
Marshal
Posts: 14053
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch. Definitely not intended by OP.
 
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!