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

1. ## Testing double null

I need a little advice on how to execute a particular class. I've created a Kinematics class that will execute and calculate all the functions pertaining to kinematic equations. In the main class, I will have the user provide the data in JTextField. Now I need to test which JTextfields (variables) were left empty. I plan to use a number of if/else statements :
`if(distanceI != null && distanceF != null)`
The problem with this is that these variables are double and therefore cannot be tested as null. I read a few things about Double but I really don't see how I'm supposed convert each double variable to Double, is that even possible?

I know this code is going to take quite a bit of time to write and will be cumbersome since I need to manipulate each equation to solve for the missing variable, but figuring this out would be a big step in completing this program.

```public class Kinematics {
double distanceI, distanceF, velocityI, velocityF,
velocityAverage, acceleration;
public Kinematics(){

//v2 = v1 + a*dt
if(distanceI != null && distanceF != null){

}
else{

}

}
public void setDistanceI(double distanceI){
this.distanceI = distanceI;
}
public void setDistanceF(double distanceF){
this.distanceF = distanceF;
}
public void setVelocityI(double velocityI){
this.velocityI = velocityI;
}
public void setVelocityF(double velocityF){
this.velocityF = velocityF;
}
public void setVelocityAverage(double velocityAverage){
this.velocityAverage = velocityAverage;
}
public void setAcceleration(double acceleration){
this.acceleration = acceleration;
}
public double getDistanceI(){
return distanceI;
}
public double getDistanceF(){
return distanceF;
}
public double getVelocityI(){
return velocityI;
}
public double getVelocityF(){
return velocityF;
}
public double getVelocityAverage(){
return velocityAverage;
}
public double getAcceleration(){
return acceleration;
}
}```

2. ## Re: Testing double null

What do you want to test for? A primitive will never have a null value.

3. ## Re: Testing double null

When I set the restrictions for the if/else statements, the program needs to know which variable to solve for. So for instance, F = ma:
```if(m != null && a!= null)
F = m*a;
else if(F!=null && m!= null)
a = F / m;
else().....```

Do you think that is the best way to do this?

4. ## Re: Testing double null

Perhaps the caller of the method could sort out what computation to do and call an appropriate method to solve it.

5. ## Re: Testing double null

The method would still need to test whether or not a double is null...no? I had considered making a method for each equation, but since there are slight variations in each of them I thought I could contain them within the same method...

6. ## Re: Testing double null

test whether or not a double is null
A primitive like double will never have a null value.

7. ## Re: Testing double null

Originally Posted by Norm
A primitive like double will never have a null value.
Yes, I know that, as I mentioned in my first post. I'm not sure what you are suggesting I can do with the method to "sort out what computation to do".

Would this be attached to the calculate JButton some how?

8. ## Re: Testing double null

test which JTextfields (variables) were left empty.
Test the String that is returned by the getText method.

9. ## Re: Testing double null

Thank you, I took your advice and found some good stuff online. I'm just trying to get my thoughts down in code (what you see below), but after that, I started thinking about the method you were talking about. Should I create a single "checkJTF" method that will perform the actions of the "for" loop below, for each textfield array (e.g. kinematics, dynamics, circuits..etc.) and include the try/catch. . Then I can just call the method inside the actionListener and from there pass the values to the class to be calculated?

My concern with doing it this way is that if I used a single method to check the JTF that it will include all the empty JTF's from each JPanel. So in other words, there would be the empty textfield for voltage, current, and resistors, when the user is actually just trying to to find acceleration.

Also, when I tried to assign the empty JTF to unknown -- what type do I make it?

```calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){

for(int i = 0; i < kinematicsJT.length ; i++){
if(kinematicsJT[i].getText().isEmpty()){
String unknown = kinematicsJT[i];
}
}
try{
/*double distanceI = Double.parseDouble(dI.getText());
double distanceF = Double.parseDouble(d0.getText());
double v0 = Double.parseDouble(velocityI.getText());
double v1 = Double.parseDouble(velocityF.getText());
double vA = Double.parseDouble(velocityAverage.getText());
double aA = Double.parseDouble(accelerationAverage.getText());
*/
} catch(NumberFormatException ex){
JOptionPane.showConfirmDialog(null, "Numbers Only");
}
}
});```

10. ## Re: Testing double null

user is actually just trying to to find acceleration.
Would the actionlistener know what the user wants to find and be able to see if the required data has been entered in some textfields? If any required data is missing, issue an error message and go back to the user for correction.

