Arivazhagan
Arivazhagan

Reputation: 53

How to print sequence of characters according to the number next to it?

Input: a2b4c3
Output: aabbbbccc

Here is my code.........which has wrong output:

import java.lang.*;
import java.util.ArrayList;

class hi
{
public static void main(String[] args)
{
String a="a2b4c3";
String alpha="";
ArrayList<Character> num=new ArrayList<Character>();
for(int i=0;i<a.length();i++)
{
  char c=a.charAt(i);
  if(Character.isDigit(c)==true)
   {
     num.add(c);
   }
  else
   {
    alpha+=c;
   }
}
for(int y=0; y<num.size(); y++)
 {
   for(int z=0; z<num.get(y); z++)
    { 
      System.out.print(alpha.charAt(y));
    }
  }
}
}

Whats wrong in this code, I didn't get the output as expected

My output is: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb ccccccccccccccccccccccccccccccccccccccccccccccccccc

Need corrections or code in java

Upvotes: 0

Views: 734

Answers (6)

saipavanadiraj
saipavanadiraj

Reputation: 1

public static String pringChars(String a)
{
    Character myChar = null;
    String finalString = "";
    int value = 0;
    for (int i = 0; i < a.length(); i++) {
        char temp = a.charAt(i);
        if (Character.isDigit(temp) == true) {
            int k = Character.getNumericValue(temp);
            for (int j = 0; j < k; j++) {
                finalString += myChar;
            }
            myChar = null;
        }
        else {
            myChar = temp;
        }
    }
    return finalString;
}

Upvotes: -1

WJS
WJS

Reputation: 40047

Did you consider this?

String input = "a2b4c3";

for(String group : input.split("(?<=\\d++)")) {          
   System.out.print(group.substring(0,1).repeat(Integer.parseInt(group.substring(1))));
}

prints

aabbbbccc

It works as follows:

  • (?<=\\d++) says split at locations following a group of digits
  • then just use substring combined with the repeat method to process the character and convert the number to an int to print the result.

Upvotes: 1

vigneshwaran m
vigneshwaran m

Reputation: 376

Hi Here your answer.

import java.lang.*;
import java.util.ArrayList;

class Hi
{
    public static void main(String[] args)
    {
        String a="a2b4c3";
        String alpha="";
        ArrayList<Character> num=new ArrayList<Character>();
        for(int i=0;i<a.length();i++)
        {
            char c=a.charAt(i);
            if(Character.isDigit(c)==true)
            {
                num.add(c);
            }
            else
            {
                alpha+=c;
            }
        }
        for(int y=0; y<num.size(); y++)
        {
            int count=Integer.parseInt(String.valueOf(num.get(y)));

            for(int z=0; z<count; z++)
            {
                System.out.print(alpha.charAt(y));
            }
        }
    }
}

Or

import java.lang.*; import java.util.ArrayList;

class Hi
{
    public static void main(String[] args)
    {
        String a="a2b4c3";
        String alpha="";
        ArrayList<Integer> num=new ArrayList<>();
        for(int i=0;i<a.length();i++)
        {
            char c=a.charAt(i);
            if(Character.isDigit(c)==true)
            {
                num.add(Integer.parseInt(String.valueOf(c)));
            }
            else
            {
                alpha+=c;
            }
        }
        for(int y=0; y<num.size(); y++)
        {
            for(int z=0; z<num.get(y); z++)
            {
                System.out.print(alpha.charAt(y));
            }
        }
    }
}

Upvotes: -1

Charul
Charul

Reputation: 252

Change your for loop as this:


for(int y=0; y<num.size(); y++)
 {
    for(int z=0; z<Integer.parseInt(String.valueOf(num.get(y))); z++)
      {
        System.out.print(alpha.charAt(y));
      }
 }

Or better try this approach:

class hi
{
    public static void main(String[] args)
    {
        String a="a2b4c3";
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=0;i<a.length()-1;i=i+2)
        {
            int value=Integer.parseInt(String.valueOf(a.charAt(i+1)));
            for (int j=0;j<value;j++){
                stringBuilder.append(a.charAt(i));
            }
        }
        System.out.println(stringBuilder.toString());
    }
}

Upvotes: 1

user4910279
user4910279

Reputation:

Try this.

String input = "a2b4c3";
Pattern pat = Pattern.compile("(?i)([a-z])(\\d+)");
String output = pat.matcher(input).replaceAll(m -> m.group(1).repeat(Integer.valueOf(m.group(2))));
System.out.println(output);

Upvotes: -1

luk2302
luk2302

Reputation: 57174

The problem is that '2' has the integer value 50, see http://www.asciitable.com/ . That is why you get 50 * a printed.

Solution: subtract '0' (which has value 48), e.g. before adding it to the list: num.add(c - '0');. Not necessary but makes the intention cleaner: you should then change num to be an ArrayList<Integer> instead.

Upvotes: 1

Related Questions