Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Question the number of occurrences of each alphabetical letter in the string.

    i try this code but it reapts result please help me
    Java Code:
    import java.util.*;
    public class Main {
      static Scanner console= new Scanner(System.in);
        public static void main(String[] args) 
        {
        System.out.print("Please Enter aString ");
        System.out.println();
        String str =console.next();
        char[]third =str.toCharArray();
       for(int counter =0;counter<third.length;counter++)
       {   char letter= third[counter];
           int flage=0;
    	for (int i=0; i<third.length; i++) 
            {
            if (letter==third[i])  
    	flage++;
            else continue;
    	}
            
          
       }
    		
        }
    public static void counter (int x)
    {
     int []count= new int[x];
     // this method to store the number of occurrences of each 
     //alphabetical letter in the string
    }
    public static void read (int x)
    {
    // this method should recive acharacter 
    // then update its the corresponding counter    
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    if the frist character is a for example
    and its counter is 2 then the third character is also a it output the same counter 2 i donot want this i want that he tld me that the character a counter is 2 one time only

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    How it happened. If the first character is 'a' the counter should be 1, and if the third character is 'a' again the counter should be 2 and so on.

    What you mean,

    he tld me that the character a counter is 2 one time only

  5. #5
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    it is the same character i want to count uniqe character . no repitition

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Say the string is "this is a line of text as a string"

    what is your expecting output. You mean wants to count a character at once.

  7. #7
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    yes You mean wants to count a character at once
    the output should be
    t ocuures 3
    h ocuures 1
    i ocuures3
    s ocuures4
    discard i then calcualte a because i was calculated before

  8. #8
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default check out this

    import java.util.*;
    import java.io.*;
    public class example{
    static Scanner console= new Scanner(System.in);
    public static void main(String[] args)
    {
    System.out.print("Please Enter aString ");
    System.out.println();
    String str =console.next();


    char[]third =str.toCharArray();
    for(int counter =0;counter<third.length;counter++)
    { char letter= third[counter];

    int flage=0;
    for ( int i=0; i<third.length; i++)
    {
    if (letter==third[i])

    flage++;


    }
    if(counter==0)
    {
    System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
    continue;
    }
    boolean flag=false;
    for(int j=counter-1;j>=0;j--)
    {
    if(letter==third[j])
    flag=true;
    }
    if(!flag)
    {

    System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
    }

    }
    }


    }




    Note: Your program will not take a whole String , i mean if u give String as
    "this is a line of text as a string"
    "Str" will only accepts "this" ...
    Last edited by kavithaprabhaker; 05-13-2008 at 11:23 AM.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    Join Date
    May 2008
    Posts
    6
    Rep Power
    0

    Default

    if we consider the white space then we need to use BufferedReader instead of scanner . the code goes here

    import java.util.*;
    import java.io.*;
    public class triangle{
    //static Scanner console= new Scanner(System.in);
    public static void main(String[] args) throws IOException
    {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please Enter aString ");
    System.out.println();
    String str=br.readLine();
    System.out.println(str);


    char[]third =str.toCharArray();
    for(int counter =0;counter<third.length;counter++)
    { char letter= third[counter];

    int flage=0;
    for ( int i=0; i<third.length; i++)
    {
    if (letter==third[i])

    flage++;


    }
    if(counter==0)
    {
    System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
    continue;
    }
    boolean flag=false;
    for(int j=counter-1;j>=0;j--)
    {
    if(letter==third[j])
    flag=true;
    }
    if(!flag)
    {

    System.out.println("The letter ="+letter+" is repeated "+flage+" No of Times ");
    }

    }
    }


    }

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, but if you want using scanner also it's possible. Just use nextLine() on scanner object. But in that case you have to think about whitespace. nextLine() calculate those too.

  12. #12
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    how to make method that receives a character and updates the corresponding counter

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Use switch-case statements there. But in that case you have to deal with large number of variables.

  14. #14
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    how please show me with code

  15. #15
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Here is a Switch Statement from Sun's Java tutorial section....
    freedom exists in the world of ideas

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, it's much simpler example. You can update a variable than printing values like in the example.

  17. #17
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    thanks alot but i am very new to java please show me this code
    Last edited by masaka; 05-14-2008 at 08:18 AM.

  18. #18
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    thanks alot but i am very new to java please show me this code
    REGARDS

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You can try something like this in very simple way. It's not much difficult at all.

    Java Code:
        private void counter(char ch) {
            int temp1 = 0;
            int temp2 = 0;
            int temp3 = 0;
            
            switch(ch){
                case 'a': temp1++; break;
                case 'b': temp2++; break;
                case 'c': temp3++; break;
                default: System.out.println("Invalid");
            }
            
            System.out.println("a has " + temp1 + " times\n" +
                    "b has " + temp2 + " times\n" +
                    "c has " + temp3 + " times\n");
        }

  20. #20
    masaka is offline Member
    Join Date
    Mar 2008
    Posts
    31
    Rep Power
    0

    Default

    very thanks to you
    regards
    Last edited by masaka; 05-14-2008 at 10:45 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  2. Drive letter sniff/open explorer window
    By iiz in forum New To Java
    Replies: 1
    Last Post: 04-04-2008, 03:50 AM
  3. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 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
  •