Results 1 to 13 of 13
- 10-25-2011, 12:42 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
I see the error in my code. It prints it out where it is in the string, rather than sorting it by number...Java 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 } }
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.
- 10-25-2011, 01:22 AM #2
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.
- 10-25-2011, 04:33 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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
I know right now it just printsJava 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]); } }
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!
- 10-25-2011, 04:41 AM #4
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.
- 10-25-2011, 04:34 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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.
- 10-25-2011, 06:24 PM #6
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
Re: print out occurrences of number in a string
Within any for loop you have the index available. In
The variable 'index' would be your index.Java Code:for(int index = 0; index<10; index++)
- 10-25-2011, 06:25 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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.
- 10-25-2011, 07:14 PM #8
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
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...
- 10-25-2011, 07:23 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: print out occurrences of number in a string
Just use it in the string.
Java Code:System.out.println("Hello\nworld"); Output: Hello world
- 10-25-2011, 07:32 PM #10
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
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.
- 10-25-2011, 11:21 PM #11
- 10-25-2011, 11:31 PM #12
Member
- Join Date
- Oct 2011
- Posts
- 90
- Rep Power
- 0
- 10-25-2011, 11:36 PM #13
Similar Threads
-
to print first and last number from a set of input numbers
By 95673641 in forum New To JavaReplies: 12Last Post: 03-28-2011, 09:30 PM -
print out string name with the space :) between the first and last name
By the beginner in forum New To JavaReplies: 3Last Post: 02-03-2011, 10:19 PM -
I can only print string literals
By leeavital in forum New To JavaReplies: 1Last Post: 12-11-2009, 01:20 AM -
Number of occurrences in a int[ ]
By random0munky in forum New To JavaReplies: 10Last Post: 10-10-2009, 10:22 PM -
the number of occurrences of each alphabetical letter in the string.
By masaka in forum New To JavaReplies: 20Last Post: 05-14-2008, 09:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks