Results 1 to 13 of 13
  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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.


    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
     
    }
    }
    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default 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.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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
    Java 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!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default 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.

  5. #5
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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.

  6. #6
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: print out occurrences of number in a string

    Within any for loop you have the index available. In
    Java Code:
    for(int index = 0; index<10; index++)
    The variable 'index' would be your index.

  7. #7
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  8. #8
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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...

  9. #9
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default 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. #10
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: print out occurrences of number in a string

    Quote Originally Posted by joeyvitoro View Post
    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.

  12. #12
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: print out occurrences of number in a string

    Quote Originally Posted by Junky View Post
    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.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default 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.

Similar Threads

  1. Replies: 12
    Last Post: 03-28-2011, 09:30 PM
  2. Replies: 3
    Last Post: 02-03-2011, 10:19 PM
  3. I can only print string literals
    By leeavital in forum New To Java
    Replies: 1
    Last Post: 12-11-2009, 01:20 AM
  4. Number of occurrences in a int[ ]
    By random0munky in forum New To Java
    Replies: 10
    Last Post: 10-10-2009, 10:22 PM
  5. Replies: 20
    Last Post: 05-14-2008, 09:42 AM

Posting Permissions

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