Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: How to call a value from a different method to main

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to call a value from a different method to main

    import java.util.Scanner;
     
    public class Quiz7
    {
    	public static void main(String[] args)
    	{
    		Scanner kb1 = new Scanner(System.in);
    		String programName;
    		String dueDate;
    		int iMenuInput = 0;
    		System.out.print("What is the name of the program? ");
    			programName=kb1.nextLine();
    			System.out.print("What is the due date of the program? ");
    			dueDate=kb1.nextLine();
    			displayMyInfo(programName, dueDate);
    			menu();
     
    			switch(iMenuInput)
    			{
    				case 1:
    						System.out.print("You got it right!");	
    			}
    	}//end of main
     
    	public static void displayMyInfo(String programName, String dueDate)
    	{
    			System.out.print("The program name is" +programName);
    			System.out.print("The due date of the program is" +dueDate);
    	}//end of displayMyInfo
     
    	public static int menu()
    	{
    		Scanner kb2 = new Scanner(System.in);
    		int iMenuInput;
    		System.out.print("* * * * * * * * M E N U * * * * * * * *");
    		System.out.print("\n\n1>   Area of a circle");
    		System.out.print("\n2>    Circumference of a circle");
    		System.out.print("\n3>    Area of a rectangle");
    		System.out.print("\n4>   Perimeter of a rectangle");
    		System.out.print("\n5>    Volume of a box");
    		System.out.print("\n6>    Volume of a cone");
    		System.out.print("Enter your choice ");
    		iMenuInput=kb2.nextInt();
    		return iMenuInput;
    	}//end of menu
     
    	public static double areaCircle(int radius)
    	{
    		final double PI = 3.1416f;
    		double dAreaCircle;
    		dAreaCircle = PI * radius * radius;
    		return dAreaCircle;
    	}//end of areaCircle
     
    	public static double areaCircle(int radius)
    	{
    		final double PI = 3.1416f;
    		double dAreaCircle;
    		dAreaCircle = PI * radius * radius;
     
    		return dAreaCircle;
     
    	}//end of areaCircle
     
    	public static double circumference(int radius)
    	{
    		final double PI = 3.1416f;
    		float fCircumference;
    		fCircumference = (float)(2 * PI * radius);
    		return fCircumference;
    	}//end of circumference
     
    	public static int areaRectangle(int length, int width)
    	{
    		int iAreaRectangle;
    		iAreaRectangle = length * width;
    		return iAreaRectangle;
    	}//end of areaRectangle
     
    	public static int perimeterRectangle(int length, int width)
    	{
    		int iPerimeterRectangle;
    		iPerimeterRectangle = 2 *(length * width);
    		return iPerimeterRectangle;
    	}//end of perimeterRectangle
     
    	public static int volumeBox(int length, int width, int height)
    	{
    		int iVolumeBox;
    		iVolumeBox = length * width * height;
    		return iVolumeBox;
    	}//end of volumeBox
     
    	public static double volumeCone(int radius, int height)
    	{
    		final double PI = 3.1416f;
    		float fVolumeCone;
    		fVolumeCone = (float)(PI*radius*radius*radius*height)/3.0f;
    		return fVolumeCone;
     
    	}//end of volumeCone
     
     
    }//end of Quiz7

    For instance, if I choose option 1, I want it to call the formula from method AreaCircle to main, and apply it to the case statement, I believe. How would I go about doing this? Thanks in advance again!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    You're returning a value from your menu() method, but then you aren't doing anything with that value (like storing it in a variable). Are you sure that's what you want to be doing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    How would I store this value in a variable? I'm completely new to Java in my 5th week of class, sorry for seeming ignorant.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    Believe it or not, you already know how to store a value returned from a method in a variable. The Scanner methods nextLine() and nextInt() are simply methods that return values. You'd store the value returned from your menu() method in exactly the same way.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    CrimsonFlash (October 22nd, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    But I'm storing it in the variable iMenuInput, right? I'm confused, and I'm sure I'm confusing you as well. If you could give me an example of what you mean on which line, I'd appreciate it.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    I see your confusion. The problem is that you have two different variables named iMenuInput, and they each have their own scope. One is only visible in the main() method, and the other is only available in the menu() method. Making a change to one of them doesn't do anything to the other. You might want to call them different things though, just to make it easier to keep them separate in your brain.

    But what you want to do is set the value of the iMenuInput variable in your main() method to the value returned from the menu() method. For example, say I have a method named add() and a variable named sum. Here is what I would do to set sum to the value returned from the add() method:

    public static void main(String... args){
       int sum = add(1, 2);
    }
     
    public static int add(int a, int b){
       return a+b;
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    Thank you! I'll try that; if I have anymore questions I'll know who to ask.

  9. #8
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    But my menu method doesn't contain any parameters..... how would I do it in this case?

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    Quote Originally Posted by CrimsonFlash View Post
    But my menu method doesn't contain any parameters..... how would I do it in this case?
    What did you try? The parameters have nothing to do with the return value. It was just an example.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    In the menu method, I renamed ImenuInput to ImenuInput and declared ImenuInput2 =1 + 2 and then declared iMenuInput1 = 3. It doesn't work needless to say.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    Quote Originally Posted by CrimsonFlash View Post
    In the menu method, I renamed ImenuInput to ImenuInput and declared ImenuInput2 =1 + 2 and then declared iMenuInput1 = 3. It doesn't work needless to say.
    What? Why? My add() method example was just an example. It has nothing to do with your code, other than the fact that it contains a method that returns a value, and a variable that holds that value. The actual numbers and addition have nothing to do with what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  13. The Following User Says Thank You to KevinWorkman For This Useful Post:

    CrimsonFlash (October 22nd, 2011)

  14. #12
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    ....I understand now. Thanks a bunch! I believe I'm a bit too dense for Java coding as a career.

  15. #13
    Junior Member
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to call a value from a different method to main

    Though I do have another question: How do I call a method in a switch statement?

    switch(iMenuInput1)
    {
    case 1:
    areaCircle(radius);
    System.out.print("Enter the radius: ");

    }
    I want to call the method public static double areaCircle(int radius) to my switch statement.

  16. #14
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: How to call a value from a different method to main

    Quote Originally Posted by CrimsonFlash View Post
    ....I understand now. Thanks a bunch! I believe I'm a bit too dense for Java coding as a career.
    No worries, everybody starts out feeling that way. Good programmers probably feel that way throughout most of their careers. The more you learn, the more you find out what you don't know.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  2. Call method(s) within the same class
    By mwr76 in forum Object Oriented Programming
    Replies: 8
    Last Post: September 26th, 2011, 12:58 AM
  3. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM
  4. call super method outside of 'this'
    By questionmark in forum Object Oriented Programming
    Replies: 1
    Last Post: January 29th, 2011, 11:29 AM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM