Expanding a condensed stringHow to generate a random alpha-numeric string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?Convert a string to an integer in JavaScript?How can I convert a stack trace to a string?How to split a string in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?Why is char[] preferred over String for passwords?Why does this code using random strings print “hello world”?

What should be the ideal length of sentences in a blog post for ease of reading?

What does "Scientists rise up against statistical significance" mean? (Comment in Nature)

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

I'm just a whisper. Who am I?

Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?

When is "ei" a diphthong?

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

El Dorado Word Puzzle II: Videogame Edition

In One Punch Man, is King actually weak?

Unable to disable Microsoft Store in domain environment

How much do grades matter for a future academia position?

Showing mass murder in a kid's book

What does "tick" mean in this sentence?

Quoting Keynes in a lecture

Do people actually use the word "kaputt" in conversation?

How to make a list of partial sums using forEach

What is the meaning of "You've never met a graph you didn't like?"

Proving an identity involving cross products and coplanar vectors

How would a solely written language work mechanically

Limit max CPU usage SQL SERVER with WSRM

Why Shazam when there is already Superman?

When and why was runway 07/25 at Kai Tak removed?

Do you waste sorcery points if you try to apply metamagic to a spell from a scroll but fail to cast it?

Would a primitive species be able to learn English from reading books alone?



Expanding a condensed string


How to generate a random alpha-numeric string?How do I read / convert an InputStream into a String in Java?How to get an enum value from a string value in Java?Convert a string to an integer in JavaScript?How can I convert a stack trace to a string?How to split a string in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?Why is char[] preferred over String for passwords?Why does this code using random strings print “hello world”?













1















I am trying take a string similar to this: 3A5o2n4t and expand it back to a string like this: AAAooooonntttt (the number in front of the letter is how many times the letter repeats)
I was trying to use Integer.parseInt() to get the number in front of the letter, but it grabs all of the numbers. Is there a way to grab one number at a time? Also, does my code look okay after that issue is resolved? Or am I missing a bit still?



public String runLengthDecoding(String str3) 
String convert = "";
int number = 0;
if (! str3.isEmpty())
convert = str3.charAt(0) + "";

for (int i = 0; i <= str3.length() - 1; i++)
if (Character.isDigit(str3.charAt(i))) //true or false, the current character is a digit
String temp = "" + str3.charAt(i); //if true, make that character a string
number = Integer.parseInt(temp); /*now store that character as a number (but I only want the current
number, not all the numbers in the string*/
System.out.println(number); /*Testing to see what number is, which is where I found it was
storing all the numbers */
String temp2 = str3.charAt(i + 1) + ""; //Its supposed to start making a string that prints the character in front of it
convert = temp2.repeat(number); //it will print the character however many times that number was stored as



return convert;



Also I have not yet learned how to use arrays, that is why I am not using an array.










share|improve this question






















  • It seems that you are using an undefined method here -> temp2.repeat(number);

    – Mark
    Mar 7 at 3:19






  • 1





    When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

    – user85352
    Mar 7 at 3:21











  • Can you post the link where you found the String.repeat() method?

    – Mark
    Mar 7 at 3:23











  • w3schools.com/jsref/jsref_repeat.asp

    – user85352
    Mar 7 at 3:26











  • The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

    – Mark
    Mar 7 at 3:31















1















I am trying take a string similar to this: 3A5o2n4t and expand it back to a string like this: AAAooooonntttt (the number in front of the letter is how many times the letter repeats)
I was trying to use Integer.parseInt() to get the number in front of the letter, but it grabs all of the numbers. Is there a way to grab one number at a time? Also, does my code look okay after that issue is resolved? Or am I missing a bit still?



public String runLengthDecoding(String str3) 
String convert = "";
int number = 0;
if (! str3.isEmpty())
convert = str3.charAt(0) + "";

for (int i = 0; i <= str3.length() - 1; i++)
if (Character.isDigit(str3.charAt(i))) //true or false, the current character is a digit
String temp = "" + str3.charAt(i); //if true, make that character a string
number = Integer.parseInt(temp); /*now store that character as a number (but I only want the current
number, not all the numbers in the string*/
System.out.println(number); /*Testing to see what number is, which is where I found it was
storing all the numbers */
String temp2 = str3.charAt(i + 1) + ""; //Its supposed to start making a string that prints the character in front of it
convert = temp2.repeat(number); //it will print the character however many times that number was stored as



return convert;



Also I have not yet learned how to use arrays, that is why I am not using an array.










share|improve this question






















  • It seems that you are using an undefined method here -> temp2.repeat(number);

    – Mark
    Mar 7 at 3:19






  • 1





    When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

    – user85352
    Mar 7 at 3:21











  • Can you post the link where you found the String.repeat() method?

    – Mark
    Mar 7 at 3:23











  • w3schools.com/jsref/jsref_repeat.asp

    – user85352
    Mar 7 at 3:26











  • The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

    – Mark
    Mar 7 at 3:31













1












1








1








I am trying take a string similar to this: 3A5o2n4t and expand it back to a string like this: AAAooooonntttt (the number in front of the letter is how many times the letter repeats)
I was trying to use Integer.parseInt() to get the number in front of the letter, but it grabs all of the numbers. Is there a way to grab one number at a time? Also, does my code look okay after that issue is resolved? Or am I missing a bit still?



public String runLengthDecoding(String str3) 
String convert = "";
int number = 0;
if (! str3.isEmpty())
convert = str3.charAt(0) + "";

for (int i = 0; i <= str3.length() - 1; i++)
if (Character.isDigit(str3.charAt(i))) //true or false, the current character is a digit
String temp = "" + str3.charAt(i); //if true, make that character a string
number = Integer.parseInt(temp); /*now store that character as a number (but I only want the current
number, not all the numbers in the string*/
System.out.println(number); /*Testing to see what number is, which is where I found it was
storing all the numbers */
String temp2 = str3.charAt(i + 1) + ""; //Its supposed to start making a string that prints the character in front of it
convert = temp2.repeat(number); //it will print the character however many times that number was stored as



return convert;



Also I have not yet learned how to use arrays, that is why I am not using an array.










share|improve this question














I am trying take a string similar to this: 3A5o2n4t and expand it back to a string like this: AAAooooonntttt (the number in front of the letter is how many times the letter repeats)
I was trying to use Integer.parseInt() to get the number in front of the letter, but it grabs all of the numbers. Is there a way to grab one number at a time? Also, does my code look okay after that issue is resolved? Or am I missing a bit still?



public String runLengthDecoding(String str3) 
String convert = "";
int number = 0;
if (! str3.isEmpty())
convert = str3.charAt(0) + "";

for (int i = 0; i <= str3.length() - 1; i++)
if (Character.isDigit(str3.charAt(i))) //true or false, the current character is a digit
String temp = "" + str3.charAt(i); //if true, make that character a string
number = Integer.parseInt(temp); /*now store that character as a number (but I only want the current
number, not all the numbers in the string*/
System.out.println(number); /*Testing to see what number is, which is where I found it was
storing all the numbers */
String temp2 = str3.charAt(i + 1) + ""; //Its supposed to start making a string that prints the character in front of it
convert = temp2.repeat(number); //it will print the character however many times that number was stored as



return convert;



Also I have not yet learned how to use arrays, that is why I am not using an array.







java parseint charat






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 7 at 3:14









user85352user85352

124




124












  • It seems that you are using an undefined method here -> temp2.repeat(number);

    – Mark
    Mar 7 at 3:19






  • 1





    When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

    – user85352
    Mar 7 at 3:21











  • Can you post the link where you found the String.repeat() method?

    – Mark
    Mar 7 at 3:23











  • w3schools.com/jsref/jsref_repeat.asp

    – user85352
    Mar 7 at 3:26











  • The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

    – Mark
    Mar 7 at 3:31

















  • It seems that you are using an undefined method here -> temp2.repeat(number);

    – Mark
    Mar 7 at 3:19






  • 1





    When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

    – user85352
    Mar 7 at 3:21











  • Can you post the link where you found the String.repeat() method?

    – Mark
    Mar 7 at 3:23











  • w3schools.com/jsref/jsref_repeat.asp

    – user85352
    Mar 7 at 3:26











  • The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

    – Mark
    Mar 7 at 3:31
















It seems that you are using an undefined method here -> temp2.repeat(number);

– Mark
Mar 7 at 3:19





It seems that you are using an undefined method here -> temp2.repeat(number);

– Mark
Mar 7 at 3:19




1




1





When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

– user85352
Mar 7 at 3:21





When I looked up the repeat method it was setup to be String.repeat(int). I think its undefined because number isn't just 1 number

– user85352
Mar 7 at 3:21













Can you post the link where you found the String.repeat() method?

– Mark
Mar 7 at 3:23





Can you post the link where you found the String.repeat() method?

– Mark
Mar 7 at 3:23













w3schools.com/jsref/jsref_repeat.asp

– user85352
Mar 7 at 3:26





w3schools.com/jsref/jsref_repeat.asp

– user85352
Mar 7 at 3:26













The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

– Mark
Mar 7 at 3:31





The one you posted is a JavaScript method. You are using Java. Do not confuse JavaScript with Java or vice versa

– Mark
Mar 7 at 3:31












2 Answers
2






active

oldest

votes


















1














Edited to:

- accommodate strings that has length more then 1. Example: 10AA

- accommodate input that starts with a string. Example: A5o



To solve this you have to get all the simultaneous digits, example if you have "55s", you have to get "55", That is why your code is incorrect since if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous digits first and only parseInt when you encounter the first non digit.



Refer to the code and comments for details:



public class Main 
public static void main(String[] args)
System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));


public static String runLengthDecoding(String str3)
String convert = "";
int number = 0;
String numberString = "";
String toBeRepeatedString = "";
boolean flag = false;
for (int i = 0; i <= str3.length() - 1; i++)
char currentChar = str3.charAt(i);
if (Character.isDigit(currentChar)) // true or false, the current character is a digit
numberString = numberString + currentChar; // store the possible integer
else
if (i + 1 < str3.length())
char nextChar = str3.charAt(i + 1); // check if the next char is a digit
if (!Character.isDigit(nextChar)) else
flag = true;



if (flag)
toBeRepeatedString += currentChar;

// This will accomodate inputs "A3B";
if (!numberString.isEmpty())
number = Integer.parseInt(numberString); // parse the number of repeats
else
number = 1;


numberString = ""; // reset number

String temp2 = "";

// Repeat the currentChar
for (int j = 0; j < number; j++)
temp2 += toBeRepeatedString;


convert = convert + temp2; // store it to the result
toBeRepeatedString = ""; // reset toBeRepeatedString





return convert;





Result:



Input: 3A5o2n4t => Output : AAAooooonntttt
Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
Input: A5o => Output : Aooooo
Input: AB5o => Output : ABooooo





share|improve this answer

























  • Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

    – user85352
    Mar 7 at 3:51











  • You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

    – Mark
    Mar 7 at 3:54












  • Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

    – user85352
    Mar 7 at 4:23












  • I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

    – Mark
    Mar 7 at 4:25












  • I have edited it. Kindly check

    – Mark
    Mar 7 at 4:38


















0














Here is the best way of doing the above problem it will handle all your scenarios:



public static void main(String[] args) 
String input = "5a2s3T66e";
System.out.println("Input is: "+input+" and output is: "+expandingCondenseString(input));

private static String expandingCondenseString(String input)
StringBuilder result = new StringBuilder();
String size = "";
String value = "";
for (int i=0;i<input.length();i++)
if (Character.isDigit(input.charAt(i)))
size = size + input.charAt(i);
else
value = value + input.charAt(i);
if(i+1<input.length() && !Character.isDigit(input.charAt(i+1)))
continue;

if(size.isEmpty())
size = "1";

for (int j=0;j<Integer.parseInt(size);j++)
result.append(value);

size = "";
value = "";


return String.valueOf(result);






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035500%2fexpanding-a-condensed-string%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Edited to:

    - accommodate strings that has length more then 1. Example: 10AA

    - accommodate input that starts with a string. Example: A5o



    To solve this you have to get all the simultaneous digits, example if you have "55s", you have to get "55", That is why your code is incorrect since if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous digits first and only parseInt when you encounter the first non digit.



    Refer to the code and comments for details:



    public class Main 
    public static void main(String[] args)
    System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
    System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
    System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
    System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
    System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
    System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));


    public static String runLengthDecoding(String str3)
    String convert = "";
    int number = 0;
    String numberString = "";
    String toBeRepeatedString = "";
    boolean flag = false;
    for (int i = 0; i <= str3.length() - 1; i++)
    char currentChar = str3.charAt(i);
    if (Character.isDigit(currentChar)) // true or false, the current character is a digit
    numberString = numberString + currentChar; // store the possible integer
    else
    if (i + 1 < str3.length())
    char nextChar = str3.charAt(i + 1); // check if the next char is a digit
    if (!Character.isDigit(nextChar)) else
    flag = true;



    if (flag)
    toBeRepeatedString += currentChar;

    // This will accomodate inputs "A3B";
    if (!numberString.isEmpty())
    number = Integer.parseInt(numberString); // parse the number of repeats
    else
    number = 1;


    numberString = ""; // reset number

    String temp2 = "";

    // Repeat the currentChar
    for (int j = 0; j < number; j++)
    temp2 += toBeRepeatedString;


    convert = convert + temp2; // store it to the result
    toBeRepeatedString = ""; // reset toBeRepeatedString





    return convert;





    Result:



    Input: 3A5o2n4t => Output : AAAooooonntttt
    Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
    Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
    Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
    Input: A5o => Output : Aooooo
    Input: AB5o => Output : ABooooo





    share|improve this answer

























    • Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

      – user85352
      Mar 7 at 3:51











    • You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

      – Mark
      Mar 7 at 3:54












    • Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

      – user85352
      Mar 7 at 4:23












    • I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

      – Mark
      Mar 7 at 4:25












    • I have edited it. Kindly check

      – Mark
      Mar 7 at 4:38















    1














    Edited to:

    - accommodate strings that has length more then 1. Example: 10AA

    - accommodate input that starts with a string. Example: A5o



    To solve this you have to get all the simultaneous digits, example if you have "55s", you have to get "55", That is why your code is incorrect since if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous digits first and only parseInt when you encounter the first non digit.



    Refer to the code and comments for details:



    public class Main 
    public static void main(String[] args)
    System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
    System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
    System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
    System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
    System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
    System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));


    public static String runLengthDecoding(String str3)
    String convert = "";
    int number = 0;
    String numberString = "";
    String toBeRepeatedString = "";
    boolean flag = false;
    for (int i = 0; i <= str3.length() - 1; i++)
    char currentChar = str3.charAt(i);
    if (Character.isDigit(currentChar)) // true or false, the current character is a digit
    numberString = numberString + currentChar; // store the possible integer
    else
    if (i + 1 < str3.length())
    char nextChar = str3.charAt(i + 1); // check if the next char is a digit
    if (!Character.isDigit(nextChar)) else
    flag = true;



    if (flag)
    toBeRepeatedString += currentChar;

    // This will accomodate inputs "A3B";
    if (!numberString.isEmpty())
    number = Integer.parseInt(numberString); // parse the number of repeats
    else
    number = 1;


    numberString = ""; // reset number

    String temp2 = "";

    // Repeat the currentChar
    for (int j = 0; j < number; j++)
    temp2 += toBeRepeatedString;


    convert = convert + temp2; // store it to the result
    toBeRepeatedString = ""; // reset toBeRepeatedString





    return convert;





    Result:



    Input: 3A5o2n4t => Output : AAAooooonntttt
    Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
    Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
    Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
    Input: A5o => Output : Aooooo
    Input: AB5o => Output : ABooooo





    share|improve this answer

























    • Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

      – user85352
      Mar 7 at 3:51











    • You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

      – Mark
      Mar 7 at 3:54












    • Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

      – user85352
      Mar 7 at 4:23












    • I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

      – Mark
      Mar 7 at 4:25












    • I have edited it. Kindly check

      – Mark
      Mar 7 at 4:38













    1












    1








    1







    Edited to:

    - accommodate strings that has length more then 1. Example: 10AA

    - accommodate input that starts with a string. Example: A5o



    To solve this you have to get all the simultaneous digits, example if you have "55s", you have to get "55", That is why your code is incorrect since if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous digits first and only parseInt when you encounter the first non digit.



    Refer to the code and comments for details:



    public class Main 
    public static void main(String[] args)
    System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
    System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
    System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
    System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
    System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
    System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));


    public static String runLengthDecoding(String str3)
    String convert = "";
    int number = 0;
    String numberString = "";
    String toBeRepeatedString = "";
    boolean flag = false;
    for (int i = 0; i <= str3.length() - 1; i++)
    char currentChar = str3.charAt(i);
    if (Character.isDigit(currentChar)) // true or false, the current character is a digit
    numberString = numberString + currentChar; // store the possible integer
    else
    if (i + 1 < str3.length())
    char nextChar = str3.charAt(i + 1); // check if the next char is a digit
    if (!Character.isDigit(nextChar)) else
    flag = true;



    if (flag)
    toBeRepeatedString += currentChar;

    // This will accomodate inputs "A3B";
    if (!numberString.isEmpty())
    number = Integer.parseInt(numberString); // parse the number of repeats
    else
    number = 1;


    numberString = ""; // reset number

    String temp2 = "";

    // Repeat the currentChar
    for (int j = 0; j < number; j++)
    temp2 += toBeRepeatedString;


    convert = convert + temp2; // store it to the result
    toBeRepeatedString = ""; // reset toBeRepeatedString





    return convert;





    Result:



    Input: 3A5o2n4t => Output : AAAooooonntttt
    Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
    Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
    Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
    Input: A5o => Output : Aooooo
    Input: AB5o => Output : ABooooo





    share|improve this answer















    Edited to:

    - accommodate strings that has length more then 1. Example: 10AA

    - accommodate input that starts with a string. Example: A5o



    To solve this you have to get all the simultaneous digits, example if you have "55s", you have to get "55", That is why your code is incorrect since if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous digits first and only parseInt when you encounter the first non digit.



    Refer to the code and comments for details:



    public class Main 
    public static void main(String[] args)
    System.out.println("Input: 3A5o2n4t => Output : " + runLengthDecoding("3A5o2n4t"));
    System.out.println("Input: 3AA5o2n4t => Output : " + runLengthDecoding("3AA5o2n4t"));
    System.out.println("Input: 10A5o2n4t => Output : " + runLengthDecoding("10A5o2n4t"));
    System.out.println("Input: 10AA5o2n4t => Output : " + runLengthDecoding("10AA5o2n4t"));
    System.out.println("Input: A5o => Output : " + runLengthDecoding("A5o"));
    System.out.println("Input: AB5o => Output : " + runLengthDecoding("AB5o"));


    public static String runLengthDecoding(String str3)
    String convert = "";
    int number = 0;
    String numberString = "";
    String toBeRepeatedString = "";
    boolean flag = false;
    for (int i = 0; i <= str3.length() - 1; i++)
    char currentChar = str3.charAt(i);
    if (Character.isDigit(currentChar)) // true or false, the current character is a digit
    numberString = numberString + currentChar; // store the possible integer
    else
    if (i + 1 < str3.length())
    char nextChar = str3.charAt(i + 1); // check if the next char is a digit
    if (!Character.isDigit(nextChar)) else
    flag = true;



    if (flag)
    toBeRepeatedString += currentChar;

    // This will accomodate inputs "A3B";
    if (!numberString.isEmpty())
    number = Integer.parseInt(numberString); // parse the number of repeats
    else
    number = 1;


    numberString = ""; // reset number

    String temp2 = "";

    // Repeat the currentChar
    for (int j = 0; j < number; j++)
    temp2 += toBeRepeatedString;


    convert = convert + temp2; // store it to the result
    toBeRepeatedString = ""; // reset toBeRepeatedString





    return convert;





    Result:



    Input: 3A5o2n4t => Output : AAAooooonntttt
    Input: 3AA5o2n4t => Output : AAAAAAooooonntttt
    Input: 10A5o2n4t => Output : AAAAAAAAAAooooonntttt
    Input: 10AA5o2n4t => Output : AAAAAAAAAAAAAAAAAAAAooooonntttt
    Input: A5o => Output : Aooooo
    Input: AB5o => Output : ABooooo






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 7 at 5:10

























    answered Mar 7 at 3:28









    MarkMark

    871118




    871118












    • Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

      – user85352
      Mar 7 at 3:51











    • You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

      – Mark
      Mar 7 at 3:54












    • Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

      – user85352
      Mar 7 at 4:23












    • I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

      – Mark
      Mar 7 at 4:25












    • I have edited it. Kindly check

      – Mark
      Mar 7 at 4:38

















    • Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

      – user85352
      Mar 7 at 3:51











    • You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

      – Mark
      Mar 7 at 3:54












    • Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

      – user85352
      Mar 7 at 4:23












    • I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

      – Mark
      Mar 7 at 4:25












    • I have edited it. Kindly check

      – Mark
      Mar 7 at 4:38
















    Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

    – user85352
    Mar 7 at 3:51





    Wouldn't I want to do the parseInt if the true or false statement is true? Also isn't the numberString just an empty string in the else statement?

    – user85352
    Mar 7 at 3:51













    You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

    – Mark
    Mar 7 at 3:54






    You have to get all the simultaneous ints, example if you have "55s" and if you parseInt whenever you see a digit then it will immediately parse into 5, but the actual number is 55, thus you should accumulate the simultaneous ints and only parse when you encounter a non digit. The numberString will be the holder of those possible digits, you are appending those digits whenever you encounter a digit and if you encounter a non digit then you will first convert it to int then reset numberString to empty ("") so that it can hold the next possible digits

    – Mark
    Mar 7 at 3:54














    Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

    – user85352
    Mar 7 at 4:23






    Okay that makes sense, thank you. I was able to fix my code so that it works with your help. However, I noticed that if i put the number the character appears after the character it returns an error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "" " Do you know why that is? (so instead of 3A5o2n4t I do A3o5n2t4)

    – user85352
    Mar 7 at 4:23














    I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

    – Mark
    Mar 7 at 4:25






    I have edited my answer, kindly check. I haven't included that test case. I will edit my code. Kindly wait

    – Mark
    Mar 7 at 4:25














    I have edited it. Kindly check

    – Mark
    Mar 7 at 4:38





    I have edited it. Kindly check

    – Mark
    Mar 7 at 4:38













    0














    Here is the best way of doing the above problem it will handle all your scenarios:



    public static void main(String[] args) 
    String input = "5a2s3T66e";
    System.out.println("Input is: "+input+" and output is: "+expandingCondenseString(input));

    private static String expandingCondenseString(String input)
    StringBuilder result = new StringBuilder();
    String size = "";
    String value = "";
    for (int i=0;i<input.length();i++)
    if (Character.isDigit(input.charAt(i)))
    size = size + input.charAt(i);
    else
    value = value + input.charAt(i);
    if(i+1<input.length() && !Character.isDigit(input.charAt(i+1)))
    continue;

    if(size.isEmpty())
    size = "1";

    for (int j=0;j<Integer.parseInt(size);j++)
    result.append(value);

    size = "";
    value = "";


    return String.valueOf(result);






    share|improve this answer





























      0














      Here is the best way of doing the above problem it will handle all your scenarios:



      public static void main(String[] args) 
      String input = "5a2s3T66e";
      System.out.println("Input is: "+input+" and output is: "+expandingCondenseString(input));

      private static String expandingCondenseString(String input)
      StringBuilder result = new StringBuilder();
      String size = "";
      String value = "";
      for (int i=0;i<input.length();i++)
      if (Character.isDigit(input.charAt(i)))
      size = size + input.charAt(i);
      else
      value = value + input.charAt(i);
      if(i+1<input.length() && !Character.isDigit(input.charAt(i+1)))
      continue;

      if(size.isEmpty())
      size = "1";

      for (int j=0;j<Integer.parseInt(size);j++)
      result.append(value);

      size = "";
      value = "";


      return String.valueOf(result);






      share|improve this answer



























        0












        0








        0







        Here is the best way of doing the above problem it will handle all your scenarios:



        public static void main(String[] args) 
        String input = "5a2s3T66e";
        System.out.println("Input is: "+input+" and output is: "+expandingCondenseString(input));

        private static String expandingCondenseString(String input)
        StringBuilder result = new StringBuilder();
        String size = "";
        String value = "";
        for (int i=0;i<input.length();i++)
        if (Character.isDigit(input.charAt(i)))
        size = size + input.charAt(i);
        else
        value = value + input.charAt(i);
        if(i+1<input.length() && !Character.isDigit(input.charAt(i+1)))
        continue;

        if(size.isEmpty())
        size = "1";

        for (int j=0;j<Integer.parseInt(size);j++)
        result.append(value);

        size = "";
        value = "";


        return String.valueOf(result);






        share|improve this answer















        Here is the best way of doing the above problem it will handle all your scenarios:



        public static void main(String[] args) 
        String input = "5a2s3T66e";
        System.out.println("Input is: "+input+" and output is: "+expandingCondenseString(input));

        private static String expandingCondenseString(String input)
        StringBuilder result = new StringBuilder();
        String size = "";
        String value = "";
        for (int i=0;i<input.length();i++)
        if (Character.isDigit(input.charAt(i)))
        size = size + input.charAt(i);
        else
        value = value + input.charAt(i);
        if(i+1<input.length() && !Character.isDigit(input.charAt(i+1)))
        continue;

        if(size.isEmpty())
        size = "1";

        for (int j=0;j<Integer.parseInt(size);j++)
        result.append(value);

        size = "";
        value = "";


        return String.valueOf(result);







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 7 at 4:42

























        answered Mar 7 at 4:33









        Ammar AliAmmar Ali

        444314




        444314



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55035500%2fexpanding-a-condensed-string%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко