Results 1 to 5 of 5
  1. #1
    taipower26 is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Unhappy how to count the char from the input string

    Code following
    Java Code:
    import java.io.*;
    import java.util.*;
     
    class  test001 {
      public static void main(String[] args) throws Exception{
    	  
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("請輸入文章字串\n ");
      String st=br.readLine().replaceAll("\\W", "").toLowerCase();
    
      
      System.out.println(st);	
       
      
      
         char str[] =st.toCharArray();
    
      for(int counter =0 ; counter < str.length+1 ; counter++ ){  
    	  if(!( str[counter] >= 97 &&  str[counter] <= 122 ))
      		continue;
     
    	  	char ch1= str[counter];
    	 
    	  	int count1=0;
    	  	int count2=0;
    	  
    	  	for( int i=0; i<str.length; i++){
    	  			for (int j=0;j<str.length+1;j++){
    	  				if (ch1==str[i])    count1 ++;
    	  				if (ch1==str[i] && ch1==str[j]) count2++;
    	  			}
    	  	}
    	boolean pig=false;
      	  for(int j	=counter-1;j>=0;j--){
    		if(ch1==str[j])
    
    			pig=true;
    			}
    		if(!pig){
    	 System.out.println("字元 :"+ch1 + " occurs "+count1+" times ");
    	 System.out.println("字元 :"+ch1+ch1 + " occurs "+count2+" times ");
     		}
      	}
      }
       
    }

    My question is
    for example
    input "abcddwlkkmn"

    count a,b,c...z how much for each
    count aa,ab,ac,...az how much for each

    Thank you :)
    Last edited by Tolls; 03-13-2013 at 09:34 AM. Reason: Fixed code tag

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: how to count the char from the input string

    So what does your code do that it shouldn't?
    Where is it going wrong?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    taipower26 is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: how to count the char from the input string

    Quote Originally Posted by Tolls View Post
    So what does your code do that it shouldn't?
    Where is it going wrong?

    My question is i dont know how to code the data structure to control i/o
    for "for loops"

    thanks :)

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    393
    Rep Power
    5

    Default Re: how to count the char from the input string

    Hi taipower26,

    You will need to be much clearer than that. We could do with a full description of what you are want to achieve in the program to provide you with the correct information.

    Regards.

  5. #5
    Join Date
    Jan 2013
    Posts
    24
    Rep Power
    0

    Default Re: how to count the char from the input string

    Similar problem in about every Ivor horton book ever written.
    Seperate the individual characters of the string into a simple char arrary
    Java Code:
    char[] cArray = new char[str.length];
    //convert the string to a char array with a loop.
    for(int i = 0; i < str.length;i++)
    {
         cArray[i] = str[0];//string automatically have the index.
    }
    PSEUDO
    Java Code:
    for(i = 0; cArray.length;i++)
    {
       for(ii = 96;ii < 122;ii++)//or whatever the char range is for you /--a list of chars--/     http://www.scism.lsbu.ac.uk/jfl/Appa/appa4.html
         {
           if (cArray[i] == char(ii))
              {
                count ++ ;// increase count
               } 
         }
    }
    }
    that what you're after? Not really clear on what you're asking man.

Similar Threads

  1. Replies: 13
    Last Post: 10-16-2012, 10:47 PM
  2. Char Count Issue
    By Goldie in forum New To Java
    Replies: 3
    Last Post: 03-28-2011, 12:02 AM
  3. check a string char by char
    By Sotsiak in forum New To Java
    Replies: 2
    Last Post: 10-23-2010, 09:24 PM
  4. Count the characters in an input box.
    By TheRocket in forum New To Java
    Replies: 3
    Last Post: 11-20-2008, 01:25 PM
  5. Help with, String, Char
    By lenny in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 02:58 PM

Posting Permissions

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