urrgent problem .. please help me

Write here if you have problems with your Java source code

Moderator: Board moderators

Post Reply
rymdare
New poster
Posts: 4
Joined: Wed Mar 01, 2006 8:02 pm
Contact:

urrgent problem .. please help me

Post 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
:(
misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof »

Code: Select all

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

is not good enough? ;)
rymdare
New poster
Posts: 4
Joined: Wed Mar 01, 2006 8:02 pm
Contact:

Re: urrgent problem .. please help me

Post 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:
chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post 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.
rymdare
New poster
Posts: 4
Joined: Wed Mar 01, 2006 8:02 pm
Contact:

Re: urrgent problem .. please help me

Post 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'));
Darko
Guru
Posts: 580
Joined: Fri Nov 11, 2005 9:34 am
Location: Calgary, Canada

Post 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.
rymdare
New poster
Posts: 4
Joined: Wed Mar 01, 2006 8:02 pm
Contact:

Re: urrgent problem .. please help me

Post by rymdare »

thank you :D
Post Reply

Return to “Java”