print out occurrences of number in a string
I'm trying to make a program that counts up how many digits 0-9 are in a string and organizes the digits by frequency
input: abc121 -->
output:
digit Frequencies
0 0
1 2
2 1
3 0
and so on through 9.
Code:
public class stuck{
public static void main(String[] args){
String s= "abc1";
int[] count=new int[10];
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
count[i]++; // Finds if a character is a digit and raises the count
}//end if loop
}// ends for loop
for(int j=0;j<count.length;j++)
System.out.print(count[j]);// Prints the array and its counts
}
}
I see the error in my code. It prints it out where it is in the string, rather than sorting it by number...
So the output for abc1 is:
000100000 because the 1 occurs at string position 4.
How should I go about fixing this error? I just cant seem to figure it out! If anyone could point me in the right direction I'd really appreciate it.
Re: print out occurrences of number in a string
You need to parse the char '1' to the int 1 and then use that as the index of your array.
Re: print out occurrences of number in a string
Ok, thanks, I didn't use parse but I saw the error- needed to get char to int. I'm trying to think now how to get the list
like
number count
0 2
1 0
2 1
etc.
I thought about making another array list for the left, but I can't make them side by side.Instead I had the count on top then the count underneath in a long vertical line...because each had their own for loop
So I tried to make a simpler example
Code:
public class SideBySide{
public static void main(String[] args){
int [] Array1={0,1};
int[] Array2={9,8};
for(int i=0;i<Array1.length;i++)
System.out.println(Array1[i]);
}
}
I know right now it just prints
0
1
because I can't figure out whether I need to make another for loop, or how to get the other one to print next to it not below it. Any help would be really appreciated!
Re: print out occurrences of number in a string
You don't need 2 arrays. Simply print the index and the count at that index.
Re: print out occurrences of number in a string
I don't see how to print out the index though....I know the for loop gives me the number at the index, but not the index itself..
I thought of the indexOf method but I don't think that applies here.
So I can get it to print the count part, but not the index.
Re: print out occurrences of number in a string
Within any for loop you have the index available. In
Code:
for(int index = 0; index<10; index++)
The variable 'index' would be your index.
Re: print out occurrences of number in a string
To get that format you will need to do a bit more. Research escape characters such as /n and see what you come up with.
Re: print out occurrences of number in a string
thanks, I see...so I just use System.out.println(index);
and that will give me a column...and now the rest is just formatting it to make the digit appear only once at the top.
I found this site :Characters (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
So to get \n, a newline, how would I set it up..I tried doing it,like System.out.println(\n "word" \n ) but that didnt work
So how do I actually set it up? Even if you send me to another site that shows more examples...
Re: print out occurrences of number in a string
Just use it in the string.
Code:
System.out.println("Hello\nworld");
Output:
Hello
world
Re: print out occurrences of number in a string
As I remember being a newbie, I and you did get the general idea of escape characters, I'll just tell you. The escape characters need to be in quotes.
Re: print out occurrences of number in a string
Quote:
Originally Posted by
joeyvitoro
The escape characters need to be in quotes.
It is! Do you see the quote before the H and the quote after the d? Well that makes the escape character inside the quotes.
Re: print out occurrences of number in a string
Quote:
Originally Posted by
Junky
It is! Do you see the quote before the H and the quote after the d? Well that makes the escape character inside the quotes.
Calm down there partner. I was addressing the OP on her post. I will never understand the need for such attitude on here. If you don't want to be nice help people out a bit, don't.
Re: print out occurrences of number in a string
Ahh!
My apologies. I thought you were replying to the post directly above yours. In other fora I have been to it actually tells you to which post you are replying.