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.

Page 2 of 2 FirstFirst 12
Results 26 to 44 of 44

Thread: Count out program-- How do i do this?

  1. #26
    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: Count out program-- How do i do this?

    it is not giving an error at a particular statement.
    Yes it does. The line number is given in the stack trace:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 60
    at countout.Remain(countout.java:20)
    The error is on line 20.

    how exactly do i fix the loop if that is the error?
    Paste all the lines that define and set values for the code used on line 20.
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Yes it does. The line number is given in the stack trace:

    The error is on line 20.


    Paste all the lines that define and set values for the code used on line 20.
    this is lines 19 and 20

    for(count=0;count <= n;++ count){
    	kids[count]=1;

  3. #28
    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: Count out program-- How do i do this?

    Did you read posts #18 and 24 about how to code the termination condition in a for loop?

    BTW The code should use the array's .length field to test the end of the array, not the variable:n
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Did you read posts #18 and 24 about how to code the termination condition in a for loop?

    BTW The code should use the array's .length field to test the end of the array, not the variable:n
    ok i made some changes and im not getting any errors.However when i enter data nothing happens after that. It just accepts the data and the cursor blinks on the screen

  5. #30
    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: Count out program-- How do i do this?

    Is the code in an infinite loop? To see, add some println() statements inside of the loops. Have them print out the values of the variables that are controlling the looping. The values printed will help you understand what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #31
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Is the code in an infinite loop? To see, add some println() statements inside of the loops. Have them print out the values of the variables that are controlling the looping. The values printed will help you understand what the code is doing.
    nothing is working
    its due at 6 this evening.

    can i use a for loop inside another for loop?
    and how do i eliminate a child if im using an array of integers?

  7. #32
    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: Count out program-- How do i do this?

    how do i eliminate a child if im using an array of integers?
    Use one int value in a slot to show the child is present and have another value to show that the child has been removed. For example: 0 means child is present and 1 means the child has been removed. To eliminate/remove a child, change the slot's contents from 0 to 1.

    can i use a for loop inside another for loop?
    Yes you can nest as many for loops as needed.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #33
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Use one int value in a slot to show the child is present and have another value to show that the child has been removed. For example: 0 means child is present and 1 means the child has been removed. To eliminate/remove a child, change the slot's contents from 0 to 1.


    Yes you can nest as many for loops as needed.
    like this?
    public static void Remain(int n,int m){
     
    int [] kids = new int[n];
     
    while(kids[n] !=1){
    	for(int j=1;j<=kids.length;j++){
    	 for(int i=1;i <=m;i++){
    		kids[m] = 0;
    		}//end for
    		j=m+1;
    	}//end for
    }
    System.out.printf("%d",kids[m]);
    }//end method

  9. #34
    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: Count out program-- How do i do this?

    Does it compile? If it does then the code is legal.
    There are no comments with the code describing what its logic is supposed to be so it's hard to see if the code is doing what the programmer wants it to do.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #35
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Does it compile? If it does then the code is legal.
    There are no comments with the code describing what its logic is supposed to be so it's hard to see if the code is doing what the programmer wants it to do.
    it compiles but after i enter the values for the # of children and the # of words it gives out of bounds.

    public static void Remain(int n,int m){
     
    int [] kids = new int[n];//array int holds total kids
     
    while(kids[n] !=1){//while total kids in array is not equal to 1
    	for(int j=1;j<=kids.length;j++){//j goes from 1 to maximum kids in array
    	 for(int i=1;i <=m;i++){//i goes from 1 to m i.e the amount of words in a sentence
    		kids[m] = 0;// mth child eliminated
    		}//end for
    		j=m+1;//starting with child after the 1 just eliminated
    	}//end for
    }
    System.out.printf("%d",kids[m]);//display the child left
    }//end method

  11. #36
    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: Count out program-- How do i do this?

    gives out of bounds.
    Where is the stack trace?
    That has happened several times in this thread. You should know by now how to fix it.

    BTW Most of your comments only say what the code is obviously doing. They should say WHY the code is doing it.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #37
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Where is the stack trace?
    That has happened several times in this thread. You should know by now how to fix it.

    BTW Most of your comments only say what the code is obviously doing. They should say WHY the code is doing it.
    whats is stack trace?

    the code is doing it because the question asked to do it. I dont know what else to do. Ill have to submit it as it is.

  13. #38
    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: Count out program-- How do i do this?

    whats is stack trace?
    You posted the stack trace in posts#17, 19, 21 and 25

    It starts with:
    Exception in thread ...
    and is followed by several lines that begin with
    at ...
    at ...
    If you don't understand my answer, don't ignore it, ask a question.

  14. #39
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    You posted the stack trace in posts#17, 19, 21 and 25

    It starts with:
    Exception in thread ...
    and is followed by several lines that begin with
    at ...
    at ...
    is this close? im getting an infinite loop then my program crashed.
    System.out.printf("Enter a number:");
    int n = in.nextInt();
    System.out.printf("Enter a value for m:");
    int m = in.nextInt();
     
    int [] nums = new int [n];
    while(n !=1){
    	int i=1;
    for(i=1;i <=n-1;i++) nums[i-1]=i;
    for(i=0;i <=m;i++){
    	m--;
     
    }System.out.printf("%d",nums[i%n]);
    	}
    }
    }

  15. #40
    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: Count out program-- How do i do this?

    is this close?
    No. It looks like a bunch of random code without any meaning.
    Post#4 was much closer, but I can not believe you wrote it.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #41
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    No. It looks like a bunch of random code without any meaning.
    Post#4 was much closer, but I can not believe you wrote it.
    lol.What do i have to do to make post #4 work?

  17. #42
    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: Count out program-- How do i do this?

    Here are a couple of suggestions to start with:
    0) fix the formatting so the code is readable
    1)Change the variable names from n and m to numKids and numWords to make the code readable
    2)Debug it by adding some println statements that print out the values of variables used in the loops.

    The Arrays class's toString() method is useful for debugging arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  18. #43
    Member
    Join Date
    Jun 2014
    Posts
    65
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Count out program-- How do i do this?

    Quote Originally Posted by Norm View Post
    Here are a couple of suggestions to start with:
    0) fix the formatting so the code is readable
    1)Change the variable names from n and m to numKids and numWords to make the code readable
    2)Debug it by adding some println statements that print out the values of variables used in the loops.

    The Arrays class's toString() method is useful for debugging arrays:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    had to submit it already. We can use those Array.toString yet since the lecturer did not teach it and does not want us implementing things he did not teach.

  19. #44
    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: Count out program-- How do i do this?

    The toString() method was for debugging. It would be taken out when the debugging was done.

    Are you done with this project now? Not interested in getting it to work now that its been turned in?
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Java program to count External devices connected with the system. Eg. Pen drive
    By arindam.mukherjee007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2013, 08:34 AM
  2. The count for each value in array
    By samar in forum Loops & Control Statements
    Replies: 4
    Last Post: January 4th, 2013, 02:38 PM
  3. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 01:31 AM
  4. Sentence and Letter Count Program
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2010, 12:10 AM
  5. select count(*)
    By jacinto in forum JDBC & Databases
    Replies: 4
    Last Post: March 2nd, 2010, 10:30 PM