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: another school homework thread

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default another school homework thread

    I just started learn this stuff and im not doing to well so far. So any help is welcome.

    Design and implement an application that reads an arbitrary number of integers that are in the range 0 to 50 inclusive and counts how many occurrences of each are entered. Stop reading input when any integer outside the range 0 to 50 is encountered. After all input has been processed, print all of the values (with the number of occurrences) that were entered one or more times.
    Prompt for input with System.out .print("enter numbers: ").
    The output should be one frequency count per line. For example:
    enter numbers: 2 2 5 5 5 5 5 5 2 5 -1
    3 occurrences of 2
    7 occurrences of 5
    SPECIFICATION OF NAMES: Please name your application class Frequency

    What i have so far is

    //Samuel Tate
    //Itec 120
    //Frequnency
    // Will not complile

    import java.util.Scanner;
     
      public class Frequency {
     
        public static void main (String[] args) { 
     
     
        int input = 0;
        int[] integers = new int[51];
        Scanner scan = new Scanner(System.in);
     
     
       while(input != 51); {
     
        System.out.print("Enter an arbitrary number of integers in the range 0-50 (enter 51 to exit) : ");
        input = scan.nextInt();
            if(input == 51) break;
            if(input < 0 || input > 50){
                System.out.println("number out of range");
                }
         integers[input]++; 
            continue;
     
             }   
     
             for(int index = 0; index < 51; index++);
                if(integers[index] > 0)
                    System.out.println(index + ": " + integers[index]);
     
     
         }   
     
     
    }


  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: another school homework thread

    Do you have any specific questions about your code?

    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
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: another school homework thread

    You are using a rough format to break down and understand, might I suggest using brackets "{" & "}" to body your for loops, if-then statements, and while loops.

    As of right now your while loop does nothing because you end the loop with a semi-colon directly after you initialize it.

    Yours:
    while(condition); {
    // body
    }

    Correct:
    while(condition) {
    // body
    }

    Continue is not a keyword to be used inside an if-then statement, neither is break. Remove those

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: another school homework thread

    Why do you have a ; after the while clause? That will just do nothing (; is a valid statement in java though it does nothing in this case.)

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: another school homework thread

    Really need some help on school

    Duplicate post
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 1
    Last Post: September 24th, 2013, 04:18 PM
  2. School Help
    By scot_stuf in forum Java Theory & Questions
    Replies: 4
    Last Post: September 22nd, 2012, 04:31 PM
  3. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  4. School Help
    By JavaNewb in forum Loops & Control Statements
    Replies: 1
    Last Post: March 24th, 2010, 09:31 PM
  5. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM