• 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Where to declare a method within a program?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

I am currently writing a program that adds together any number of integers entered by the user, adds them together and outputs both the sum and the largest number the user entered - for this particular part of the program I am using a method called 'returnLargest', since the number of values to be added together is not fixed. However, I am a bit stumped as to where abouts in the program the method should be declared.
For some reason, the compiler won't let me declare the method either just inside main or before/after it. :S

Any help/guidance would be greatly appreciated.

Here is my code:

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot define a method inside another method as you did in your code (returnLargest within main). However, if you move it outside the compiler will still complain about almost everything.

Instead of writing everything here for you, I think it would be easier to point you to Classes tutorial section, which I recommend you to read thoroughly, including all subsections.
 
Greenhorn
Posts: 29
Android Chrome Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java defining method inside a method is not allowed. You might not have known but main is also a method.
about compiler giving error about you declaring method before and after it verify whether the method declaration is inside the class. Use an IDE so that you can instantly know what mistakes you are doing by reading the errors.

ALL THE BEST
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sampath surineni wrote:Use an IDE so that you can instantly know what mistakes you are doing by reading the errors.


I have to disagree with this. Using an IDE while learning a language will make you feel too comfortable (it's not good as it sounds) because it does almost everything for you. You would pay less attention to language itself, so you would actually learn how to use a tool and just make your program work. That's not how programming should be done...
 
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic