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

Thread: Help with output

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question Help with output

    I have a question like this:

    Say that a "clump" in an array is a series of 2 or more adjacent elements of the same value. Return the number of clumps in the given array.

    e.g
    countClumps({1, 2, 2, 3, 4, 4}) ? 2
    countClumps{(1, 1, 2, 1, 2}) ? 2
    countClumps({1, 1, 1, 1, 1}) ? 1
    Here is my code:

    import java.util.*;
    import java.io.*;
    import java.text.*;
    import java.lang.*;
     
     
    public class Program2 {
     
     
        int[] clumps = new int[100];
     
        public static void main(String[] args) {
     
            int index;
            Program2 program = new Program2();
     
            Scanner keyboard;
            keyboard = new Scanner(System.in);
     
            System.out.println("Please enter an array of numbers seperated by space:");
     
            for(index = 0;index <= program.clumps.length;index++)
                program.clumps[index] = keyboard.nextInt();
     
            System.out.print("countClumps({");
            for(index = 0;index <= program.clumps.length;index++)
                System.out.print(program.clumps[index] + ", ");
            System.out.print("}) ? " + program.countClumps(program.clumps));
        }
     
        public int countClumps(int[] nums) {
            int count = 0;
     
            for (int i = 0; i <= clumps.length; i++) {
                for (int j = 1; j <= clumps.length; j++) {
                    if (clumps[i] == clumps[j])
                        count =+ 1;
                }
            }
            return count;
        }
     
    }

    The program compiles fine, but after I enter any array and press enter key doesn't seems to have anything going... Hope someone can clarify what I did wrong here...


  2. #2
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Help with output

    can you say in detail how you get the count?
    countClumps({1, 2, 2, 3, 4, 4}) ? 2 <--- does 2 means  2,2 and 4,4 ?
    countClumps{(1, 1, 2, 1, 2}) ? 2  <--- why is it 2? there is only 1 pair of elements that are adjacent, ie 1,1
    countClumps({1, 1, 1, 1, 1}) ? 1  <-- why is it 1?

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with output

    Hi there.

    Sorry about the sample output... The second one should be this:

    countClumps{(1, 1, 2, 1, 1}) ? 2

    For the last one, as we have two or more 1 that is the same as the 1 at position 0, so we count all of them as 1 clump

    The 2 means, there are 2 pairs of clumps.. there is a 2 adjacent to the 2 at position 1. Similar to number 4, there is another adjacent 4 at position 5. Hence 2 clumps
    Same explanation to sample 2

  4. #4
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Help with output

    you can ask the user to enter the numbers by pressing the enter key.
    public class Program2 {
     
        int[] clumps = new int[5];  // change to 5 for testing
        public static void main(String[] args) {
            int index;
            Program2 program = new Program2();
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.println("Please enter an array of numbers, press enter for new number:");
     
            // change for loop condition to index < program.clumps.length
            for(index = 0;index < program.clumps.length;index++)
                program.clumps[index] = keyboard.nextInt();
     
            System.out.print("countClumps({");
            for(index = 0;index < program.clumps.length;index++)
                System.out.print(program.clumps[index] + ", ");
            System.out.print("}) ? " + program.countClumps(program.clumps));
        }
     
        public int countClumps(int[] nums) {
            int count = 0;
     
            for (int i = 0; i < clumps.length; i++) {
                for (int j = 1; j < clumps.length; j++) {
                    if (clumps[i] == clumps[j])
                        count =+ 1;
                }
            }
            return count;
        }
     
    }

  5. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with output

    Thanks.

    However, when I tried your code. The result for the first example does not return 2 but 1... Also, I assume the question are referring to user inputting any arbitrary number of elements in the array... Hence I am wondering if there is a way to automatically determine the size of the array to match the amount of elements users input?

  6. #6
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: Help with output

    sorry you are mistaken. Its not my code. Its your code . I did not do anything drastic to it
    you can set the integer array size after you ask the user "how many numbers you want to enter? ". this is just a suggestion. I suppose also, that you are not allow to use Collections like ArrayList, where you can expand your array dynamically. If you have no restriction, then you might want to try using Arrays....

    As for the logic of counting adjacent items, maybe you can declare another array to store the numbers
          for (int i = 0; i < clumps.length; i++) {
                for (int j = 1; j < clumps.length; j++) {
                    if (clumps[i] == clumps[j])
                        count =+ 1;
                        anotherArray [ clumps[i] ] = count;  // just an idea...
                }
            }
            return count;

  7. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Help with output

    Thanks for the tips.

    Will post back if I encounter further problem...

Similar Threads

  1. what's the output?
    By dcshoecousa in forum Java Theory & Questions
    Replies: 3
    Last Post: November 27th, 2010, 10:11 AM
  2. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  3. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  4. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM
  5. [SOLVED] Java program error in displaying Output
    By crazydeo in forum AWT / Java Swing
    Replies: 9
    Last Post: May 14th, 2008, 10:42 AM