Results 1 to 13 of 13
Thread: if method
- 11-10-2010, 12:25 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
if method
can i use a method inside of an if statement?
i keep getting:
Error: E:\project\Candyland9.java:78: illegal start of expression
for method:
Java Code:public static double calcTotal(double one, double two , double three ) { subtotal = (one + two + three)*tax; total = one + two + three + subtotal; return total;
- 11-10-2010, 12:27 AM #2
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
What if statement? I don't see it in your code.
The answer is yes by the way.
ErikI'm new to Java but I like to help where ever I can. :)
-
You can use a method inside of an if statement, but you can't declare a method there (with some exceptions).
- 11-10-2010, 12:31 AM #4
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
oh i'm so embarrassed to post the whole thing but here it is:
i read that you couldn't use a method inside an if statement, so i thought i'd just ask.Java Code:import javax.swing.JOptionPane; import java.text.DecimalFormat; import java.io.*; import java.util.Scanner; public class Candyland9 { static double dumdums = 1.50; static double snickerballs = 3.00; static double sweettarts = 2.00; static double skittles = 2.75; static final Double tax = .06; static double subtotal; static double total; public static void main (String []args) throws IOException { double choice1; double choice2; double choice3; String input=""; File myFile = new File("E:\\project\\candy.txt"); // 1) open file Scanner inputFile = new Scanner(myFile); // 2) use scanner as input DecimalFormat decimalf = new DecimalFormat("$##.##"); JOptionPane.showMessageDialog(null, "Welcome to CandyLand!"); JOptionPane.showMessageDialog(null,"Items sold here: \n\t" + "1)Dumdums \n\t2)Snickerballs" + "\n\t2)Sweettarts \n\t4)Skittles" + "\n\tPlease enter your choice by number."); while(inputFile.hasNext()) { String inFile = inputFile.nextLine();// 2) read file System.out.print(inFile); // print out } input = JOptionPane.showInputDialog("Enter your first choice."); // need to somehow get user input and assign it to the right price choice1 = Double.parseDouble(input); input = JOptionPane.showInputDialog ("Enter a second choice or enter none"); if(input.equalsIgnoreCase("none")) { total = calcTotal(choice1,0,0); displayMsg(); } else { input = JOptionPane.showInputDialog ("Enter a third choice or enter none?"); } if(input.equalsIgnoreCase("none")) { choice2 = Double.parseDouble(input); total = calcTotal(choice1,choice2,0); displayMsg(); } else { choice2 = Double.parseDouble(input); choice3 = Double.parseDouble(input); total = calcTotal(choice1, choice2,choice3); displayMsg(); } String morecandy; double totalcandy = 0; double morechoices = 0; String additional = JOptionPane.showInputDialog("For customers wanting additonal orders," + "\n\tplease enter Y for yes or N for no."); if (additional.equalsIgnoreCase("Y")) { JOptionPane.showMessageDialog(null,"Items sold here: \n\t" + "1)Dumdums \n\t2)Snickerballs" + "\n\t2)Sweettarts \n\t4)Skittles"); do { morecandy = JOptionPane.showInputDialog("Please make your selection by number." + "\n\t Enter 0 when done."); morechoices = Double.parseDouble(morecandy); // convert morecandy to useable number } while(morechoices != 0); { totalcandy += morechoices; subtotal = totalcandy * tax; total = totalcandy + subtotal; JOptionPane.showMessageDialog(null,"All candy is sold with a" + decimalf.format(tax) + "tax"); JOptionPane.showMessageDialog(null, "Your total is\t " + decimalf.format (total) + "\n\tHave a nice day!"); } } else { JOptionPane.showMessageDialog(null, "Goodbye!"); } // inputFile.close(); //3) close file } } public static double calcTotal(double one, double two, double three ) { subtotal = (one + two + three) * tax; total = one + two + three + subtotal; return total; } public static void displayMsg() { JOptionPane.showMessageDialog (null, "Tax for all items is: " + decimalf.format(tax)); JOptionPane.showMessageDialog(null,"Your total is: $" + total); JOptionPane.showMessageDialog(null, "Thank you for shopping at CandyLand!\n\t" + "Tell a friend!"); }
anyway i keep getting the error message above. i'm still working on this so there are probably a lot of things wrong. *covers face*
any edits to my code will go hereLast edited by smray7; 11-10-2010 at 01:59 AM. Reason: fixed formatting
-
It looks like you're curly braces aren't matching up. First and foremost, fix your code formatting especially the indentation so that it makes sense. Your code as written is almost unreadable and extremely difficult to debug. A little effort towards correcting the formatting will go a long way towards you and us helping to solve your problem.
- 11-10-2010, 12:42 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Hey there,
the if statement is not your problem..
here is your problem (with inner code removed)
you can't have a mthod inside of a Method. both calcTotal and displayMsg are inside main. move these OUTSIDE of the main function :)Java Code:import javax.swing.JOptionPane; import java.text.DecimalFormat; import java.io.*; import java.util.Scanner; public class Candyland9 { static double dumdums = 1.50; static double snickerballs = 3.00; static double sweettarts = 2.00; static double skittles = 2.75; static final Double tax = .06; static double subtotal; static double total; public static void main (String []args) throws IOException { public static double calcTotal(double one, double two , double three ) { } public static void displayMsg() { } } //This is the End of your MAIN method. }Last edited by maknib; 11-10-2010 at 12:49 AM.
- 11-10-2010, 01:21 AM #7
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
thanks maknib...and to you furbarable. I fixed the formatting. is that ok?
and now what does: Error: E:\project\Candyland9.java:111: class, interface, or enum expected mean?
-
It's better but you still have random blocks indented for no reason. You would help yourself and us by reading up on the industry standards for indentation and formatting and follow them. By doing this, more people will be able to analyze your code at a glance and be able to help you, and that's your goal here, right?
It suggests to me that you've got code in a location where it doesn't belong. Show your latest code and please indicate which line causes this error. It is sometimes helpful to look right above the line causing the error to find the true fault with this error.and now what does: Error: E:\project\Candyland9.java:111: class, interface, or enum expected mean?
Luck.
- 11-10-2010, 01:38 AM #9
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
No Problem,
to be honest the only way i was able to figure that out was by going to Google and typing copy paste your Java error in and the very first link said "you have a method in a method"
i couldn't read your code from formatting.
you should read this and choose one that suits you..
personahttp://en.wikipedia.org/wiki/Indent_style
personally i use:
just a style i adoptedJava Code:myStuff() { //2 tabs in sometimes 4 if(){ //if function 2 tabs in }else{ //more stuff } }
- 11-10-2010, 02:05 AM #10
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
File: E:\project\Candyland9.java [line: 107]
Error: E:\project\Candyland9.java:107: class, interface, or enum expected
same error for line 110, 111,112,113,116,117,119
it's primarily dealing with this code:
believe me, i'm this reading up and down, left to right, crossed eyed... and i'm still not figuring things out.Java Code:public static double calcTotal(double one, double two, double three ) { subtotal = (one + two + three) * tax; total = one + two + three + subtotal; return total; } public static void displayMsg() { JOptionPane.showMessageDialog (null, "Tax for all items is: " + decimalf.format(tax)); JOptionPane.showMessageDialog(null,"Your total is: $" + total); JOptionPane.showMessageDialog(null, "Thank you for shopping at CandyLand!\n\t" + "Tell a friend!"); }
-
You'll want to show the whole program and then indicate the exact lines that are causing the errors. We don't know what's going on without all the necessary information, and your showing a portion of your code when you don't know what's causing your error and without indicating which lines are causing the errors won't do it.
Luck.
- 11-10-2010, 03:16 AM #12
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
sounds to me like you have move everyone OUTSIDe of your Class and not between the Main and class ending }
- 11-10-2010, 08:17 PM #13
Member
- Join Date
- Oct 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
ArrayLists compareTo method, equals method
By random0munky in forum New To JavaReplies: 2Last Post: 10-26-2009, 07:20 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM


LinkBack URL
About LinkBacks


Bookmarks