Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Can't declare Variable in new method

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can't declare Variable in new method

    In the very last method below
    import javax.swing.JOptionPane;
    public class program08 {
    public static void main(String[] args) {
        String getWeightString = JOptionPane.showInputDialog(null,
                "Enter the weight of the package");
        double getWeight = Double.parseDouble(getWeightString);
     
        String handling = JOptionPane.showInputDialog(null,
               "Would you like Special handling? Enter 'Y' for yes.");
        char handlingChar = handling.charAt(0);
     
        String insurance = JOptionPane.showInputDialog (null,
                "Would you like insurance? Enter 'Y' for yes.");
        char insuranceChar = insurance.charAt(0);
     
     
     
     
        JOptionPane.showMessageDialog(null, "The total cost is" + "");
        }
     
    public static double price(double getWeight) {
        double price;
        if (getWeight <= 2.00)
            price = 1.00;
        else if (getWeight > 2.00 && getWeight < 5.00)
            price = 2.00;
        else
            price = 4.00;
     
        return price;
        }
     
    public static double handlingPrice(char handlingChar) {
        double handlingPrice =0;
        if (handlingChar == 'Y' || handlingChar == 'y')
            handlingPrice = 5.00;
     
        return handlingPrice;
        }
     
    public static double insPrice(char insuranceChar) {
        double insPrice = 0;
        if (insuranceChar == 'Y' || insuranceChar == 'y')
            String insPriceString = JOptionPane.showInputDialog(null,
                    "How much insurance would you like?");
        insPrice = Double.parseDouble(insPriceString);
        insPrice += insPrice * 0.10;
        return insPrice;
        }
     
     
    }

    At the line that reads "String insPriceString = JOptionPane.showInputDialog"
    I get a warning that reads "Variable declaration not allowed here"
    The assignment requires that the variable for the amount of insurance the user wants, if they want insurance, to be declared in a different method, not the main method.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't declare Variable in new method

    You need to add {}s to enclose the statements controlled by the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't declare Variable in new method

    The if statement is not a loop. In only needs {} if it is a loop, no?

  4. #4
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Can't declare Variable in new method

    you have to add braces in your condition

    and concentrate with your return statement..

    Learn this: coding style - Single statement if block - braces or no? - Programmers
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can't declare Variable in new method

    You should always code {}s with if, else and loops
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Suggestion to declare variable that is declared
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 28th, 2013, 06:32 PM
  2. Help with returning variable from method
    By m7abraham in forum What's Wrong With My Code?
    Replies: 19
    Last Post: September 17th, 2012, 03:01 PM
  3. Is this a method call or variable declaration ???
    By hoboy in forum Java Theory & Questions
    Replies: 8
    Last Post: March 11th, 2012, 10:32 AM
  4. how to pass a variable from a method to another
    By amr in forum Java Theory & Questions
    Replies: 1
    Last Post: November 20th, 2010, 10:32 PM
  5. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM