J2ME String Splitter Counter - java

J2ME String Tokenizer:
public String[] split(String toSplit, char delim, boolean ignoreEmpty) {
StringBuffer buffer = new StringBuffer();
Stack stringStack = new Stack();
for (int i = 0; i < toSplit.length(); i++) {
if (toSplit.charAt(i) != delim) {
buffer.append((char) toSplit.charAt(i));
} else {
if (buffer.toString().trim().length() == 0 && ignoreEmpty) {
} else {
stringStack.addElement(buffer.toString());
}
buffer = new StringBuffer();
}
}
if (buffer.length() != 0) {
stringStack.addElement(buffer.toString());
}
String[] split = new String[stringStack.size()];
for (int i = 0; i < split.length; i++) {
split[split.length - 1 - i] = (String) stringStack.pop();
}
stringStack = null;
buffer = null;
return split;
}
Method Used To Call It:
String toSplit = myThreadObject.GetInfo();
String del = DelimiterValue.getString();
char delimiter = del.charAt(0);
String[] result = split(toSplit, delimiter, false);
if (result != null) {
for (int i = 0; i < result.length; i++) {
System.out.println("The elements are: " + result[i]);
}
} else {
System.out.println("The result is empty");
}
This is an example of how to split up a string in J2ME, it is actually splitting up html content from a website pulled in a thread.
Can anyone tell me how I add a simple counter into this code to count the number of times the result is printed out, i.e. how many tokens there are? Because i am struggling.
Many thanks

No need to add a counter as the array has the public .length property which exposes the count for you. I added one line to your code (and a comment immediately before it). I also removed your check for result != null because your split() method will never return null. It returns a zero length array if there are no matches.
String toSplit = myThreadObject.GetInfo();
String del = DelimiterValue.getString();
char delimiter = del.charAt(0);
String[] result = split(toSplit, delimiter, false);
// add the line below:
System.out.println("There are " + result.length + " results");
for (int i = 0; i < result.length; i++) {
System.out.println("The elements are: " + result[i]);
}

Related

How to compress string on java without using map

I've recently started java and I want to compress a string like this:
Input:aaaaabbbbwwwccc Output:a5b4w3c3
Input:aaabbccds Output:a3b2c2ds
Input:Abcd Output:Abcd
The following code is what I have done but, it does not work.
public class CompressString {
public static void main(String[] args) {
String out = "";
Scanner in = new Scanner(System.in);
String input = in.next();
int length = input.length();
int counter = 1;
if (length == 0) {
System.out.println(" ");
} else {
for (int i = 0; i<length;i++){
if (input.charAt(i)==input.charAt(i+1)){
counter++;
}else {
if (counter == 1){
out = out+input.charAt(i-counter);
}else{
out = out+input.charAt(i-counter)+counter;
}
}
i++;
counter = 1;
}
System.out.println(out.toString());
}
}
}
The simplest program to do that would loop through each character in the string and check when the character is different from the previous seen one and, if so, add the last one and its count to the compressed string:
String input = "aaaaabbbbwwwccc";
StringBuilder compressed = new StringBuilder();
char last = 0;
int lastCount = 0;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (last == 0 || c != last) {
if (lastCount != 0) {
compressed.append(last);
if (lastCount > 1) {
compressed.append(lastCount);
}
}
last = c;
lastCount = 1;
} else {
lastCount++;
}
}
// take care of the last repeating sequence if any
if (lastCount > 0) {
compressed.append(last);
if (lastCount > 1) {
compressed.append(lastCount);
}
}
Here is a very compact way of doing this with a regex matcher along with a string buffer:
String input = "aaaaabbbbwwwccc";
Pattern r = Pattern.compile("(.)\\1{0,}");
Matcher m = r.matcher(input);
StringBuffer buffer = new StringBuffer();
while (m.find()) {
m.appendReplacement(buffer, m.group(1) + m.group(0).length());
}
m.appendTail(buffer);
System.out.println(buffer.toString());
This prints:
a5b4w3c3
For an explanation, the above logic searches for the regex pattern (.)\1{0,}. This will match any single character, along with that same character occurring again possibly one or more times afterwards. It then replaces with just the single character followed by the count of the number of times it occurs.

Array size issue for converting String to Unicode

I have a string "text" in one class which calls on a method in another class to convert text in various ways. In this method though I am left with an "ArrayIndexOutOfBoundsException" error.
public String toUnicode() {
char unicodeTextArray[] = new char[text.length()];
if (text == null || text.isEmpty()) {
return "";
}
String unicodeTextArrayString[] = new String[text.length()];
for (int i = 0; i < text.length(); i++) {
unicodeTextArray[i] = text.charAt(i);
if (unicodeTextArray[i] < 0x10) {
unicodeTextArrayString[i] = "\\u000" + Integer.toHexString(unicodeTextArray[i]);
} else if (unicodeTextArray[i] < 0x100) {
unicodeTextArrayString[i] = "\\u00" + Integer.toHexString(unicodeTextArray[i]);
} else if (unicodeTextArray[i] < 0x1000) {
unicodeTextArrayString[i] = "\\u0" + Integer.toHexString(unicodeTextArray[i]);
}
unicodeTextArrayString[i] = "\\u" + Integer.toHexString(unicodeTextArray[i]);
}
String unicode = unicodeTextArrayString[text.length()];
return unicode;
}
Changing one line to an arbitrarily large number such as:
String unicodeTextArrayString[] = new String[9999];
Results in no error, but it returns null.
I thought about setting an int variable to increase the length of the array, but * 4 was still too small of an array size and it seems like if I go too large it just returns null.
How could I go about getting the correct length of the array?
EDIT: I found a non-array approach that works, but I would still like to know if there is a way to make the above array approach work in some way.
public String toUnicode()
{
String unicodeString = "";
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
String s = String.format ("\\u%04x", (int)c);
unicodeString = unicodeString + s;
}
return unicodeString;
}
EDIT 2: In case anyone reading this is curious, to get the decimal value of the unicode:
public String toUnicode()
{
String unicodeString = "";
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
int unicodeDecimal = c;
unicodeString = unicodeString + unicodeDecimal + " ";
}
return unicodeString;
}
EDIT 3: I ended up deciding to use the following, which separates unicode decimals by space, and checks for the unicode value 10 (which means new line) and outputs a new line into the string instead of that value.
public String toUnicode()
{
String unicodeString = "";
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
int unicodeDecimal = c;
if (unicodeDecimal == 10)
{
unicodeString = unicodeString + "\n";
}
else
{
unicodeString = unicodeString + unicodeDecimal + " ";
}
}
return unicodeString;
}
couple of things
1) Move line
char unicodeTextArray[] = new char[text.length()]; after following code
if (text == null || text.isEmpty())
{
return "";
}
char unicodeTextArray[] = new char[text.length()];
2) Error is because of this String unicode = unicodeTextArrayString[text.length()];
e.g you get a text as "hello", then you initialized unicodeTextArrayString of size text.length() which is 5. So you can fetch back from this array for index 0 to 4 only, but you are trying to fetch from index 5, which is out of bounds.
3) After having said so, the code/logic seems wrong. I just modified your logic using StringBuilder instead. You can check for conversion logic
public static String toUnicode(String text)
{
if (text == null || text.isEmpty())
{
return "";
}
StringBuilder unicodeTextArrayString = new StringBuilder();
for (int i = 0; i < text.length(); i++)
{
char ch = text.charAt(i);
if (ch < 0x10)
{
unicodeTextArrayString.append("\\u000" + Integer.toHexString(ch));
}
else if (ch < 0x100)
{
unicodeTextArrayString.append("\\u00" + Integer.toHexString(ch));
}
else if (ch < 0x1000)
{
unicodeTextArrayString.append("\\u0" + Integer.toHexString(ch));
}
else
{
unicodeTextArrayString.append("\\u" + Integer.toHexString(ch));
}
}
return unicodeTextArrayString.toString();
}
4) If you want to use array based approach, then add each chars to arrays, and then iterate again through array where u stored chars, and then build a string (instead of getting a string from last index) and then return the string
this one is the culprit
String unicode = unicodeTextArrayString[text.length()];
edit:
If you really want to make the original code to work in some way, I think there are several ways to do it. The following code is one of them.
public String toUnicode() {
char unicodeTextArray[] = new char[text.length()];
if (text == null) {
return "";
}
String unicodeTextArrayString[] = new String[text.length()];
StringBuilder unicode= new StringBuilder();
for (int i = 0; i < text.length(); i++) {
unicodeTextArray[i] = text.charAt(i);
if (unicodeTextArray[i] < 0x10) {
unicodeTextArrayString[i] = "\\u000" + Integer.toHexString(unicodeTextArray[i]);
} else if (unicodeTextArray[i] < 0x100) {
unicodeTextArrayString[i] = "\\u00" + Integer.toHexString(unicodeTextArray[i]);
} else if (unicodeTextArray[i] < 0x1000) {
unicodeTextArrayString[i] = "\\u0" + Integer.toHexString(unicodeTextArray[i]);
} else
unicodeTextArrayString[i] = "\\u" + Integer.toHexString(unicodeTextArray[i]);
unicode = unicode.append(unicodeTextArrayString[i]);
}
return unicode.toString();
}

Compression algorithm in java

My goal is to write a program that compresses a string, for example:
input: hellooopppppp!
output:he2l3o6p!
Here is the code I have so far, but there are errors.
When I have the input: hellooo
my code outputs: hel2l3o
instead of: he213o
the 2 is being printed in the wrong spot, but I cannot figure out how to fix this.
Also, with an input of: hello
my code outputs: hel2l
instead of: he2lo
It skips the last letter in this case all together, and the 2 is also in the wrong place, an error from my first example.
Any help is much appreciated. Thanks so much!
public class compressionTime
{
public static void main(String [] args)
{
System.out.println ("Enter a string");
//read in user input
String userString = IO.readString();
//store length of string
int length = userString.length();
System.out.println(length);
int count;
String result = "";
for (int i=1; i<=length; i++)
{
char a = userString.charAt(i-1);
count = 1;
if (i-2 >= 0)
{
while (i<=length && userString.charAt(i-1) == userString.charAt(i-2))
{
count++;
i++;
}
System.out.print(count);
}
if (count==1)
result = result.concat(Character.toString(a));
else
result = result.concat(Integer.toString(count).concat(Character.toString(a)));
}
IO.outputStringAnswer(result);
}
}
I would
count from 0 as that is how indexes work in Java. Your code will be simpler.
would compare the current char to the next one. This will avoid printing the first character.
wouldn't compress ll as 2l as it is no smaller. Only sequences of at least 3 will help.
try to detect if a number 3 to 9 has been used and at least print an error.
use the debugger to step through the code to understand what it is doing and why it doesn't do what you think it should.
I am doing it this way. Very simple:
public static void compressString (String string) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
int count = 1;
while (i + 1 < string.length()
&& string.charAt(i) == string.charAt(i + 1)) {
count++;
i++;
}
if (count > 1) {
stringBuffer.append(count);
}
stringBuffer.append(string.charAt(i));
}
System.out.println("Compressed string: " + stringBuffer);
}
You can accomplish this using a nested for loops and do something simial to:
count = 0;
String results = "";
for(int i=0;i<userString.length();){
char begin = userString.charAt(i);
//System.out.println("begin is: "+begin);
for(int j=i+1; j<userString.length();j++){
char next = userString.charAt(j);
//System.out.println("next is: "+next);
if(begin == next){
count++;
}
else{
System.out.println("Breaking");
break;
}
}
i+= count+1;
if(count>0){
String add = begin + "";
int tempcount = count +1;
results+= tempcount + add;
}
else{
results+= begin;
}
count=0;
}
System.out.println(results);
I tested this output with Hello and the result was He2lo
also tested with hellooopppppp result he2l3o6p
If you don't understand how this works, you should learn regular expressions.
public String rleEncodeString(String in) {
StringBuilder out = new StringBuilder();
Pattern p = Pattern.compile("((\\w)\\2*)");
Matcher m = p.matcher(in);
while(m.find()) {
if(m.group(1).length() > 1) {
out.append(m.group(1).length());
}
out.append(m.group(2));
}
return out.toString();
}
Try something like this:
public static void main(String[] args) {
System.out.println("Enter a string:");
Scanner IO = new Scanner(System.in);
// read in user input
String userString = IO.nextLine() + "-";
int length = userString.length();
int count = 0;
String result = "";
char new_char;
for (int i = 0; i < length; i++) {
new_char = userString.charAt(i);
count++;
if (new_char != userString.charAt(i + 1)) {
if (count != 1) {
result = result.concat(Integer.toString(count + 1));
}
result = result.concat(Character.toString(new_char));
count = 0;
}
if (userString.charAt(i + 1) == '-')
break;
}
System.out.println(result);
}
The problem is that your code checks if the previous letter, not the next, is the same as the current.
Your for loops basically goes through each letter in the string, and if it is the same as the previous letter, it figures out how many of that letter there is and puts that number into the result string. However, for a word like "hello", it will check 'e' and 'l' (and notice that they are preceded by 'h' and 'e', receptively) and think that there is no repeat. It will then get to the next 'l', and then see that it is the same as the previous letter. It will put '2' in the result, but too late, resulting in "hel2l" instead of "he2lo".
To clean up and fix your code, I recommend the following to replace your for loop:
int count = 1;
String result = "";
for(int i=0;i<length;i++) {
if(i < userString.length()-1 && userString.charAt(i) == userString.charAt(i+1))
count++;
else {
if(count == 1)
result += userString.charAt(i);
else {
result = result + count + userString.charAt(i);
count = 1;
}
}
}
Comment if you need me to explain some of the changes. Some are necessary, others optional.
Here is the solution for the problem with better time complexity:
public static void compressString (String string) {
LinkedHashSet<String> charMap = new LinkedHashSet<String>();
HashMap<String, Integer> countMap = new HashMap<String, Integer>();
int count;
String key;
for (int i = 0; i < string.length(); i++) {
key = new String(string.charAt(i) + "");
charMap.add(key);
if(countMap.containsKey(key)) {
count = countMap.get(key);
countMap.put(key, count + 1);
}
else {
countMap.put(key, 1);
}
}
Iterator<String> iterator = charMap.iterator();
String resultStr = "";
while (iterator.hasNext()) {
key = iterator.next();
count = countMap.get(key);
if(count > 1) {
resultStr = resultStr + count + key;
}
else{
resultStr = resultStr + key;
}
}
System.out.println(resultStr);
}

Store parts of string in a string array

I have a string tajmahal.txt sarjan.pdf noorjahan.exe. I want to store this string in a string array such that ar[0] = tajmahal.txt, ar[1] = sarjan.pdf and ar[2] = noorjahan.exe. How can I do it without using any build functions in java (because I am using j2me which does not support many j2se functions). Any help would be great. Thanks in advance.
Since String.split is not available. You can see the implementation of split from this answer
public static String[] Split(String splitStr, String delimiter) {
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
// split
char[] chars = splitStr.toCharArray();
for (int i=0; i < chars.length; i++) {
if (delimiter.indexOf(chars[i]) != -1) {
// we bumbed into a delimiter
if (token.length() > 0) {
tokens.addElement(token.toString());
token.setLength(0);
}
} else {
token.append(chars[i]);
}
}
// don't forget the "tail"...
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] splitArray = new String[tokens.size()];
for (int i=0; i < splitArray.length; i++) {
splitArray[i] = (String)tokens.elementAt(i);
}
return splitArray;
}
String str="tajmahal.txt sarjan.pdf noorjahan.exe";
StringTokenizer st=new StringTokenizer(str," ");
String[] arr=new String[st.countTokens()];
int i=0;
while (st.hasMoreElements()){
arr[i]=st.nextToken();
i++;
}
There are implementations of StringTokenizer in J2me.
check out this example to help you out with the task.
StringTokenizer token;
token = new StringTokenizer(str);
int i=0;
while(token.hasMoreElements()){
ar[i++]= tok.nextToken();
}
It depends on which Java ME configuration/profile set you use.
When it comes to CLDC/MIDP, where there is no collections, StringTokenizer, split(), and so on.
count those space(' ')s first.
prepare a String[count + 1].
split each token while buffering characters with a StringBuffer.
static String[] split(final String string) {
// count spaces
int spaces = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == 0x20) {
spaces++;
}
}
// prepare the array and buffer
final String[] split = new String[spaces + 1];
final StringBuffer buffer = new StringBuffer();
int index = 0;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == 0x20) {
split[index++] = buffer.toString();
buffer.delete(0, buffer.length());
continue;
}
buffer.append(string.charAt(i));
}
return split;
}

