Problems With Method Calls and Variables
Here is the deal; I've been doing this program in school and my teacher feels as though he has spent too much time helping me, and he said he has gotten the program to run, but I cannot figure it out, and he won't help me anymore. Everything seems to be in order but I have 9 errors.
Here are the program guidelines:
Task:
Write a program that calculates the volume of a rectangular solid with a length, width and height defined by the user. The user should be allowed to find the volume of multiple rectangular solids.
Conditions:
Use JOptionPane dialog boxes.
A control loop(do while, while) should be used within main() to allow the user to run the program multiple times.
main() should call a introduction method, a get values method and an end program method.
The get values method should call a three parameter calculate method.
Allow the user to enter lengths that contain decimals.
Format the volume to two decimals.
Do not forget System.exit(0);
Here is my code:
Code Java:
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
import cs1.Keyboard;
public class C6R7 {
public static void main(String[] args) {
//loop it fool
do{
//method call
introduction();
getvalues();
endprogram();
//char choice
String word1, word2;
word2 = "yes";
boolean value;
//do it again
word1 = JOptionPane.showInputDialog(null,
"Enter yes to continue: ",
"Continue?", JOptionPane.QUESTION_MESSAGE);
//equals
value = word1.equalsIgnoreCase(word2);
}while(value == true);
}
//-------------introduction---------------------
static void introduction(){
//intro
JOptionPane.showMessageDialog(null,
"Welcome to the amazing volume calculator...",
"Welcome", JOptionPane.INFORMATION_MESSAGE);
}
//--------------getvalues----------------------
static void getvalues(){
//variables
String length, width, height;
double lengthp = 0, widhtp = 0, heightp = 0, voulume = 0;
//decimal format
DecimalFormat fmt = new DecimalFormat("#####.##");
//get values
//input length
length = JOptionPane.showInputDialog(null,
"Enter the length of the figure: ",
"Length", JOptionPane.QUESTION_MESSAGE);
//input width
width = JOptionPane.showInputDialog(null,
"Enter the width of the figure: ",
"Width", JOptionPane.QUESTION_MESSAGE);
//input height
height = JOptionPane.showInputDialog(null,
"Enter the height of the figure: ",
"Height", JOptionPane.QUESTION_MESSAGE);
//parse it
heightp = Double.parse(height);
lengthp = Double.parse(length);
widthp = Double.parse(width);
//volume
volume = (lengthp * widthp) * heightp;
//output
JOptionPane.showMessageDialog(null,
"The volume of the figure is " + volume,
"The Volume", JOptionPane.INFORMATION_MESSAGE);
}
//------------------end program--------------------------------
static void endprogram(){
//byebye
JOptionPane.showMessageDialog(null,
"Thank you for using the Java Volume Calculator",
"Have a nice day!", JOptionPane.INFORMATION_MESSAGE);
//end it
System.exit(0);
}
}
Here are my errors:
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:32 : cannot find symbol
symbol : variable value
location: class C6R7
}while(value == true);
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:69 : cannot find symbol
symbol : method parse(java.lang.String)
location: class java.lang.Double
heightp = Double.parse(height);
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:70 : cannot find symbol
symbol : method parse(java.lang.String)
location: class java.lang.Double
lengthp = Double.parse(length);
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:71 : cannot find symbol
symbol : variable widthp
location: class C6R7
widthp = Double.parse(width);
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:71 : cannot find symbol
symbol : method parse(java.lang.String)
location: class java.lang.Double
widthp = Double.parse(width);
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:74 : cannot find symbol
symbol : variable volume
location: class C6R7
volume = (lengthp * widthp) * heightp;
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:74 : cannot find symbol
symbol : variable widthp
location: class C6R7
volume = (lengthp * widthp) * heightp;
^
\\Hsstusvr\2012\hartgrc\Java\C6R7\src\C6R7.java:78 : cannot find symbol
symbol : variable volume
location: class C6R7
"The volume of the figure is " + volume,
^
8 errors
Process completed.
Re: Problems With Method Calls and Variables
"cannot find symbol" means the compiler can not find a definition for that symbol that is in scope where the code is trying to use it. In scope means within the same {} pair. If you define a variable inside of a {} pair, it is not known outside of those {}s.
Look at where the error occurs. Is there a definition for the variable within the same {}s?
If there is not, you need to make a definition for the variable that is in the same {}s
Code :
String var0 = "eee"; // define a varable that will be known in the following {}s
{
String var1 = "DDD"; // define var1 and give it value (local to these{}s)
var1 = var0; // Ok because var0 is in scope /known here
} // end of scope for var1
{ // begin new scope
String var2 = var1; // here var1 is not defined (it is out of scope)
var2 = var0; // Ok because var0 is in scope here
}
Re: Problems With Method Calls and Variables
You must know that before using any variable, you must declare/initialize it.