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

Thread: Homework help what am I doing wrong?

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework help what am I doing wrong?

    Ok, in my Java programming class we have to write a program that reads some integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Here is a sample run of the program:

    Enter some integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
    2 occurs 2 times
    3 occurs 1 time
    4 occurs 1 time
    5 occurs 2 times
    23 occurs 1 time
    43 occurs 1 time

    Note that if a number occurs more than one time, the plural word "times" is used in the output. Also,
    note that the integers input will be between 1 and 100 but there may be more than 100 numbers input.
    Finally, note that for integers input 0 times, nothing is printed.

    Ok so here's what I have so far, but the program won't run! If you could be as thorough as possible, I'm a beginner and terrible at programming! Thanks so much!

    import java.util.Scanner;
     
    class NumberOccurance{
     
     public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
     
                   System.out.print("Enter the integers between 1 and 100: ");
     
            int i, count[] = new int[101];
     
            while (input.hasNextInt())
                count[input.nextInt()]++;
     
            for (i=1; i < 101; i++)
                if (count[i] != 0)
                    System.out.println(i+" occurs "+count[i]+" time"+(count[i]>1?"s":""));
        }
    }

    When I debug this in netbeans it allows me to enter the numbers but I get no response what is wrong? What am I missing?


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

    Default Re: Homework help what am I doing wrong?

    I get no response
    Is the program waiting for your input?
    To see where the code is executing, add some println() statements that print out messages to show you where the code is being executed.

    One potential problem I see it that there aren't any {}s surrounding the code in the loops or following the if statement. {}s should always be used to make code more readable and easier to understand.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Yes I enter the following numbers to test it 2 5 6 5 4 3 23 43 2 0 and the cursor just moves to the next line and that is it.

    --- Update ---

    Ok unfortunately I have a teacher that doesn't explain things too well for us can you give an example of where the {} should be. Because I see this alot in netbeans and was just told not to worry about it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Here's a site with many coding conventions defined:
    Code Conventions for the Java Programming Language: Contents
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Quote Originally Posted by Norm View Post

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    Not sure I understand what you mean here please explain.

    --- Update ---

    Thanks for your help that site will help me learn some more. My teacher is no help.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Put the following tag before the code:
    [code=java]


    <YOUR CODE GOES HERE>


    Put the following tag after the code:
    [/code]
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Ok got it thanks Norm I will do so. Did i do it right?

    --- Update ---

    This what i get when I enter the numbers

    debug:
    Enter the integers between 1 and 100: 2 5 6 5 4 3 23 43 2 0
    <<<<(The cursor ends up here)

    --- Update ---

    Norm you still there?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Did i do it right?
    The code tags are good. Now it needs the { and }

    If you keep entering numbers, The program will continue reading them forever. Try a few hundred to see.

    This is a hint of what the code should do:
    Assume the input ends with 0.
    I assume that means that the code should look for a 0 value and exit the loop where it is reading input.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Quote Originally Posted by Norm View Post
    The code tags are good. Now it needs the { and }
    Where do I put the { AND }? Not sure like I said I am really new at this.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Here's a site with many coding conventions defined:
    Code Conventions for the Java Programming Language: Contents

    Look at some other postings on this site. Most have {}s
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    But from what you see here am I headed in the right direction? The code is correct right?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    The code is correct
    If it executes and generates the right results, then it could be correct.
    Did you fix the code for the problem I mentioned in post#8?

    Did you try typing in 100 numbers as the program reads input? I think the program will go forever.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Hey Norm I am going to sign off for now I am watching the nba on espn so I can't quite concentrate be back in a few hours thanks for the help I'll be back later. My wife is headed off to school too so I need to watch our two year old. I'll be back when I can concentrate better. sorry

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Back tomorrow then.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    probably later tonight or tomorrow yes. Thank for your help again friend.

  16. #16
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Hey Norm you around? I typed several more numbers in to that code and still got nothing.

    --- Update ---

    Well I'll check back later today is my 7th wedding aniversary! yeah I lasted 7 years so far! Still in love too!

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    I typed several more numbers in to that code and still got nothing.
    I suspect the program will continue to ask for numbers long after you get tired of entering them.

    You need to have code that makes the loop stop looping. See post#8
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Norm is there a simpler way of writing this without the hasNextInt involved? evedently that is to advanced for my class and he wants it simpler can you show me how you would do this and explain it to me so I may learn? Remember please keep it simple I really do want to learn this but I have a teacher who will not help and a dead line to turn it it in too. The dead line is May 1st to turn it in can you help?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    without the hasNextInt
    Ask the user how many numbers he is going to enter, and use that value to control how many times the loops goes around. That will require the user to do an accurate count before entering the data
    vs entering the data and ending it with a special value.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Quote Originally Posted by Norm View Post
    Ask the user how many numbers he is going to enter, and use that value to control how many times the loops goes around. That will require the user to do an accurate count before entering the data
    vs entering the data and ending it with a special value.
    Is this what you are talking about? Something like this?

    import java.util.Scanner;
     
    class NumberOccurance{
     
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
     
    System.out.print("Enter the integers between 1 and 100: ");
     
    for (int i = 0; i <= 101; i++) {
     
    int[] count = new int[101];
    count[i] = input.nextInt();
    count[i]++;
     
    while (i != 0) {
     
    if (count[i] > 0) {
    if (count[i] > 1) (
    System.out.println(i + " occurs " + count[i] + " times");
    }
    else
    System.out.println(i + " occurs 1 time");
    }
    }
    }
    }
    }


    --- Update ---

    sees to be off still. but it is simpler right?

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    The code is not properly formatted. Nested statements need to be indented 3-4 spaces.
    The statements should NOT all start in the first column.

    What happens when you compile and execute the code?

    If you are going to get a number from the user to control the looping, you need to print a message asking the user how any numbers he is going to enter, then read his answer and use that number to control the number of times that the for loop goes around.

    Also there is a if statement that does not have {}s surrounding the code that follows the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    Quote Originally Posted by Norm View Post

    What happens when you compile and execute the code?
    When I enter the following test numbers : 2 5 6 5 4 3 23 43 2 0 I get the following repeated over and over.
    1 occurs 6 times

    --- Update ---

    Also according to the assignment we have to write a program that reads some integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Homework help what am I doing wrong?

    Its hard' to read and understand unformatted code.

    This statement is a clue for how to write the input reading loop:
    Assume the input ends with 0.

    The code should test the input's value for 0 and stop reading input when a 0 is read.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Junior Member
    Join Date
    Apr 2013
    Location
    El Centro, CA
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework help what am I doing wrong?

    I got It! Like this right?

    import java.util.Scanner;
     
    class NumberOccurance{
     
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    int[] counts = new int[100];
     
    System.out.print("Enter the integers between 1 and 100 and 0 to exit the program: ");
     
     
    int number = input.nextInt();
    while (number != 0) {
    counts[(number - 1)] += 1;
    number = input.nextInt();
    }
     
    for (int i = 1; i < 100; i++)
    if (counts[i] > 0)
    System.out.println(i + 1 + " occurs " + counts[i] + (
    counts[i] == 1 ? " time" : " times"));
    }
    }


    --- Update ---

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

    Default Re: Homework help what am I doing wrong?

    Not yet.
    Nested statements need to be indented 3-4 spaces.
    The statements should NOT all start in the first column.

    There are if statements and for statements that are missing {} for the code they control. The while() statement does have {}s
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help With my Homework Please :)
    By surfelijo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 06:32 PM
  2. Help with homework
    By gta1 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 11th, 2013, 08:58 PM
  3. Homework help?
    By regi.dg in forum Object Oriented Programming
    Replies: 0
    Last Post: October 16th, 2012, 08:17 PM
  4. I am doing my homework =D
    By valenciajv in forum Java Theory & Questions
    Replies: 9
    Last Post: October 7th, 2011, 04:39 PM
  5. Homework help
    By cd247 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2009, 05:56 PM