-
Calling methods (Help!)
I've written a generalized inputDimension method that takes two parameters: a String that has one of the two values “length” or “width”, and an integer that can has the values 1 or 2, representing the pizza number. I have to implement this method, which I've done by creating the variable 'dimension.' But now I have to call it, as I did the first one: firstLength = inputDimension(“length”, 1);. But, I keep getting an error about parse. It reads:
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at edu.uwec.cs.jimeneam.lab5.Lab5Start.main(Lab5Start.java:37)
. Any help would be greatly appreciated, as I'd like to learn how to properly execute it.
Code:
package edu.uwec.cs.jimeneam.lab5;
import javax.swing.JOptionPane;
public class Lab5Start {
public static void main (String [] args) {
// --- general variables
String inputString = ""; // input string for variables
String outputString = ""; // output string for message dialog
char continueChar = ' '; // character to hold 'Y' to continue or 'N' to stop
// --- variables for first pizza
double firstLength = 0.0; // length of first pizza if rectangular
double firstWidth = 0.0; // width of first pizza if rectangular
double firstArea = 0.0; // area of first pizza
double firstCost = 0.0; // cost of first pizza
double firstCostPerArea = 0.0; // cost per unit area of first pizza
// --- variables for second pizza
double secondLength = 0.0; // length of second pizza if rectangular
double secondWidth = 0.0; // width of second pizza if rectangular
double secondArea = 0.0; // area of second pizza
double secondCost = 0.0; // cost of second pizza
double secondCostPerArea = 0.0; // cost per unit area of second pizza
// initialize to go through loop first time
continueChar = 'Y';
// --- main processing loop
while (continueChar != 'N') {
// get the length and width of the first pizza
firstLength = inputDimension("length", 1);
//inputString = JOptionPane.showInputDialog(null, "Enter the length of the first pizza in inches");
firstLength = Double.parseDouble(inputString);
firstWidth = inputDimension("width,", 1);
//inputString = JOptionPane.showInputDialog(null, "Enter the width of the first pizza in inches");
//firstWidth = Double.parseDouble(inputString);
// get the cost of the first pizza
inputString = JOptionPane.showInputDialog(null, "Enter the cost of the first pizza");
firstCost = Double.parseDouble(inputString);
// get the dimension(s) of the second pizza
secondLength = inputDimension("Length", 2);
secondLength = Double.parseDouble(inputString);
secondWidth = inputDimension("Width", 2);
secondWidth = Double.parseDouble(inputString);
//inputString = JOptionPane.showInputDialog(null, "Enter the length of the second pizza in inches");
//secondLength = Double.parseDouble(inputString);
//inputString = JOptionPane.showInputDialog(null, "Enter the width of the second pizza in inches");
//secondWidth = Double.parseDouble(inputString);
// get the cost of second pizza
inputString = JOptionPane.showInputDialog(null, "Enter the cost of the second pizza");
secondCost = Double.parseDouble(inputString);
// calculate the area of the first pizza, and format that area
firstArea = firstLength * firstWidth;
firstArea = ((int)(firstArea * 100)) / 100.0;
// calculate the area of the second pizza, and format that area
secondArea = secondLength * secondWidth;
secondArea = ((int)(secondArea * 100)) / 100.0;
// calculate the cost per area of the first pizza, and format that cost per area
firstCostPerArea = firstCost / firstArea;
firstCostPerArea = ((int)(firstCostPerArea * 10000)) / 10000.0;
// calculate the cost per area of the second pizza, and format that cost per area
secondCostPerArea = secondCost / secondArea;
secondCostPerArea = ((int)(secondCostPerArea * 10000)) / 10000.0;
// initialize the output string
outputString = "";
// append the information for the first pizza to the output string
outputString += "\n First pizza: ";
outputString += " Rectangle with length " + firstLength + " inches, width " + firstWidth + " inches";
outputString += ", Area of " + firstArea + " sq. inches, Cost of $" + firstCost + ", Cost per square inch of $" + firstCostPerArea;
// append the information for the second pizza to the output string
outputString += "\n Second pizza: ";
outputString += " Rectangle with length " + secondLength + " inches, width " + secondWidth + " inches";
outputString += ", Area of " + secondArea + " sq. inches, Cost of $" + secondCost + ", Cost per square inch of $" + secondCostPerArea;
// generate and append a comparison string to the output string
if (secondCostPerArea < firstCostPerArea) {
outputString += "\n Pizza 2 is a better value";
}
else if (firstCostPerArea < secondCostPerArea) {
outputString += "\n Pizza 1 is a better value";
}
else {
outputString += "\n Pizza 1 and Pizza 2 are of equal value";
}
// output the output string to a message window
JOptionPane.showMessageDialog(null, outputString);
// ask the user if they want to continue
inputString = JOptionPane.showInputDialog(null, "Do you want to continue? Enter Y to continue, N to quit");
continueChar = inputString.charAt(0);
while (continueChar != 'Y' && continueChar != 'N') {
inputString = JOptionPane.showInputDialog(null, "Do you want to continue? Enter Y to continue, N to quit");
continueChar = inputString.charAt(0);
}
} // end - while loop
} // end - main method
// other methods go here
// method to input a dimension in general
public static double inputDimension(String dimensionName, int pizzaNumber) {
// declare local variables for an input string and the dimension as a general value
String inputString = "";
double dimension = 0.0;
// display an input box to get the "dimensionName" dimension for pizza "pizzaNumber"
inputString = JOptionPane.showInputDialog(null,"Enter the " + dimensionName + " of pizza " + pizzaNumber + ":");
// parse that input string into the dimension variable
dimension = Double.parseDouble(inputString);
// return the dimension
return dimension;
}
// method to calculate area of pizza
} // end - class Lab5
-
Take a look at the line the error is pointing to. The last line of the exception output is:
Code:
at edu.uwec.cs.jimeneam.lab5.Lab5Start.main(Lab5Start.java:37)
which points to the following:
Code:
firstLength = inputDimension("length", 1);
//inputString = JOptionPane.showInputDialog(null, "Enter the length of the first pizza in inches");
firstLength = Double.parseDouble(inputString); // <-- exception points here
firstWidth = inputDimension("width,", 1);
You have two copies of inputString One is in your main routine, and it is initialized to an empty string and nothing else is put in it.
The second is in the inputDimension method where it is used to receive the user input. It is in no way connected to the one in your main.
If you look at the lines of code I show above, you are setting firstLength with your inputDimension method (this is good), but then you immediately try to overwrite it with the Double.parseDouble line (this is bad).
Double.parseDouble throws because inputString is still empty and you can't parse a double from nothing. The good news is this parse is unnecessary because you've already parsed the input value in your inputDimension method and passed back a good double. Remove the parseDouble line in your main method and things should work better. Take a look at where you have similar code, and remember you are getting the double from the inputDimension method.
BTW, from my point of view, you should delete all the commented out code. It just makes things harder to read and is a horrible backup method.
Similar Threads
-
Replies: 4
Last Post: 02-14-2007, 09:51 AM
-
By Patrick Troughton in forum .NET
Replies: 78
Last Post: 08-13-2002, 11:18 AM
-
Replies: 0
Last Post: 08-07-2002, 08:13 AM
-
Replies: 1
Last Post: 11-09-2000, 05:38 PM
-
By Vesa Kilpelänaho in forum Java
Replies: 0
Last Post: 11-08-2000, 02:20 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|