how can i calculate the number of specific chars in a string?

Given a string how can i figure out the number of times each char in a string repeats itself
ex: aaaabbaaDD
output: 4a2b2a2D
public static void Calc() {
Input();
int count = 1;
String compressed = "";
for (int i = 0; i < input.length(); i++) {
if (lastChar == input.charAt(i)) {
count++;
compressed += Integer.toString(count) + input.charAt(i);
}
else {
lastChar = input.charAt(i);
count = 1;
}
}
System.out.println(compressed);
}
What you'r looking for is "Run-length encoding". Here is the working code to do that;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RunLengthEncoding {
public static String encode(String source) {
StringBuffer dest = new StringBuffer();
// iterate through input string
// Iterate the string N no.of.times where N is size of the string to find run length for each character
for (int i = 0; i < source.length(); i++) {
// By default run Length for all character is one
int runLength = 1;
// Loop condition will break when it finds next character is different from previous character.
while (i+1 < source.length() && source.charAt(i) == source.charAt(i+1)) {
runLength++;
i++;
}
dest.append(runLength);
dest.append(source.charAt(i));
}
return dest.toString();
}
public static String decode(String source) {
StringBuffer dest = new StringBuffer();
Pattern pattern = Pattern.compile("[0-9]+|[a-zA-Z]");
Matcher matcher = pattern.matcher(source);
while (matcher.find()) {
int number = Integer.parseInt(matcher.group());
matcher.find();
while (number-- != 0) {
dest.append(matcher.group());
}
}
return dest.toString();
}
public static void main(String[] args) {
String example = "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW";
System.out.println(encode(example));
System.out.println(decode("1W1B1W1B1W1B1W1B1W1B1W1B1W1B"));
}
}
This program first finds the unique characters or numbers in a string. It will then check the frequency of occurance.
This program considers capital and small case as different characters. You can modify it if required by using ignorecase method.
import java.io.*;
public class RunLength {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
System.out.println("Please enter the string");
String str = br.readLine();//the input string is in str
calculateFrequency(str);
}
private static void calculateFrequency(String str) {
int length = str.length();
String characters[] = new String[length];//to store all unique characters in string
int frequency[] = new int[length];//to store the frequency of the characters
for (int i = 0; i < length; i++) {
characters[i] = null;
frequency[i] = 0;
}
//To get unique characters
char temp;
String temporary;
int uniqueCount = 0;
for (int i = 0; i < length; i++) {
int flag = 0;
temp = str.charAt(i);
temporary = "" + temp;
for (int j = 0; j < length; j++) {
if (characters[j] != null && characters[j].equals(temporary)) {
flag = 1;
break;
}
}
if (flag == 0) {
characters[uniqueCount] = temporary;
uniqueCount++;
}
}
// To get the frequency of the characters
for(int i=0;i<length;i++){
temp=str.charAt(i);
temporary = ""+temp;
for(int j=0;i<characters.length;j++){
if(characters[j].equals(temporary)){
frequency[j]++;
break;
}
}
}
// To display the output
for (int i = 0; i < length; i++) {
if (characters[i] != null) {
System.out.println(characters[i]+" "+frequency[i]);
}
}
}}
Some hints: In your code sample you also need to reset count to 0 when the run ends (when you update lastChar). And you need to output the final run (after the loop is done). And you need some kind of else or continue between the two cases.
#Balarmurugan k's solution is better - but just by improving upon your code I came up with this -
String input = "aaaabbaaDD";
int count = 0;
char lastChar = 0;
int inputSize = input.length();
String output = "";
for (int i = 0; i < inputSize; i++) {
if (i == 0) {
lastChar = input.charAt(i);
count++;
} else {
if (lastChar == input.charAt(i)) {
count++;
} else {
output = output + count + "" + lastChar;
count = 1;
lastChar = input.charAt(i);
}
}
}
output = output + count + "" + lastChar;
System.out.println(output);

Categories