11. ## Re: Testing double null

My thinking was that, as you know, one equation with one unknown, so when the program executes the empty JTF would be assigned the unknown variable. The kinematic class contains the get/set methods, and those values that have been checked and parsed (check method) would be passed to a "calculation" method that contains if/else statements each containing the equations to solve for particular unknown.

Screen Shot 2014-05-02 at 3.05.20 PM.jpg

The logic makes sense, but the code is another story.

12. ## Re: Testing double null

Where can a missing entry be detected? How can that information be passed to a method for processing?

13. ## Re: Testing double null

That's why I had if(kinematicsJT[i].getText().isEmpty()) so that it would then assign the empty JTF a variable "unknown". I would then use set method from kinematic class to assign the unknown in the kinematic class.

Is any of what I'm talking about even possible, or am I heading down a dark path? I mean there must be a way for this to work.

14. ## Re: Testing double null

If you passed wrapper class objects: Double vs double, the value could be null.

15. ## Re: Testing double null

I have never used wrapper classes....but I was thinking, what if I assigned an integer number to an "unknown" variable depending on which JTF in the array was left blank. This would tell the switch/case statement in the kinematic class which equation to use for the calculation. May not be elegant but it should work..right?

16. ## Re: Testing double null

What info do you need to pass to the method? What are the ways to pass it?
If you use an object to pass a value to a method, then a value of null would show the data is missing.
Another way would be to use a variable to flag which of the other variables have no value. For example a boolean array with true/false values for which variables are defined and which are not defined.
callTheMethod(var1, var2, var3, boolArray);
boolArray would have 3 elements corresponding to var1, var2 and var3

17. ## Re: Testing double null

Norm, I hope I am on the right path here...

Method I created to check JTF and assign unknown. It's the only thing I could really understand and work with, so hopefully it's right. The calculate AL is just a rough draft to see if this approach will work and verify I can get a result. Problem, when I try to invoke the method using the kinematics object, this error shows up:
HTML Code:
```cannot find symbol
symbol:   method checkJTF(JTextField[])
location: variable kinematics of type Kinematics```
``` public void checkJTF(JTextField[] a){
for (int i = 0 ; i < a.length ; i++){
if(a[i].getText().isEmpty()){
int unknown = i;
kinematics.setUnknown(unknown);
}
}
}```

Calculate ActionListener:

```calculate.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
kinematics.checkJTF(kinematicsJT);
try{
float distanceI = Float.parseFloat(dI.getText());
kinematics.setDistanceI(distanceI);
double distanceF = Double.parseDouble(d0.getText());
double v0 = Double.parseDouble(velocityI.getText());
double v1 = Double.parseDouble(velocityF.getText());
double vA = Double.parseDouble(velocityAverage.getText());
double aA = Double.parseDouble(accelerationAverage.getText());

} catch(NumberFormatException ex){
JOptionPane.showConfirmDialog(null, "Numbers Only");
}
}
});```

Not sure why the float distance and float time would not work when I tried to use them in the equations.
This is the switch/case I will use in the kinematic class:

```public Kinematics() {
System.out.println(unknown);
//float distance = distanceF - distanceI;
//float time = timeF - timeI;

if (unknown >= 0 && unknown <= 6) {
switch (unknown) {
case 1:
velocityF = velocityI + acceleration * (timeF - timeI);
case 2:
distanceF = distanceI + ((velocityI + velocityF) / 2) * (timeF - timeI);
case 3:
velocityF = (float) Math.sqrt(velocityI * velocityI) + 2 * acceleration * (distanceF - distanceI);
case 4:
distanceF = (float) (velocityF * (timeF - timeI) + 0.5 * (acceleration * ((timeF - timeI) * (timeF - timeI))));
case 5:
velocityAverage = (distanceF - distanceI) / (timeF - timeI);
}
}
}```

18. ## Re: Testing double null

cannot find symbol
symbol: method checkJTF(JTextField[])
The compiler can not find a definition for the method named in the error message. Where is that method define?
Is it in scope where the code tries to use it?

The code snippets don't show enough.

19. ## Re: Testing double null

This is both classes in their entirety. Hopefully this will work. Thanks a lot for help.

