Results 1 to 13 of 13

Thread: if method

  1. #1
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default 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;

  2. #2
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    What if statement? I don't see it in your code.

    The answer is yes by the way.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can use a method inside of an if statement, but you can't declare a method there (with some exceptions).

  4. #4
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    oh i'm so embarrassed to post the whole thing but here it is:

    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!");
      }
    i read that you couldn't use a method inside an if statement, so i thought i'd just ask.

    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 here
    Last edited by smray7; 11-10-2010 at 02:59 AM. Reason: fixed formatting

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  6. #6
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Hey there,

    the if statement is not your problem..

    here is your problem (with inner code removed)

    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.   
    }
    you can't have a mthod inside of a Method. both calcTotal and displayMsg are inside main. move these OUTSIDE of the main function :)
    Last edited by maknib; 11-10-2010 at 01:49 AM.

  7. #7
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by smray7 View Post
    thanks maknib...and to you furbarable. I fixed the formatting. is that ok?
    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?

    and now what does: Error: E:\project\Candyland9.java:111: class, interface, or enum expected mean?
    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.

    Luck.

  9. #9
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by smray7 View Post
    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?
    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:

    Java Code:
    myStuff() {
      //2 tabs in sometimes 4
      if(){
        //if function 2 tabs in
      }else{
        //more stuff
      }
    }
    just a style i adopted

  10. #10
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    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:
    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!");
      }
    believe me, i'm this reading up and down, left to right, crossed eyed... and i'm still not figuring things out.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  12. #12
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    sounds to me like you have move everyone OUTSIDe of your Class and not between the Main and class ending }

  13. #13
    smray7 is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by maknib View Post
    sounds to me like you have move everyone OUTSIDe of your Class and not between the Main and class ending }

    um yeah, lol - i did didn't i? so i moved my methods between the main and class and now my program works...weell, the method part works atleast. thank guys/gals!:)

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Replies: 2
    Last Post: 03-26-2010, 06:12 PM
  3. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 08:20 PM
  4. Replies: 29
    Last Post: 09-25-2008, 08:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 09:37 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
  •