Java classes

I am new to Java and I am trying to create a Validator Class to validate my application. Here is the gui part:

//Thomas Tate

//Project 2 -GUI

import javax.swing.*;

public class GUIGradeApp

{

public static void 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 inout from the user

String strFirstMessage = ("Please enter a test score");

dataEntry = JOptionPane.showInputDialog(strFirstMessage, JOptionPane.ERROR_MESSAGE);

//loop that calculates totals.

while (!dataEntry.equalsIgnoreCase("x"))

{

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 strSecondMessage = "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(strSecondMessage, JOptionPane.ERROR_MESSAGE);

}

}

And here is the validator class:

public class Valdiator

{

//Validator val = new Validator();

//}

public static getIsValid(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 userEntry;

}

}

}

How would i fix this? I would like to call the validiator class in my GUI application but I still don't know how? Can anyone help?

[2843 byte] By [TY_Whiteya] at [2007-11-27 2:12:20]
# 1
Is the problem that when you try to add the validator it tells you it can't be found?As far as I'm aware, you have 2 choices: You could put both classes in one file and make one class a public static class, or you can attach package headers to your classes.
tjacobs01a at 2007-7-12 2:06:30 > top of Java-index,Java Essentials,New To Java...
# 2
Check out this trail http://java.sun.com/docs/books/tutorial/java/package/packages.html
tjacobs01a at 2007-7-12 2:06:30 > top of Java-index,Java Essentials,New To Java...
# 3
Well I am creating two seperate files located in the same folder. I am trying to call the validator class in the GradeGuiApp
TY_Whiteya at 2007-7-12 2:06:30 > top of Java-index,Java Essentials,New To Java...
# 4

I have no idea where in your GUI class you want to call it or what exactly you want to pass in to it, but basically, you'd call it like this:

X x = Validator.getIsValid(whatEverStringIWantToPassInHere);

Then interrogate x.

Waitwaitwait! ... I did not read your code carefully enough (please use codetags) ... look at this please:

public static getIsValid(string dataEntry) { // No return type - this won't compile! Also, String not string!

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 userEntry; // return type - will not compile!

}

}

~Bill

Message was edited by:

abillconsl

abillconsla at 2007-7-12 2:06:30 > top of Java-index,Java Essentials,New To Java...