```package physicspractice;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Frame extends JFrame{

//Objects
final Kinematics kinematics = new Kinematics();

public Frame() throws IOException {
super("PhysAssist");

// Buttons
JButton kinematic = new JButton("Kinematic");
kinematic.setMnemonic(KeyEvent.VK_K);
JButton dynamics = new JButton("Dynamics");
dynamics.setMnemonic(KeyEvent.VK_D);
JButton power = new JButton("Power and Energy");
power.setMnemonic(KeyEvent.VK_E);
JButton wave = new JButton("Wave");
wave.setMnemonic(KeyEvent.VK_W);
JButton sound = new JButton("Sound");
sound.setMnemonic(KeyEvent.VK_S);
JButton circuit = new JButton("Circuit");
circuit.setMnemonic(KeyEvent.VK_C);

// Execute Buttons
JButton calculate = new JButton("Calculate");
calculate.setMnemonic(KeyEvent.VK_ENTER);
calculate.setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
calculate.setForeground(Color.BLUE);
JButton back = new JButton("Back");
back.setMnemonic(KeyEvent.VK_ESCAPE);
back.setFont(new java.awt.Font("Georgia", Font.ITALIC, 16));
back.setForeground(Color.RED);

// Button Array Characteristics
final JButton[] buttonArray = { kinematic, dynamics, power, wave,
sound, circuit };
for (int i = 0; i < buttonArray.length; i++) {
buttonArray[i].setFont(new java.awt.Font("Georgia", Font.BOLD, 16));
buttonArray[i].setForeground(Color.darkGray);
buttonArray[i].setBackground(Color.GRAY);
}

// Panels
final JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());

final JPanel executePanel = new JPanel();
executePanel.setLayout(new FlowLayout());

final JPanel sidePanel = new JPanel();
sidePanel.setLayout(new BorderLayout());

final JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 1));
buttonPanel.setBackground(Color.DARK_GRAY);

// Kinematic Pane and Contents
final JPanel kinematicPane = new JPanel(new GridLayout(6, 2));
final JTextField dI = new JTextField();
final JTextField d0 = new JTextField();
final JTextField velocityI = new JTextField();
final JTextField velocityF = new JTextField();
final JTextField velocityAverage = new JTextField();
final JTextField accelerationAverage = new JTextField();
final JTextField [] kinematicsJT = {dI, d0, velocityI, velocityF,
velocityAverage, accelerationAverage};

// Dynamics Pane
final JPanel dynamicsPane = new JPanel(new GridLayout(6, 2));
final JTextField force = new JTextField();
final JTextField massOne = new JTextField();
final JTextField massTwo = new JTextField();
final JTextField radiusOne = new JTextField();
final JTextField radiusTwo = new JTextField();
final JTextField frictionNormal = new JTextField();

// Power, Work, Energy Pane
final JPanel powerPane = new JPanel(new GridLayout(7, 2));
final JTextField work = new JTextField();
final JTextField energy = new JTextField();
final JTextField powerP = new JTextField();
final JTextField mass = new JTextField();
final JTextField timeOne = new JTextField();
final JTextField timeTwo = new JTextField();
final JTextField velocity = new JTextField();
final JTextField powerForce = new JTextField();

final JPanel wavePane = new JPanel();

final JPanel soundPane = new JPanel();

final JPanel circuitPane = new JPanel(new GridLayout(3, 2));
final JTextField voltage = new JTextField();
final JTextField current = new JTextField();
final JTextField resistance = new JTextField();

/*final JPanel imagePanel = new JPanel();
imagePanel.setBackground(Color.DARK_GRAY);
"physicsImg.jpg"));
ImageLabel image = new ImageLabel(bfimage);
JLabel headerLabel = new JLabel(new ImageIcon(bfimage));
*/

validate();

public void actionPerformed(ActionEvent ae) {
for (int i = 0; i < buttonArray.length; i++) {
mainPanel.remove(buttonArray[i]);
}
mainPanel.remove(sidePanel);
mainPanel.revalidate();
mainPanel.repaint();
}
});

public void actionPerformed(ActionEvent ae){
kinematics.checkJTF(kinematicsJT);
try{
float distanceI = Float.parseFloat(dI.getText());
kinematics.setDistanceI(distanceI);
float distanceF = Float.parseFloat(d0.getText());
float v0 = Float.parseFloat(velocityI.getText());
float v1 = Float.parseFloat(velocityF.getText());
float vA = Float.parseFloat(velocityAverage.getText());
float aA = Float.parseFloat(accelerationAverage.getText());

} catch(NumberFormatException ex){
JOptionPane.showConfirmDialog(null, "Numbers Only");
}
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();

float f = Float.parseFloat(force.getText());
float m1 = Float.parseFloat(massOne.getText());
float m2 = Float.parseFloat(massTwo.getText());
float fN = Float.parseFloat(frictionNormal.getText());
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();

float w = Float.parseFloat(work.getText());
float e = Float.parseFloat(energy.getText());
float p = Float.parseFloat(powerP.getText());
float m = Float.parseFloat(mass.getText());
float t1 = Float.parseFloat(timeOne.getText());
float t2 = Float.parseFloat(timeTwo.getText());
float v = Float.parseFloat(velocity.getText());
float pF = Float.parseFloat(powerForce.getText());
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();
}
});

public void actionPerformed(ActionEvent ae) {
//mainPanel.remove(imagePanel);
mainPanel.revalidate();
mainPanel.repaint();

float volt = Float.parseFloat(voltage.getText());
float cI = Float.parseFloat(current.getText());
float rR = Float.parseFloat(resistance.getText());
}
});

}
public void checkJTF(JTextField[] a){
for (int i = 0 ; i < a.length ; i++){
if(a[i].getText().isEmpty()){
int unknown = i;
kinematics.setUnknown(unknown);
}
}
}
}```

