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

Thread: Not sure what is wrong

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Not sure what is wrong

    Ok, I am trying to set this program up using a for loop. I do not know if I am just to tired to find what is wrong, or if I am way off base, but my program is not compiling. Here is my code.

    public static void main (String [] args)
    {

    int i;
    int number = 0, sum = 0, numamount=0;

    Scanner input = new Scanner(System.in);



    {
    System.out.println("Enter a number that is positive or zero(negative to terminate): ");
    i = input.nextInt();
    for(i>=0){
    numamount++;
    sum += i;
    double average = sum/numamount;

    }
    }
    {


    System.out.println("You have entered " + numamount + " numbers");
    System.out.println("Total of the numbers you entered is " + sum);
    System.out.println("Average of the numbers is " + average);
    }
    }
    }

    I am trying to prompt the user to enter any number 0 or greater. Once a negative number is entered, it is supposed to give the number of integers given, as well as the sum and average of the given integers.


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: Not sure what is wrong

    just from quickly looking though the code.. the for loop should be

    for(i = 0; <= 5; i++)

    First section is declaring the integer and setting the start value of it as 0
    Second section is saying execute this loop while i is less than or equal to 5
    Third section is the increment, States i++ each time the loop is executed.

    Also inside the actual for loop you should not have i++ or it will be adding + 2 to i per loop as the increment section automaticly does it each loop.

  3. #3
    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: Not sure what is wrong

    my program is not compiling.
    When you get errors, please copy and paste the full text here. If we had the error message we could tell you what is wrong.

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what is wrong

    This is the error I get when I compile.

    cannot find symbol
    symbol : variable average
    location: class Assign7_1_Key
    System.out.println("Average of the numbers is " + average);


    Ite seems to indicate that average is not valid variable.

  5. #5
    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: Not sure what is wrong

    Is the variable average in scope where you are trying to use it?
    The scope of a variable's definition is within the enclosing pair of {}s. If you define a variable inside of a pair of {}s it is not known outside of those {}s

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what is wrong

    OK, seems it seems I did have it closed into a pair of {}, I removed them and it comiles successfully now, however it is not running properly.

    This is the results I should get..

    *Enter a number that is positive or zero(negative to terminate):
    2
    Enter a number that is positive or zero(negative to terminate):
    3
    Enter a number that is positive or zero(negative to terminate):
    3
    Enter a number that is positive or zero(negative to terminate):
    4
    Enter a number that is positive or zero(negative to terminate):
    -1
    You have entered 4 numbers
    Total of the numbers you entered is 12
    Average of the numbers is 3.0
    Press any key to continue . .


    This is what I am getting...

    Enter a number that is positive or zero(negative to terminate):
    2
    You have entered 1 numbers
    Total of the numbers you entered is 2
    Average of the numbers is 2.0
    Press any key to continue . . .

    It should keep letting me enter numbers until and unless I enter a negative number, then it gives me sum, average, etc.

  7. #7
    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: Not sure what is wrong

    Time to play computer and manually step through your code, do what the computer would do for each statement recording the results on a piece of paper.

    Can you post your current code. The original code had several problems that you have fixed. We need to see the current code to help.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what is wrong

    That is what I have been doing, but I think the sinus headache I awoke with this morning is taking over my thought process...lol.

    Here is my current code:

    public static void main (String [] args)
    {

    int i;
    int number = 0, sum = 0, numamount=0;

    Scanner input = new Scanner(System.in);




    System.out.println("Enter a number that is positive or zero(negative to terminate): ");
    i = input.nextInt();
    if(i>=0){
    numamount++;
    sum += i;
    double average = sum/numamount;





    {
    System.out.println("You have entered " + numamount + " numbers");
    System.out.println("Total of the numbers you entered is " + sum);
    System.out.println("Average of the numbers is " + average);
    }
    }


    }
    }

  9. #9
    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: Not sure what is wrong

    What you posted will not compile. It is missing the import statements and the class definition.

    Also when you post code put the code inside of code tags.
    See: http://www.java-forums.org/misc.php?do=bbcode#code

    Did you do what I recommended earlier?

    Time to play computer and manually step through your code, do what the computer would do for each statement recording the results on a piece of paper.
    Last edited by Norm; June 18th, 2011 at 10:33 AM.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure what is wrong

    Sorry, I did not realize I had not copied the import and class info....

     import java.util.Scanner;
     
      public class Assign7_1_Key {
     
     
         public static void main (String [] args)
         {
     
          int i;
          int number = 0, sum = 0, numamount=0;
     
          Scanner input = new Scanner(System.in);
     
     
     
     
              System.out.println("Enter a number that is positive or zero(negative to terminate): ");
              i = input.nextInt();
              if(i>=0){
                 numamount++;
                 sum += i;
                 double average = sum/numamount;
     
     
     
     
     
             {
        System.out.println("You have entered " + numamount + " numbers");
          System.out.println("Total of the numbers you entered is " + sum);
    		 System.out.println("Average of the numbers is " + average);
         }
     }
     
     
    }
    }

    I am trying to go through it, but think I may have to wait until this headache eases off to be able to find the issue. I will try to take a fresh look at it later and maybe I can find it, and I do appreciate your help!

  11. #11
    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: Not sure what is wrong

    Ok. See you later.
    When you do look at the code, look for where you need a loop to ask the user for the next input and to test that input to determine whether to continue the loop to get more data or to exit the loop and do the computations.

Similar Threads

  1. Not sure what is wrong with this
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 02:23 PM
  2. Something is wrong? Please help.
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 29th, 2010, 07:47 AM
  3. What's wrong?!
    By deeerek in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 22nd, 2010, 07:11 PM
  4. don't know what's wrong
    By james in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 15th, 2010, 07:37 PM
  5. Where am I going wrong? :)
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 18th, 2009, 12:03 AM

Tags for this Thread