Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Occurrences of eac digit in a string

Posted on 2007-11-27
Medium Priority
2,130 Views
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;
}
``````
0
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

LVL 23

Accepted Solution

Ajay-Singh earned 172 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;
}
``````
0

LVL 17

Assisted Solution

contactkarthi earned 164 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;
}
``````
0

LVL 9

Assisted Solution

the_b1ackfox earned 164 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

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
###### Suggested Courses
Course of the Month5 days, 18 hours left to enroll