Results 1 to 18 of 18
Thread: Code will not compile
- 05-09-2009, 07:04 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
[SOLVED] Code will not compile
I'm not sure why I am getting this, this is my code.
Java Code://This is the main program, it calculates information using the class import javax.swing.JOptionPane; public class Project { public static void main(String args[]) { double width = 0; double length = 0; String firstAnswer; String secondAnswer = "yes"; String lengthString; String widthString; String netSpeedString; double netSpeed = 0; DigitalPhoto photo = new DigitalPhoto(); firstAnswer = JOptionPane.showInputDialog("Do you have a photo you would like us to scan (Yes or No)"); if(firstAnswer.equalsIgnoreCase("Yes")) { netSpeedString = JOptionPane.showInputDialog("What is the speed of your Internet connection (in bits/second)? "); netSpeed = Double.parseDouble.(netSpeedString); while(secondAnswer.equalsIgnoreCase("Yes")) { widthString = JOptionPane.ShowInputDialog("What is the width of your photo in inches?"); width = Double.parseDouble.(widthString); while(width > 8) { System.out.println("Sorry, we cannot handle a photo of that size"); widthString = JOptionPane.ShowInputDialog("What is the width of your photo in inches?"); width = Double.parseDouble.(widthString); } lengthString = JOptionPane.ShowInputDialog("What is the length of your photo in inches?"); length = Double.parseDouble.(lengthString); while(length > 8) { System.out.println("Sorry, we cannot handle a photo of that size"); lengthString = JOptionPane.ShowInputDialog("What is the length of your photo in inches?"); length = Double.parseDouble.(widthString); } photo.showValues(); } } if(firstAnswer.!equalsIgnoreCase("Yes")) { System.out.println("Thanks for stopping by, come again soon"); } } }
Java Code:C:\Users\Jackson\Project.java:21: <identifier> expected netSpeed = Double.parseDouble.(netSpeedString); ^ C:\Users\Jackson\Project.java:26: <identifier> expected width = Double.parseDouble.(widthString); ^ C:\Users\Jackson\Project.java:32: <identifier> expected width = Double.parseDouble.(widthString); ^ C:\Users\Jackson\Project.java:36: <identifier> expected length = Double.parseDouble.(lengthString); ^ C:\Users\Jackson\Project.java:42: <identifier> expected length = Double.parseDouble.(widthString); ^ C:\Users\Jackson\Project.java:49: <identifier> expected if(firstAnswer.!equalsIgnoreCase("Yes")) ^ C:\Users\Jackson\Project.java:49: ';' expected if(firstAnswer.!equalsIgnoreCase("Yes"))
Last edited by ShotGunRockets; 05-10-2009 at 04:43 AM.
- 05-09-2009, 07:07 PM #2
You haven't declared the types of any of these variables.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-09-2009, 07:11 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Well I did initialize them at the top, am I missing what you mean?
- 05-09-2009, 07:28 PM #4
No, my bad. What's actually wrong is all the dots before the brackets. And the randomly inserted !s.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-09-2009, 07:55 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Okay thank you that cleared up some of the problem. But what exactly do you mean by the randomly inserted !s, where should they go?
-
before the object.
Java Code:if (!myString.equalsIgnoreCase("foo")) { //... }
- 05-09-2009, 08:09 PM #7
You don't yet appear to have a grasp of Java syntax.
if (expression) {code} --> executes "code" if "expression" evaluates to true
!expression --> evaluates to true if "expression" evaluates to false, and vice versa
The following are all expressions:
Class.name --> The static field "name" in "Class"
var.name --> The instance field "name" of the object in variable "var"
var.name() --> Returns the result of the instance method "name()" of the object in variable "var"
var.name(foo) --> As above but using the argument foo
The . loosely means "inside" (reading the expression backwards)Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-09-2009, 08:10 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Alright, I fixed that but now I get these errors:
Java Code:C:\Users\Jackson\Project.java:25: cannot find symbol symbol : method ShowInputDialog(java.lang.String) location: class javax.swing.JOptionPane widthString = JOptionPane.ShowInputDialog("What is the width of your photo in inches?"); ^ C:\Users\Jackson\Project.java:31: cannot find symbol symbol : method ShowInputDialog(java.lang.String) location: class javax.swing.JOptionPane widthString = JOptionPane.ShowInputDialog("What is the width of your photo in inches?"); ^ C:\Users\Jackson\Project.java:35: cannot find symbol symbol : method ShowInputDialog(java.lang.String) location: class javax.swing.JOptionPane lengthString = JOptionPane.ShowInputDialog("What is the length of your photo in inches?"); ^ C:\Users\Jackson\Project.java:41: cannot find symbol symbol : method ShowInputDialog(java.lang.String) location: class javax.swing.JOptionPane lengthString = JOptionPane.ShowInputDialog("What is the length of your photo in inches?");
Last edited by ShotGunRockets; 05-09-2009 at 11:46 PM.
- 05-09-2009, 08:11 PM #9
Also note that Java is case sensitive. ShowInputDialog is not showInputDialog.
For "not equal to (ignore case)" you needJava Code:(!firstAnswer.equalsIgnoreCase("Yes"))
- 05-09-2009, 08:23 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Thank you very much Darryl.Burke, that was a very helpful hint and now that code compiles :D
Now I have another question, I can now see that my class file has some errors. The program I am trying to make should produce an output like this:
Java Code:Scanned Photo Details Resolution: 600.00 pixels per inch Compression Ratio: 3.50 Required Storage: 2.06 Cost of Scanning Photo: $2.40
Java Code:public class DigitalPhoto { //I will declare the variables here private double width; private double length; private double scanAccuracy = 360000; private double compression = 3.5; private double Mbytes = 0; private double totalCost = 0.00; private double cost = 0.00; private double transferTimeBits = 100000; private double netSpeed = 0; private double resolution = 600.00; private double storage = 0; private double totalStorage = 0; private final int DIVISOR = 60; //Here is my constructor methed DigitalPhoto() { width = 4; length = 4; compression = 3.5; cost = 0.00; storage = 2.06; netSpeed = transferTimeBits / 8000000; totalStorage = Mbytes + totalStorage; totalCost = totalCost + cost; } public void showValues() { System.out.println("Scanned Photo Details"); System.out.println("Resolution: " + resolution + " pixels per inch"); System.out.println("Compression Ratio: " + compression); System.out.println("Required Storage: " + storage); System.out.println("Cost of Scanning Photo: " + cost); } //Here I am getting the values that I will work with in this class public double getWidth() { return width; } public double getLength() { return length; } public double getTransferTimeBits() { return transferTimeBits; } public void setWidth(double thisWidth) { width = thisWidth; } public void setLength(double thisLength) { length = thisLength; } public void setTransferTimeBits(double thisTime) { transferTimeBits = thisTime; } double area = width * length; //Below are my methods of how to get the correct information. double transferTimeSeconds = transferTime(totalStorage, netSpeed); int transferTimeSecondsInt = (int)transferTimeSeconds; double transferTimeMinutes = transferTimeSecondsInt / DIVISOR; double leftOverTime = transferTimeSecondsInt % DIVISOR; //I created a special method for calculating the cost of a photo public static double cost(double area) { double cost = .12 * area; return cost; } public static double datasize(double w, double L, double scanAccuracy, double compress) { double bytes = (w * L * scanAccuracy) / compress; double Mbytes = bytes / 1000000; return Mbytes; } public static double transferTime(double totalStorage, double netSpeed) { double transferTime = totalStorage / netSpeed; return transferTime; } }
Last edited by ShotGunRockets; 05-09-2009 at 11:46 PM.
- 05-09-2009, 08:34 PM #11
Read this Trail: Learning the Java Language (The Java™ Tutorials)
You are assigning different constants to the same variable repeatedly eg:
Java Code:private double storage = 0; ... storage = 2.06;
Java Code://Below are my methods of how to get the correct information. double transferTimeSeconds = transferTime(totalStorage, netSpeed); int transferTimeSecondsInt = (int)transferTimeSeconds; double transferTimeMinutes = transferTimeSecondsInt / DIVISOR; double leftOverTime = transferTimeSecondsInt % DIVISOR;
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-09-2009, 09:46 PM #12
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Thank you OrangeDog, I have read that tutorial but when I try to insert an object into my class like the tutorial says
Java Code:photo.setLength(length);
Java Code:C:\Users\Jackson\Project.java:46: cannot find symbol symbol : method setLength(double) location: class DigitalPhoto photo.setLength(length);
Last edited by ShotGunRockets; 05-09-2009 at 11:49 PM.
-
The error message usually tells you exactly (or almost exactly) what's going wrong here. So, does your DigitalPhoto class have a setLength method that takes a double value as its parameter?
- 05-09-2009, 10:17 PM #14
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Well I'm very new at this, but I believe it does which is why I'm confused, here is my latest DigitalPhoto class:
Java Code:public class DigitalPhoto { //I will declare the variables here private double width; private double length; private double thisWidth; private double thisLength; private double scanAccuracy = 360000; private double compression; private double Mbytes = 0; private double totalCost = 0.00; private double cost; private double transferTimeBits = 100000; private double totalTimeMinutes; private double totalTimeSeconds; private double netSpeed = 0; private double resolution = 600.00; private double storage; private double totalStorage = 0; private final int DIVISOR = 60; private double thisWidth2 = setWidth(width); //Here is my constructor methed public DigitalPhoto() { width = thisWidth2; length = 0; compression = 3.5; cost = 0; storage = 0; netSpeed = transferTimeBits / 8000000; totalStorage = Mbytes + totalStorage; totalCost = totalCost + cost; double transferTimeSeconds = transferTime(totalStorage, netSpeed); int transferTimeSecondsInt = (int)transferTimeSeconds; double transferTimeMinutes = transferTimeSecondsInt / DIVISOR; double leftOverTime = transferTimeSecondsInt % DIVISOR; } public void showValues() { System.out.println("Scanned Photo Details"); System.out.println("Resolution: " + resolution + " pixels per inch"); System.out.println("Compression Ratio: " + compression); System.out.println("Required Storage: " + storage); System.out.println("Cost of Scanning Photo: $" + cost); } public void showValues2() { System.out.println("Your photos will cost $" + totalCost); System.out.println("and will require " + totalStorage + " Mbytes of storage"); System.out.println("and " + totalTimeMinutes + "minutes, " + totalTimeSeconds + " seconds to transfer"); System.out.println(); } //Here I am getting the values that I will work with in this class public double getWidth() { return width; } public double getLength() { return length; } public double getTransferTimeBits() { return transferTimeBits; } public double setWidth(double thisWidth) { width = thisWidth; return thisWidth; } public double setLength(double thisLength) { length = thisLength; return thisLength; } public double setTransferTimeBits(double thisTime) { transferTimeBits = thisTime; return thisTime; } double area = width * length; //I created a special method for calculating the cost of a photo public static double cost(double area) { double cost = .12 * area; return cost; } public static double datasize(double w, double L, double scanAccuracy, double compress) { double bytes = (w * L * scanAccuracy) / compress; double Mbytes = bytes / 1000000; return Mbytes; } public static double transferTime(double totalStorage, double netSpeed) { double transferTime = totalStorage / netSpeed; return transferTime; } }
Last edited by ShotGunRockets; 05-09-2009 at 11:48 PM.
- 05-09-2009, 11:42 PM #15
Firstly, you're using QUOTE tags instead of CODE tags.
At this stage i would suggest chucking this class away and starting again. There appear to be far too many instance variables. As an example this
Java Code:private double thisWidth2 = setWidth(width);
Think about what properties an individual image might have, and only include those as fields. You don't need things the will be calculated from other fields.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-10-2009, 03:32 AM #16
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Thanks again OrangeDog, I have done exactly what you said and I am very close to completing this program! I only have one more problem, the time will not calculate, this is my output:
Your photos will cost $3.00
and will require 2.57 Mbytes of storage
and will take 0 minutes, 0 seconds to transfer
Java Code://This is the main program, it calculates information using the class import java.text.DecimalFormat; import javax.swing.JOptionPane; public class Project { public static void main(String args[]) { DecimalFormat dollarsAndCents = new DecimalFormat("0.00"); DigitalPhoto photo = new DigitalPhoto(); double width; double length; double area; double totalStorage = 0; double totalCost = 0; double scanAccuracy = 360000; double compress = 3.5; String firstAnswer; String secondAnswer = "yes"; String lengthString; String widthString; String netSpeedString; //First I will determine whether the user wants to scan a photo or not. firstAnswer = JOptionPane.showInputDialog("Do you have a photo you would like us to scan (Yes or No)"); if(firstAnswer.equalsIgnoreCase("Yes")) { netSpeedString = JOptionPane.showInputDialog("What is the speed of your Internet connection (in bits/second)? "); double netSpeed = Double.parseDouble(netSpeedString); //Now I'll prompt the user for the values that are needed for calculation. while(secondAnswer.equalsIgnoreCase("Yes")) { widthString = JOptionPane.showInputDialog("What is the width of your photo in inches?"); width = Double.parseDouble(widthString); //This ensures the user will enter a valid width while(width > 8) { System.out.println("Sorry, we cannot handle a photo of that size"); System.out.println(); widthString = JOptionPane.showInputDialog("What is the width of your photo in inches?"); width = Double.parseDouble(widthString); } lengthString = JOptionPane.showInputDialog("What is the length of your photo in inches?"); length = Double.parseDouble(lengthString); //This ensures the user will enter a valid length while(length > 8) { System.out.println("Sorry, we cannot handle a photo of that size"); System.out.println(); lengthString = JOptionPane.showInputDialog("What is the length of your photo in inches?"); length = Double.parseDouble(widthString); } //I now am calling to my class, so I can display the values for the photo photo.datasize(width, length, scanAccuracy, compress); photo.cost(photo.area(width, length)); photo.showValues(); //Right here, I total up the values for the final statement of the program totalStorage = totalStorage + photo.datasize(width, length, scanAccuracy, compress); totalCost = totalCost + photo.cost(photo.area(width, length)); secondAnswer = JOptionPane.showInputDialog("Do you have more photos you would like us to scan (Yes or No)"); } double transferTimeSeconds = photo.transferTime(totalStorage, netSpeed); int transferTimeSecondsInt = (int)transferTimeSeconds; int transferTimeMinutes = transferTimeSecondsInt / 60; int leftOverTime = transferTimeSecondsInt % 60; System.out.println("Your photos will cost $" + dollarsAndCents.format(totalCost)); System.out.println("and will require " + dollarsAndCents.format(totalStorage) + " Mbytes of storage"); System.out.println("and will take " + transferTimeMinutes + " minutes, " + leftOverTime + " seconds to transfer"); } if(!firstAnswer.equalsIgnoreCase("Yes")) { System.out.println("Thanks for stopping by, come again soon"); } } }
Java Code://I imported the decimal format import java.text.DecimalFormat; public class DigitalPhoto { //Here are my variable declaratoins DecimalFormat dollarsAndCents = new DecimalFormat("0.00"); double scanAccuracy = 360000; double compress = 3.5; double Mbytes; double transferTimeBits; static double transferTimeSeconds; static double storage; static double cost; static double area; //Here is my method to convert seconds to an integer value public static double transferTime(double totalStorage, double netSpeed) { double transferTime = totalStorage / netSpeed; return transferTime; } //Here is how I calculate how much space the photo will require public static double datasize(double width, double length, double scanAccuracy, double compress) { double bytes = (width * length * scanAccuracy) / compress; double Mbytes = bytes / 1000000; storage = Mbytes; return Mbytes; } //I use both of the methods below together in the main program public static double area(double width, double length) { double area1 = width * length; area = area1; return area1; } public static double cost(double area) { double cost1 = area * .12; return cost1; } public void showValues() { System.out.println("Scanned Photo Details"); System.out.println("Resolution: 600.00 pixels per inch"); System.out.println("Compression Ratio: " + dollarsAndCents.format(compress)); System.out.println("Required Storage: " + dollarsAndCents.format(storage)); System.out.println("Cost of Scanning Photo: $" + dollarsAndCents.format(cost(area))); System.out.println(); } }
-
In your Project class, I want you to add one line of code where I show you:
Java Code:double transferTimeSeconds = photo.transferTime(totalStorage, netSpeed); // add this: System.out.println("transferTime: " + transferTimeSeconds);
Moral: Sometimes, your code may be correct, but your expectations for the output wrong.
Lesson: Use println statements generously when testing your code.
- 05-10-2009, 04:31 AM #18
Member
- Join Date
- Mar 2009
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Running .java-files won't work/compile does!(Code inside)
By wyldstyle in forum New To JavaReplies: 6Last Post: 02-06-2009, 09:05 PM -
Compile/Execute code in Java app
By Doctor Cactus in forum New To JavaReplies: 5Last Post: 12-16-2008, 10:58 AM -
doesn't compile?!
By jon80 in forum New To JavaReplies: 8Last Post: 06-14-2008, 06:42 PM -
simple problem - code wont compile
By dirtycash in forum New To JavaReplies: 1Last Post: 11-20-2007, 06:49 PM
Bookmarks