Static Methods
I am using a staitc method to call a class named Validator. I want to keep using the static method(since it is a requirement of the project) but I am getting the following errors off of my Validator class:
Validator.java:9: cannot find symbol
symbol : variable userEntry
location: class Validator
userEntry = Double.parseDouble(dataEntry);
^
Validator.java:10: cannot find symbol
symbol : variable userEntry
location: class Validator
if (userEntry > 100.00 || userEntry < 0.0)
^
Validator.java:10: cannot find symbol
symbol : variable userEntry
location: class Validator
if (userEntry > 100.00 || userEntry < 0.0)
^
Validator.java:18: cannot find symbol
symbol : variable strthirdMessage
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, JOptionPane.ERROR_MESSAGE);
^
Validator.java:18: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, JOptionPane.ERROR_MESSAGE);
^
Validator.java:18: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, JOptionPane.ERROR_MESSAGE);
^
Validator.java:21: incompatible types
found: java.lang.String
required: double
return dataEntry;
^
Here is the validator class
publicclass Validator
{
publicstaticdouble getIsValid(String dataEntry)
{
while (!dataEntry.equalsIgnoreCase("x"))
{
try
{
userEntry = Double.parseDouble(dataEntry);
if (userEntry > 100.00 || userEntry < 0.0)
{
thrownew Exception();
}
}
catch (Exception e)
{
String thirdMessage = ("Invalid entry. Please enter valid data");
dataEntry = JOptionPane.showInputDialog(strthirdMessage,""+JOptionPane.ERROR_MESSAGE);
}
}
return dataEntry;
}
}
And here is the main method
[code]
//Thomas Tate
//Project 2 -GUI
import javax.swing.*;
import java.util.*;
import java.lang.*;
publicclass GUIGradeApp
{
publicstaticvoid main(String[] args)
{
//defines variables
double userEntry = 0;
String dataEntry ="tyt";
double gradeTotal = 0;
double highest = 0;
double lowest = 99999.0;
double gradeAverage = 0;
int gradeCount = 0;
String letterGrade ="";
//gets input from the user
String strFirstMessage = ("Please enter a test score");
dataEntry = JOptionPane.showInputDialog(strFirstMessage,""+JOptionPane.ERROR_MESSAGE);
double something = Validator.getIsValid(dataEntry);
gradeTotal = gradeTotal + userEntry;
gradeCount = gradeCount + 1;
highest = Math.max(highest, userEntry);
lowest = Math.min(lowest, userEntry);
String strSecondMessage = ("Please enter a test score,\n or 'x' to view data results.");
dataEntry = JOptionPane.showInputDialog(strSecondMessage,""+JOptionPane.ERROR_MESSAGE);
//creates letter grade
gradeAverage = gradeTotal / gradeCount;
if (gradeAverage <= 89.5 || gradeAverage >= 100.00)
letterGrade ="A";
elseif (gradeAverage <= 79.5 || gradeAverage > 89.5)
letterGrade ="B";
elseif (gradeAverage <= 69.5 || gradeAverage > 79.5)
letterGrade ="C";
elseif (gradeAverage <= 59.5 || gradeAverage > 69.5)
letterGrade ="D";
else
letterGrade ="F";
String strThirdMessage ="Number of scores: " + gradeCount +"\n"
+"Average score: " + gradeAverage +"\n"
+"Letter Grade: " + letterGrade +"\n"
+"Best score: " + highest +"\n"
+"Press enter to continue entering scores,\nor 'x' to exit.";
JOptionPane.showInputDialog(strThirdMessage,""+JOptionPane.ERROR_MESSAGE);
}
}
[6434 byte] By [
TY_Whiteya] at [2007-11-27 2:31:23]

Just looking at the first error, the others may be similar: userEntry. This is a local variable in method main. Do you expect local variables to be magically transported from method to method?
I changed it but now it is not finding dataEntry within the Validator.
Validator.java:21: cannot find symbol
symbol : variable strthirdMessage
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:21: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:21: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:24: incompatible types
found: java.lang.String
required: double
public class Validator
{
public static double getIsValid(String dataEntry)
{
double userEntry = 0;
while (!dataEntry.equalsIgnoreCase("x"))
{
try
{
userEntry = Double.parseDouble(dataEntry);
if (userEntry > 100.00 || userEntry < 0.0)
{
throw new Exception();
}
}
catch (Exception e)
{
String thirdMessage = ("Invalid entry. Please enter valid data");
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
}
}
return dataEntry;
}
}
When i do try and declare the String dataEntry with this class an error comes back saying that it has already been defined.
> I changed it but now it is not finding dataEntry within the Validator.Did you even read these errors messages? Now it claims not to find:1. strthirdMessage2. JOptionPane
Yea I saw that and I'm just throwing it in the air. Something is not right but i can not figure out what.
Validator.java:7: dataEntry is already defined in getIsValid(java.lang.String)
String dataEntry = "";
^
Validator.java:22: cannot find symbol
symbol : variable strthirdMessage
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:22: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:22: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
^
> dataEntry is already defined in getIsValid(java.lang.String)Could it be that dataEntry is already defined in getIsValid?Just throwin' it out there.
Okay I change the code around some. Here is the edited Validator class.
public class Validator
{
public static String getIsValid(String dataEntry)
{
double userEntry = 0;
String dataEntry = "";
while (!dataEntry.equalsIgnoreCase("x"))
{
try
{
userEntry = Double.parseDouble(dataEntry);
if (userEntry > 100.00 || userEntry < 0.0)
{
throw new Exception();
}
}
catch (Exception e)
{
String thirdMessage = ("Invalid entry. Please enter valid data");
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
}
}
return dataEntry;
}
}
And here is the edited main
//Thomas Tate
//Project 2 -GUI
import javax.swing.*;
import java.util.*;
import java.lang.*;
public class GUIGradeApp
{
public static void main(String[] args)
{
//defines variables
double userEntry = 0;
String dataEntry = "";
double gradeTotal = 0;
double highest = 0;
double lowest = 99999.0;
double gradeAverage = 0;
int gradeCount = 0;
String letterGrade = "";
//gets input from the user
String strFirstMessage = ("Please enter a test score");
dataEntry = JOptionPane.showInputDialog(strFirstMessage, ""+JOptionPane.ERROR_MESSAGE);
String something = Validator.getIsValid(dataEntry);
gradeTotal = gradeTotal + userEntry;
gradeCount = gradeCount + 1;
highest = Math.max(highest, userEntry);
lowest = Math.min(lowest, userEntry);
String strSecondMessage = ("Please enter a test score,\n or 'x' to view data results.");
dataEntry = JOptionPane.showInputDialog(strSecondMessage, ""+JOptionPane.ERROR_MESSAGE);
//creates letter grade
gradeAverage = gradeTotal / gradeCount;
if (gradeAverage >= 89.5 || gradeAverage <= 100.00)
letterGrade = "A";
else if (gradeAverage <= 79.5 || gradeAverage > 89.5)
letterGrade = "B";
else if (gradeAverage <= 69.5 || gradeAverage > 79.5)
letterGrade = "C";
else if (gradeAverage <= 59.5 || gradeAverage > 69.5)
letterGrade = "D";
else
letterGrade = "F";
String strThirdMessage = "Number of scores: " + gradeCount + "\n"
+ "Average score: " + gradeAverage + "\n"
+ "Letter Grade: " + letterGrade +"\n"
+ "Best score: " + highest + "\n"
+ "Press enter to continue entering scores,\nor 'x' to exit.";
JOptionPane.showInputDialog(strThirdMessage, ""+JOptionPane.ERROR_MESSAGE);
}
}
>Validator.java:7: dataEntry is already defined in getIsValid(java.lang.String)
>String dataEntry = "";
>^
>Validator.java:22: cannot find symbol
>symbol : variable strthirdMessage
>location: class Validator
>dataEntry = JOptionPane.showInputDialog(strthirdMessage, >""+JOptionPane.ERROR_MESSAGE);
>^
>Validator.java:22: cannot find symbol
>symbol : variable JOptionPane
>location: class Validator
>dataEntry = JOptionPane.showInputDialog(strthirdMessage, >""+JOptionPane.ERROR_MESSAGE);
>^
>Validator.java:22: cannot find symbol
>symbol : variable JOptionPane
>location: class Validator
>dataEntry = JOptionPane.showInputDialog(strthirdMessage, >""+JOptionPane.ERROR_MESSAGE);
I am still getting the same error messages even with the edited code
public class Validator {
public static String getIsValid(String dataEntry) { //#1
double userEntry = 0;
String dataEntry = ""; //#2
Do you see why you are getting the error "dataEntry is already defined in getIsValid"?
I see what you are talking about but I still don't see why this keep popping up
Validator.java:22: cannot find symbol
symbol : variable JOptionPane
location: class Validator
dataEntry = JOptionPane.showInputDialog("Invalid entry. Please enter valid data", ""+JOptionPane.ERROR_MESSAGE);
^
Validator.java:22: cannot find symbol
symbol : variable JOptionPane
location: class Validator
By the way i deleted the String i declared.
You forgot to:import javax.swing.*;in that file.
did that and got this
import javax.swing.*;
public class Validator
{
public static String getIsValid(String dataEntry)
{
double userEntry = 0;
while (!dataEntry.equalsIgnoreCase("x"))
{
try
{
userEntry = Double.parseDouble(dataEntry);
if (userEntry > 100.00 || userEntry < 0.0)
{
throw new Exception();
}
}
catch (Exception e)
{
String thirdMessage = ("Invalid entry. Please enter valid data");
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE);
}
}
return dataEntry;
}
}
Error
Validator.java:24: cannot find symbol
symbol : variable strthirdMessage
location: class Validator
dataEntry = JOptionPane.showInputDialog(strthirdMessage, ""+JOptionPane.ERROR_MESSAGE)
I figured it out. see my other posting. thanks for the help and here is the full 10 duke points.