Solved

Occurrences of eac digit in a string

Posted on 2007-11-27
6
2,120 Views
Last Modified: 2013-11-23
I having problem getting my program to solve. I am suppose to write a program that how many times a digit appear in a string and have to use the header  public static int[] count(String s).
public static void main(String[] args) {
		
	String s = JOptionPane.showInputDialog("Enter a string:");
	int[] counts  = countDigit(s.intern()); 
	
	String output = "";
	for(int i = 0; i < counts.length; i++)
	{
		if (counts[i] !=0)
			output += (char)('1' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time\n" : " times\n");
		}
	JOptionPane.showMessageDialog(null, output);
	}
	
	public static int[] countDigit(String s)
	{
	   int[] counts = new int[26];
	    for(int i = 0; i< s.length(); i++)
		{
		if (Character.isDigit(s.codePointCount(0, 26)))
		 counts[s.codePointAt(i)]++;
		}
		
		return counts;
	}

Open in new window

0
Comment
Question by:juicyfruit82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 43 total points
ID: 20361403
You can try using this:
 public static int[] countDigit(String s)
	{
	   int[] counts = new int[26];
	    for(int i = 0; i< s.length(); i++)
		{
		if (Character.isDigit(s.charAt(i)))
		 counts[s.charAt(i)-'0']++;
		}
		
		return counts;
	}

Open in new window

0
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 41 total points
ID: 20362052
>> output += (char)('1' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time\n" : " times\n");
               
output += (char)('0' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time\n" : " times\n");

and the method should be
public static int[] countDigit(String s)
	        {
	           int[] counts = new int[10];
	            for(int i = 0; i< s.length(); i++)
	                {
	                if (Character.isDigit(s.charAt(i)))	                
	                 counts[s.charAt(i)-48]++;
	                }
 
	                return counts;
        }

Open in new window

0
 
LVL 9

Assisted Solution

by:the_b1ackfox
the_b1ackfox earned 41 total points
ID: 20363805
import javax.swing.*;

public class tests {

      /**
       * @param args
       */
      public static void main(String[] args) {
            
            String s = JOptionPane.showInputDialog("Enter a string:");
            int[] counts  = countDigit(s);
            
            String output = "";
            for(int i = 0; i < counts.length; i++)
            {
                  if (counts[i] !=0)
                        output += (char)('1' + i) + " appears " + counts[i] + ((counts[i] == 1) ? " time\n" : " times\n");
                  }
            JOptionPane.showMessageDialog(null, output);
            }
            
            public static int[] countDigit(String s)
            {
                String Temp="";
                for(int ix = 0; ix< s.length(); ix++)
                  {
                      // OK ... lets filter out the crap we aren't interested in
                      if(Character.isDigit(s.charAt(ix))){
                            
                      Temp+=s.charAt(ix);      
                      }
                      //System.out.print(Temp);
                  }
                  //since there are only 10 digits to worry about
                //lets create an array to hold the data
                int[] tizzl=new int [10];
               
                for(int iy = 0; iy< Temp.length(); iy++)
                  {
                      //so now we just sort it all out...
                      int tmpnum=0;
                      tmpnum=Integer.parseInt(""+Temp.charAt(iy));
                      tizzl[tmpnum-1]++;
                  }
                  
                  return (tizzl);
            }


}


Clean it up and I think it does what you want it to do...  I tested it with eclipse
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question