Jump to content

What's the wrong with my code?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
/*Write a program to input a string and print the frequency of each character.

  Example:

  Input: COMPUTER HARDWaRE

  Output: 

  Characters    Frequency

       A            1

       a            1

       C            1

       D            1

       E            2

       H            1

       M            1

       O            1

       P            1

       R            3

       T            1

       U            1

       W            1

        */

       import java.io.*;

       class Frequency

       {

           String s;

           int i,j,l,f;

           void display()throws IOException

           {

               BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

               System.out.println("Enter a string in upper case");

               s=br.readLine();

               l=s.length();

               for(i=65;i<=122;i++)

               {

                   f=0;

                   for(j=0;j<l;j++)

                   {

                   if(s.charAt(j)==i)

                   {

                       f++;

                    }

                }

                if(f>0)

                {

                    System.out.println((char)i+"\t"+f);

                }

            }

        }

    }

Edited by arunjib, 12 June 2011 - 02:17 AM.


#2
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Sorry, the program is now ok. there is no error. the thread may be marked as closed.

#3
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
if i made change as follows i can get frequency in both upper or lower case
for(i=65;i<=90;i++)
but how can i get blank space in count?

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
"Change as follows"? There's no change in the code you posted.

BTW:

for(int i='A' ; i<='Z' ; i++)

Is much more clear.

#5
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I have changes for loop as
for(i=65;i<=122;i++)
now please tell me how to count and display number blank character

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Note that 65 -> 122 will also count [, \, ], ^, _, `
Space is number 32. So either you can take 32-122 (will also count ", &, (, ), 0-9, :, ; and a bunch of others)
Or you can do your inner loop 1 more time separately
for(j=0;j<l;j++)

      {

     [B][COLOR="#000080"]if[/COLOR][/B](s.charAt(j)==' ')

      {

          f++;

       }

   }

  [B][COLOR="#000080"]if[/COLOR][/B](f>0)

   {

       System.out.println(' ' +[B][COLOR="#008000"]"t"[/COLOR][/B]+f);

   }

What to me would seem better is:

String input = [B][COLOR="#008000"]"COMPUTER HARDWARE"[/COLOR][/B];

[B][COLOR="#000080"]char[/COLOR][/B][] letters = input.toCharArray();

Arrays.sort(letters);

[B][COLOR="#000080"]for[/COLOR][/B](int i=0 ; i<letters.length ; i++){

   [B][COLOR="#000080"]if[/COLOR][/B](i > 0 && letters[i] == letters[i-1]){

       [B][COLOR="#000080"]continue[/COLOR][/B];

    }

   [B][COLOR="#000080"]int[/COLOR][/B] total = 0;

   [B][COLOR="#000080"]for[/COLOR][/B](char c : letters){

       [B][COLOR="#000080"]if[/COLOR][/B](c == letters[i]){

            total++;

        }

    }

    System.out.println(letters[i] + [B][COLOR="#008000"]": "[/COLOR][/B] + total);

}

Here you just count the characters that are in the input. Not a predefined list of a-z A-Z

(The inner loop could be optimized as I sorted the array first, but to not make it look too difficult i just loop trough the whole array)

#7
arunjib

arunjib

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
Thank you sir.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users