Computing.Net > Forums > Programming > Java String Problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Java String Problem

Reply to Message Icon

Name: kalishta
Date: January 10, 2004 at 13:28:45 Pacific
OS: Win2000
CPU/Ram: PentiumIII
Comment:

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:c3omputer

I 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:3o

but this is not what i want.i want :
Input: cooomputer
Output:c3omputer

thx in advance



Sponsored Link
Ads by Google

Response Number 1
Name: Gurjit Dhesi
Date: January 11, 2004 at 14:55:08 Pacific
Reply:

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?



0

Response Number 2
Name: SN
Date: January 11, 2004 at 23:53:11 Pacific
Reply:

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


0

Response Number 3
Name: kalishta
Date: January 14, 2004 at 11:17:49 Pacific
Reply:

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


0

Response Number 4
Name: kalishta
Date: January 14, 2004 at 11:20:00 Pacific
Reply:

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 :)


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


showing a file on an edit... Batch file: Adding to fil...



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Java String Problem

Java strings www.computing.net/answers/programming/java-strings/9620.html

JAVA String Tokenizer problem! www.computing.net/answers/programming/java-string-tokenizer-problem/4503.html

Java thread problem www.computing.net/answers/programming/java-thread-problem/13991.html