```package physicspractice;

public class Kinematics {

float distanceI, distanceF, velocityI, velocityF,
velocityAverage, acceleration, timeF, timeI;
int unknown;

public Kinematics() {
System.out.println(unknown);
//float distance = distanceF - distanceI;
//float time = timeF - timeI;

if (unknown >= 0 && unknown <= 6) {
switch (unknown) {
case 1:
velocityF = velocityI + acceleration * (timeF - timeI);
case 2:
distanceF = distanceI + ((velocityI + velocityF) / 2) * (timeF - timeI);
case 3:
velocityF = (float) Math.sqrt(velocityI * velocityI) + 2 * acceleration * (distanceF - distanceI);
case 4:
distanceF = (float) (velocityF * (timeF - timeI) + 0.5 * (acceleration * ((timeF - timeI) * (timeF - timeI))));
case 5:
velocityAverage = (distanceF - distanceI) / (timeF - timeI);
}
}
}

public void setDistanceI(float distanceI) {
this.distanceI = distanceI;
}

public void setDistanceF(float distanceF) {
this.distanceF = distanceF;
}

public void setVelocityI(float velocityI) {
this.velocityI = velocityI;
}

public void setVelocityF(float velocityF) {
this.velocityF = velocityF;
}

public void setVelocityAverage(float velocityAverage) {
this.velocityAverage = velocityAverage;
}

public void setAcceleration(float acceleration) {
this.acceleration = acceleration;
}

public void setUnknown(int unknown) {
this.unknown = unknown;
}

public int getUnknown() {
return unknown;
}

public double getDistanceI() {
return distanceI;
}

public double getDistanceF() {
return distanceF;
}

public double getVelocityI() {
return velocityI;
}

public double getVelocityF() {
return velocityF;
}

public double getVelocityAverage() {
return velocityAverage;
}

public double getAcceleration() {
return acceleration;
}
}```

20. ## Re: Testing double null

I can not compile the post code without many compiler errors.
Most are due to missing import statements.

Can you make a small, complete program that shows the error. Not 30+ errors

21. ## Re: Testing double null

Originally Posted by Norm
I can not compile the post code without many compiler errors.
Most are due to missing import statements.

Can you make a small, complete program that shows the error. Not 30+ errors
I replaced the code with both classes as I have them. And it shows only the one error. hopefully the same for you.

22. ## Re: Testing double null

See post#18

NOTE: Frame is the name of a Java SE class. It's better not to use names that conflict with Java SE class names.

23. ## Re: Testing double null

Alright I managed to fix the simple issue with the method by adding the static. I'm using a Sys.out to test the check method and it works when I print the value from within the method, but it will not pass the value to the kinematics class....

Cruising down Java Highway hitting every pothole!

24. ## Re: Testing double null

fix the simple issue with the method by adding the static
That doesn't make sense with the code in post#19

it will not pass the value to the kinematics class.
Can you explain where the code is trying to pass a value to that class?

25. ## Re: Testing double null

I created the kinematics object from the Kinematics class --- within the checkJTF method (located at very bottom of Frame class)... kinematics.setUnknown

By making the checkJTF method static in the Frame class, am I not able to call it within the class.

Page 1 of 2 12 Last