Results 1 to 2 of 2
  1. #1
    anusha is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Question how to print occurence of values in the array from 1 to9?

    public class ArrayExample {
    public static void main(String[] args) {
    Integer[] array = {1,78,23,5,6,7,8,6,5,8,8,50};

    ArrayList l=new ArrayList();
    ArrayList<Integer> s=new ArrayList<Integer>();
    for(int i=0;i<array.length;i++)
    { int a=array[i];
    if(a>9)
    {
    l.add(a);

    }
    else
    { s.add(a);
    }

    }

    countStringOccurences(s);
    System.out.println("this are 2digit number:"+l);
    }


    private static void countStringOccurences(ArrayList<Integer> strArray) {
    HashMap<Integer, Integer> countMap = new HashMap<Integer, Integer>();

    for (Integer string : strArray) {


    if (!countMap.containsKey(string)) {
    countMap.put(string, 1);
    } else {
    Integer count = countMap.get(string);
    count = count + 1;
    countMap.put(string, count);
    }
    }
    printCount(countMap);
    }

    private static void printCount(HashMap<Integer, Integer> countMap) {
    Set<Integer> keySet = countMap.keySet();
    for (Integer string : keySet) {
    System.out.println(string + " : " + countMap.get(string));
    }
    }
    }

    o/p is:
    1 : 1
    5 : 2
    6 : 2
    7 : 1
    8 : 3
    this are 2digit number:[78, 23, 50]


    but i have to show like
    0:1
    1:1
    2:0
    3:0
    ...
    ...
    9:0
    this are 2digit number:[78, 23, 50]

    Please help what i have to change pls

  2. #2
    anusha is offline Member
    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Default Re: how to print occurence of values in the array from 1 to9?

    Please help me

Similar Threads

  1. Print integer Array values into a single !, showMessagedialog
    By Kadir Karapinar in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 03:57 PM
  2. adding values in JTable and print total help
    By danpotter in forum AWT / Swing
    Replies: 10
    Last Post: 06-10-2012, 01:18 AM
  3. Replies: 0
    Last Post: 04-16-2012, 09:35 AM
  4. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  5. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 09:48 PM

Posting Permissions

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