Page 1 of 1

urrgent problem .. please help me

Posted: Wed Mar 01, 2006 8:21 pm
by rymdare
hi everybody
I have problem .. with an algorithm ..
write a code wich read a text and find the number of each repeated element in that text and replace the number with asterisk
ex:
this is a nice day:
a: **
c: *
e:*
h: *
i: ***
and so on
this is the code I write

public class man
{
public static void main (String[] args) throws java.io.IOException
{
java.util.Scanner in = new java.util.Scanner (System.in);
final int NUMCHARS = 33;

int[] lower = new int[NUMCHARS];

char current; //the current character being processed

int nbrSpaces = 0; //counter for non-alphabetics
int nbrPoints = 0;
int nbrSemicolon = 0;
int nbrColon = 0;
int nbrQuestionmark = 0;
int nbruppropstecken = 0;
int nbrRows = 0;
System.out.println ("Enter a sentence:");

String line = in.nextLine ();

// Count the number of each letter occurrence


for (int ch = 0; ch < line.length(); ch++)

{
current = line.charAt(ch);
if (current >= 'a' && current <= 'z')
lower[current-'a']++;
else if (current == ' ')
nbrSpaces ++;
else if
(current == '.')
nbrPoints ++;
else if (current == ';')
nbrSemicolon ++;
else if (current == ',')
nbrColon ++;
else if (current == '?')
nbrQuestionmark ++;
else if (current == '!')
nbruppropstecken ++;
else if (current == '\n')
nbrRows ++;
}
System.out.println ();
for (int letter=0; letter < lower.length;letter++)
{

System.out.print((char)(letter + 'a'));
System.out.println(": " + lower[letter]);
}
System.out.println ();
System.out.println ("blanktecken " + nbrSpaces);
System.out.println ("." + nbrPoints);
System.out.println (";" + nbrSemicolon);
System.out.println ("," + nbrColon);
System.out.println ("?" + nbrQuestionmark);
System.out.println ("!" + nbruppropstecken);
System.out.println ("antalrader" + nbrRows);
}
}


but now .. I can`t find a way to replace the number of repeated element with asterisk
I appreciate your help
:(

Posted: Thu Mar 02, 2006 10:53 am
by misof

Code: Select all

for (int i=0; i < someCharCount; i++) System.out.print('*'); 

is not good enough? ;)

Re: urrgent problem .. please help me

Posted: Thu Mar 02, 2006 2:32 pm
by rymdare
rymdare wrote:hi everybody
I have problem .. with an algorithm ..
write a code wich read a text and find the number of each repeated element in that text and replace the number with asterisk
ex:
this is a nice day:
a: **
c: *
e:*
h: *
i: ***
and so on
this is the code I write

public class man
{
public static void main (String[] args) throws java.io.IOException
{
java.util.Scanner in = new java.util.Scanner (System.in);
final int NUMCHARS = 33;

int[] lower = new int[NUMCHARS];

char current; //the current character being processed

int nbrSpaces = 0; //counter for non-alphabetics
int nbrPoints = 0;
int nbrSemicolon = 0;
int nbrColon = 0;
int nbrQuestionmark = 0;
int nbruppropstecken = 0;
int nbrRows = 0;
System.out.println ("Enter a sentence:");

String line = in.nextLine ();

// Count the number of each letter occurrence


for (int ch = 0; ch < line.length(); ch++)

{
current = line.charAt(ch);
if (current >= 'a' && current <= 'z')
lower[current-'a']++;
else if (current == ' ')
nbrSpaces ++;
else if
(current == '.')
nbrPoints ++;
else if (current == ';')
nbrSemicolon ++;
else if (current == ',')
nbrColon ++;
else if (current == '?')
nbrQuestionmark ++;
else if (current == '!')
nbruppropstecken ++;
else if (current == '\n')
nbrRows ++;
}
System.out.println ();
for (int letter=0; letter < lower.length;letter++)
{

System.out.print((char)(letter + 'a'));
System.out.println(": " + lower[letter]);
}
System.out.println ();
System.out.println ("blanktecken " + nbrSpaces);
System.out.println ("." + nbrPoints);
System.out.println (";" + nbrSemicolon);
System.out.println ("," + nbrColon);
System.out.println ("?" + nbrQuestionmark);
System.out.println ("!" + nbruppropstecken);
System.out.println ("antalrader" + nbrRows);
}
}


but now .. I can`t find a way to replace the number of repeated element with asterisk
I appreciate your help
:(


I don`t really get it where should I write that and how it gone work :oops:

Posted: Thu Mar 02, 2006 3:03 pm
by chunyi81
for (int letter=0; letter < lower.length;letter++)
{

System.out.print((char)(letter + 'a'));
System.out.println(": " + lower[letter]);
}
System.out.println ();
System.out.println ("blanktecken " + nbrSpaces);
System.out.println ("." + nbrPoints);
System.out.println (";" + nbrSemicolon);
System.out.println ("," + nbrColon);
System.out.println ("?" + nbrQuestionmark);
System.out.println ("!" + nbruppropstecken);
System.out.println ("antalrader" + nbrRows);
}
This part of your code, instead of printing out lower[letter] for the number of letters of counted, change it to a for loop like what misof said. Same for the spaces, nbrPoints, nbrSemicolon, nbrColon, nbrQuestionmark, nbruppropstecken, nbrRows.

Re: urrgent problem .. please help me

Posted: Thu Mar 02, 2006 4:11 pm
by rymdare
thanks ... it was a big help ...
just one more guestion .. if I want print out just the letters that is in the text , not all the letters ..what I should have instead of

System.out.print((char)(letter + 'a'));

Posted: Thu Mar 02, 2006 6:34 pm
by Darko
Well, best thing you could do is make an array of size 128 (say count[]) and store the number of occurences of each character in there. Say character is '\n', you do count['\n']++
In the end you just print the number of characters you want, same goes for letters, you just print the ones that have a count > 0.

Hope this helps.

Darko

P.S. You have to read the text character by character that way, otherwise increase count['\n'] every time you read a line. Although that might not work, text does not have to end with a newline character.

Re: urrgent problem .. please help me

Posted: Thu Mar 02, 2006 11:36 pm
by rymdare
thank you :D