How to call a value from a different method to main
Code :
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!
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?
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.
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.
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.
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:
Code java:
public static void main(String... args){
int sum = add(1, 2);
}
public static int add(int a, int b){
return a+b;
}
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.
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?
Re: How to call a value from a different method to main
Quote:
Originally Posted by
CrimsonFlash
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.
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.
Re: How to call a value from a different method to main
Quote:
Originally Posted by
CrimsonFlash
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.
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.
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.
Re: How to call a value from a different method to main
Quote:
Originally Posted by
CrimsonFlash
....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.