Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

calling methods

 
Elon Zibitt
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so i have a class that has 2 methods. method 1 parses an xml document and grabs a specific value. the second method formats the value. do i have to run both methods from a thrid method in order for the main to print out the formatted value? if so how would i do that?
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could call both methods from within the main method.
public static void main(Sting[] args)
{
// this example is assuming the value is a String
// of course it may be whatever
String value = method1( xmldocument);
String foirmattedValue = method2( value );
// or you could do it this way but it is a little less obvious
// chaine the output of method1 into the call to method 2
String formattedValue = method2( method1(xmldocument) );
}
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
timonthy's solution assumes that everything inside the class containing this main method is static. If they aren't and method1 and method2 are instance methods, then you'll need to allocate an instance of the class before you call those methods.

-Peter
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!