Results 1 to 3 of 3
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question Array count number Occurances

    I am working on some code which is supposed to count the number of occurences of each digit between 1 and 50. printing the results as follows:

    Number 1: 2
    Number 2: 0
    Number 3: 1
    ....

    It should do this by storing the digit in an array to tally the number of occurances of each digit.

    I have tried to do this by looping over the string input by the user then looping over the array to add each character to its correct place.

    i have not managed to achieve this does anyone know how i have gone wrong

    Java Code:
    package chapter7projects;
    
    import java.util.Scanner;
    
    public class PP71
    {
       public static void main (String[] args)
       {
          final int MAX = 50;
          final int MIN = 0;
          int count = 0;
    
          Scanner scan = new Scanner (System.in);
          int[] numArray = new int[MAX];
          int current;   // the current character being processed
    
          System.out.println ("Enter a number:");
            String line = scan.nextLine();
    
          for(int i = 0; i <= line.length(); i++)   //loop round input string
          {
              for(int n = 0; n <= MAX; n++)         //loop round array tallying num occurances
              {
                  if(numArray[i] == n)
                  {
                      numArray[i] = count+1;
                  }
              }
          }
            for (int j = 0; j < numArray.length; j++) {
                System.out.println("Nunber " + (j + 1) + ": " + numArray[j]);
    
           }
    
       }
    }
    -Long time no c-
    -:eek:-

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    this is one of the times where i'd like to ask where YOU think it's going wrong. what have you done to debug it? you say you "have not managed to achieve this", but you don't mention any insight into what you think is erroneous. your best bets right now would be to rethink the following pieces of code:

    Java Code:
            String line = scan.nextLine();
    
          for(int i = 0; i <= line.length(); i++)   //loop round input string
    Java Code:
              for(int n = 0; n <= MAX; n++)         //loop round array tallying num occurances
              {
                  if(numArray[i] == n)
                  {
                      numArray[i] = count+1;
                  }
              }

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Hint, hint...

    A couple of hints...
    Java Code:
    for(int n = 0; n <= MAX; n++)         //loop round array tallying num occurances
              {
                  if(numArray[i] == n)
                  {
                      numArray[i] = count+1;
                  }
              }
    You are comparing n against what's in the array, but.... when did any data get put into the array?
    Java Code:
    String line = scan.nextLine();
    In the above code, you have the user's input in a sting (line)... when is this string used on further comparations?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 07:49 AM
  2. Replies: 4
    Last Post: 03-28-2009, 05:58 PM
  3. Number Array to Sound
    By Phantasmagorical in forum Advanced Java
    Replies: 8
    Last Post: 03-01-2009, 06:36 PM
  4. Replies: 8
    Last Post: 02-04-2009, 09:55 PM
  5. [SOLVED] Array of first negative number
    By random0munky in forum New To Java
    Replies: 9
    Last Post: 12-09-2008, 12:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •