I am creating a Pool.java code for class final and all we need to do is to create a program that calculate the volume of the pool after the user enters the size of the pool. I have only one line where my code is giving me a error. The line is:
Total = sLength * sWidth * sdepth;

Can anyone take a look at it and let me know where I went wrong. Thank you.

Here is my code already:

import java.io.*;
import java.text.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class SwimCalc {
private JFrame mainFrame;
private JButton calcButton, exitButton;
private JTextField length, width, depth, volume;
private JLabel lengthLabel, widthLabel, depthLabel, volumeLabel;
public SwimCalc() {
mainFrame = new JFrame("Montalbano Swimming Pool Volume Calculator");
calcButton = new JButton("Calculate Volume");
exitButton = new JButton("Exit");
length = new JTextField(5);
width = new JTextField(5);
depth = new JTextField(5);
volume = new JTextField(5);
lengthLabel = new JLabel("Enter the length of the swimming pool: ");
widthLabel = new JLabel("Enter the width of the swimming pool: ");
depthLabel = new JLabel("Enter the depth dept of the swimming pool: ");
volumeLabel = new JLabel("Swimming pool volume: ");
JPanel c=new JPanel(new GridLayout(5,2));
c.add(lengthLabel);
c.add(length);
c.add(widthLabel); 
c.add(width);
c.add(depthLabel);
c.add(depth);
c.add(volumeLabel);
c.add(volume);
c.add(calcButton);
c.add(exitButton);
calcButton.setMnemonic('C');
exitButton.setMnemonic('x');
mainFrame.setSize(500,200);
mainFrame.add(c);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
calcButtonHandler chandler =new calcButtonHandler();
calcButton.addActionListener(chandler);
exitButtonHandler ehandler =new exitButtonHandler();
exitButton.addActionListener(ehandler);
FocusHandler fhandler =new FocusHandler();
length.addFocusListener(fhandler);
width.addFocusListener(fhandler);
depth.addFocusListener(fhandler);
volume.addFocusListener(fhandler);
mainFrame.setVisible(true);
}
class calcButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
DecimalFormat num =new DecimalFormat(",###.##");
Double cLength, cWidth, cdepth, cVolume, Total;
String sLength, sWidth, sdepth, sVolume;
sLength =length.getText();
cLength = Double.valueOf(sLength);
sWidth =width.getText();
cWidth = Double.valueOf(sWidth);
sdepth =depth.getText();
cdepth = Double.valueOf(sdepth);
if(e.getSource() == calcButton) {
Total = sLength * sWidth * sdepth;//this is the error that I have; the sLength * sWidth is the problem
volume.setText(num.format(Total));
try{
String value=volume.getText();
File file = new File("output.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("Length= "+sLength+", Width= "+sWidth+", Depth= "+sdepth+" so the volume of Swimming Pool is "+value);
out.newLine();
out.close();
}
catch(Exception ex){}
}
}
}
class exitButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
class FocusHandler implements FocusListener {
public void focusGained(FocusEvent e) {
}
public void focusLost(FocusEvent e) {
}
}
public static void main(String args[]){
new SwimCalc();
}
}

Recommended Answers

All 5 Replies

Next time, please rap your codes inside the CODE-tag

You dont say what the error is. Please be more specific.

sLength, sWidth and sDepth are Strings. You cannot multiply strings. To convert a String that contains a number, to an int/double etc.. You must parse it.

int sWidth = Integer.parseInt(width.getText());

Try that.

The error that I am getting from the original code is:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
The operator * is undefined for the argument type(s) java.lang.String, java.lang.String

sLength, sWidth and sDepth are Strings. You cannot multiply strings. To convert a String that contains a number, to an int/double etc.. You must parse it.

int sWidth = Integer.parseInt(width.getText());

Try that.

When I went to repalce the code with the new int sWidth and the others it gave me a error on the (width.getText()); part.

When I went to repalce the code with the new int sWidth and the others it gave me a error on the (width.getText()); part.

Well, it is something you are doing wrong if you get an error.
But sorry, I see in your code you use the Double.valueOf method to get the value inside the TextField. So the only problem you actually had was using sLength,sWidth,sHeight instead of cLength,cWidth,cHeight. You can also contain each declaration to 1 line.

double sLength, sWidth, sdepth, sVolume;

sLength = Double.valueOf(length.getText());

sWidth =Double.valueOf(width.getText());

sdepth =Double.valueOf(depth.getText());

if(e.getSource() == calcButton) {
Total = sLength * sWidth * sdepth;
volume.setText(num.format(Total));

Just so you know, parseDouble() returns primitive double type, but valueOf returns java.lang.Double, which is the object representative of the double. There are circumstances where you might want an Double object, instead of primitive type.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.