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 8 of 8

Thread: Need help STUCK

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Need help STUCK

    I need to ask if the user wants to add another product receive a yes or no and run the loop again? Also I need to count the products and add the prices together to figure an average?
        private double computeAverageSales() {
            WebHost webHost;
            double average = 0;
            int counter = 0;
            double total = 0;
            boolean again = true;
            double webHostPrice = 0;
     
            System.out.println("Enter 0 to end");
     
     
     
     
                System.out.println("Please enter the price of the new package: $");
                webHostPrice = scanner.nextDouble();
     
                while (true){
                for (int i = 0; i <= 0; i++) {
                    if (webHostPrice == 0) {
                        break;
                    } else if (webHostPrice <= -1) {
                        System.out.println("Please enter a positive number!");
                    } else if (webHostPrice >= 500) {
                        System.out.println("Are you sure that seems a little high!");
                    } else if (webHostPrice == 0){
                    break;
                }   
     
                    System.out.println("Please enter the product ID ");
                    int webHostId = scanner.nextInt();
                    if (webHostId <= -1) {
                        System.out.println("Please enter a positive number!");
     
                    } else if (webHostId > 10000) {
                        System.out.println("Sorry please enter a valid ID number");
     
                    } else if (webHostId == 0) {
                        break;
                    } 
     
                    System.out.println("What is the product name: ");
                    String webHostName = scanner.next();
     
                    if (webHostName == null) {
                        System.out.println("Please enter a valid Product Name!");
     
                    }
                     if (webHostName.equals("end")) {
                        break;
                    }
     
     
     
                    System.out.println("Please enter the required Bandwidth needed for your site? ");
                    double webHostBandwidth = scanner.nextDouble();
                    if (webHostBandwidth <= -1) {
                        System.out.println("Please enter a positive number!");
                        webHostBandwidth = scanner.nextDouble();
                        ///LOOK  Here's another INFINITE LOOP... This while will never quit
                    }
                    else if (webHostBandwidth > 10000000) {
                        System.out.println("Please enter a vaild number!");
     
                    }
                    else if (webHostBandwidth == 0) {
                        break;
                    }
     
     
                    System.out.println("How many FTP accounts do you need? ");
                    int webHostFtp = scanner.nextInt();
                    if (webHostFtp <= -1) {
                        System.out.println("Please enter a positive number!");
     
                        ///LOOK  And, another INFINITE LOOP !!
                    }
                    else if (webHostFtp > 120) {
                        System.out.println("Please enter a valid number!");
     
                    }
                    else if (webHostFtp == 0) {
                        break;
                    }
     
     
                    System.out.print("How many email accounts do you need? ");
                    int webHostMail = scanner.nextInt();
                    if (webHostMail <= -1) {
                        System.out.println("Please enter a positive number!");
     
                        ///LOOK  And still another INFINITE LOOP !!
                    }
                    else if (webHostMail > 225) {
                        System.out.println("Please enter a valid number!");
     
                    }
                    else if (webHostMail == 0) {
                        break;
                    }
     
                    System.out.print("What is your domain name? ");
                    String webHostDomain = scanner.next();
                    webHostDomain = webHostDomain.trim();
                    if (webHostDomain.length() < 0) {
                        System.out.println("Please enter a valid domain name!");
     
                        webHostDomain = webHostDomain.trim();
                    }
                    else if (webHostDomain.equals("0")) {
                        break;
                    }
     
                    System.out.println("Would you like to add another product? ");
     
     
     
                } // end for statement
                average = total / (counter - 1);
                return average;
                } // end while statement
        }

    I know the code is messing but that is because I have worked 4 days on this I am a newbie any suggestions is much appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help STUCK

    Can you explain what problems you are having with the code? What did you try and what happened?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help STUCK

    at the end of the loop at Justins no option to repeat in account all the inputs for the loop not just the price part of the loop

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help STUCK

    Is that a question?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help STUCK

    sorry autocorrect will respond in 30 minutes from my work

    --- Update ---

    Welcome to the Web Host Ordering System

    Enter 0 to end
    Please enter the price of the new package: $
    150
    Please enter the product ID
    312
    What is the product name:
    sh1
    Please enter the required Bandwidth needed for your site?
    1024
    How many FTP accounts do you need?
    1
    How many email accounts do you need? 1
    What is your domain name? mytms.co
    Would you like to add another product?

    The average sales is: $-0.0

    BUILD SUCCESSFUL (total time: 22 seconds)

    This is what I get after I run it no option to run again and the average should have been $150 instead -0.0

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help STUCK

    Where is the average calculated? Why is the value that is printed 0.0?
    Try debugging the code by adding some println() statements to the code that prints outs the values of the variables use to calculate the average. The print outs will help you find where the program is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    31
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Need help STUCK

    I am sorry could you please give me an example; I do not understand like I said I am a newbie!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help STUCK

    Look at the code and find where the average variable is given a value. Add a call to the println() method to print out the values of all the variables that are used to compute average. A sample println():
    System.out.println("theVariableName "+ theVariableNameHere);
    when the program is executed the print out will show you what the contents of the variables were when the println was executed. That will help you understand why average is 0
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help me please, im stuck!
    By warbie118 in forum Object Oriented Programming
    Replies: 1
    Last Post: November 15th, 2011, 09:09 AM
  2. Please Help I'm stuck
    By mael331 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 6th, 2011, 09:02 AM
  3. Help! im stuck!
    By aznguy92 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2011, 09:16 PM
  4. Im stuck, please help
    By bigsmoke101 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 12th, 2011, 04:34 PM
  5. PLEASE PLEASE I AM STUCK...
    By ThejavaBUM in forum Loops & Control Statements
    Replies: 2
    Last Post: April 5th, 2011, 10:16 PM