Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi i nee some help with Java Strings.
I need to writte a programm that will count the number of characters.Exampl:
Input: cooomputer
Output:c3omputerI have written only the part that counts the characters:
import java.io.*;
import java.lang.*;public class check2 {
public static void main(String[] args) throws IOException
{
String text = "";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name;System.out.println("Schreiben Sie Ihre Name: ");
text = in.readLine();
int oCount = 0;
int index = -1;
int i=0;
String oStr="o";
int textLength = text.length();index = text.indexOf(oStr);
//for (int i=0;i<textLength;i++){
while(index>=0)
{index = index + oStr.length();
index = text.indexOf(oStr,index);
oCount++;
}System.out.println(+oCount +oStr);
}
}
his code works like this:
input: cooomputer
output:3obut this is not what i want.i want :
Input: cooomputer
Output:c3omputerthx in advance

Hi,
Why dont you use the Java's String package?
and just use the .length() method to get the length (number of characters) of the string I/P by the user ... or isnt this an option?

Hello-
gurjit...Try reading the question again. What he really means is that he wants to find out the length of any substring of repeated characters and replace it by the number of times the character was repeated followed by one instance of the character.I won't bother telling you everything that's wrong with your code...It doesn't look like you really expected it to work. Here's a possible algorithm in pseudocode...I know this is homework but I'm feeling generous...And you did give it an attempt, and I'm sure my code will have a mistake or two in there you'll have to find and correct.
String answer;
String input;
//get input, make sure it is not null
//currchar keeps track of which character we are looking
at
char currchar=input.charAt(0);
//currnum keeps track of how many times we've seen
currchar (in a row)
int currnum=1;
//we start at 1 because we already checked out the
first character
for (int i=1; i<input.length(); i++)
{
//see if the next character is the same as the last
one. If so, increment currnum. If not, write the
appropriate string to the answer string and move on.
if (input.charAt(i)==currchar)
{
currnum++;
}
else
{
if (currnum>1)
answer=answer+currnum+currchar;
else
answer=answer+currchar;
}
}
return answer;Good luck,
-SN

Hi everybody i got the solution.Ths is the code:
---------------------------
import java.io.*;
import java.lang.*;
public class check1
{
public static void main(String[] args) throws IOException
{
String text = "";BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String name;System.out.println("Schreiben Sie Ihre Name/Writte a word: ");
text = in.readLine();
StringBuffer output = new StringBuffer();
int count = 1;
int index = 0;
char current;
char next;for (current = text.charAt(index++); index < text.length(); index++) {
// read ahead
next = text.charAt(index);// compare characters
if (next == current) {
count++;
}
else { // new character found
// at end of run, output and reset count
if (count > 1) {
output.append(count);
count = 1;
}
output.append(current);
}
// make new character current
current = next;
}
output.append(current); // output last character// print results
System.out.println(output);}
}
----------------------
This code does the following:Input: Coooomputting
Output: C4ompu2ting

sorry guys for the bad loking of the code,but dunno how to posti it just as SN did.
p.s. but important is that the code works :)

![]() |
showing a file on an edit...
|
Batch file: Adding to fil...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |