How to count the characters of each line in a string and convert the number to a hex

152 Views Asked by At

I have this String here called message.

Bruce Wayne,Batman,None,Gotham City,Robin,The Joker
Oliver Queen,Green Arrow,None,Star City,Speedy,Deathstroke
Clark Kent,Superman,Flight,Metropolis,None,Lex Luthor
Bart Allen,The Flash,Speed,Central City,Kid Flash,Professor Zoom

I need to count the number of characters in each line and print them in hex.

  • First line should be (From Bruce to Joker) 2b
  • Second line should be (From Oliver to Deathstroke) 32
  • Third line should be (From Clark to Luthor) 2e
  • fourth line should be (From Bart to Zoom) 36
package com.raghav.conversion;

import java.util.ArrayList;
public class StringToHex {
public static void Main(String[] args)   {  
    String message =    "Bruce Wayne,Batman,None,Gotham City,Robin,The Joker" +
                        "Oliver Queen,Green Arrow,None,Star City,Speedy,Deathstroke" +
                        "Clark Kent,Superman,Flight,Metropolis,None,Lex Luthor" + 
                        "Bart Allen,The Flash,Speed,Central City,Kid Flash,Professor Zoom";


    ArrayList<String> lines = new ArrayList<String>();
    ArrayList<Integer> chars = new ArrayList<Integer>();
    ArrayList<String> hex = new ArrayList<String>();

    String line = "";
    int c = 0;

    for(int i = 0; i < message.length(); i++) {
        lines.add(message.charAt(i), line);
        line = line.replaceAll(",", "".replaceAll(" ", ""));
        c = line.length();
        chars.add(c);
        hex.add(Integer.toHexString(c));
    }

    for (int i = 0; i < hex.size(); i++) {
        String padded = "00000000".substring(hex.size()) + hex.get(i);
        System.out.println(padded);
    }
}

}

This is what I have so far but I get an ArrayIndexOutOfBoundsException at Line 22 lines.add(message.charAt(i), line);

Can someone help me out? Thanks.

2

There are 2 best solutions below

0
On BEST ANSWER
String msg = "Bruce Wayne,Batman,None,Gotham City,Robin,The Joker\n" +
         "Oliver Queen,Green Arrow,None,Star City,Speedy,Deathstroke\n" +
         "Clark Kent,Superman,Flight,Metropolis,None,Lex Luthor\n" + 
         "Bart Allen,The Flash,Speed,Central City,Kid Flash,Professor Zoom";

String[] lines = msg.split("\n");
for(Integer i = 1; i <= lines.length; i++){
    int numChars = 0;
    String[] toks = lines[i - 1].split(",");
    for(String tok : toks){
        numChars += tok.replaceAll(" ", "").length();
    }
    System.out.println("Line " + i.toString() + " beginning with " + 
            toks[0] + " and ending with " + toks[toks.length - 1] + 
            " contains " + Integer.toHexString(numChars) + 
            " characters." );
}

This will do what you're looking for. Notice the \n at the end of each line, that makes them lines. Not sure what your second loop was for.

0
On

Turn

String message = "Bruce Wayne,Batman,None,Gotham City,Robin,The Joker" + "Oliver Queen,Green Arrow,None,Star City,Speedy,Deathstroke" + "Clark Kent,Superman,Flight,Metropolis,None,Lex Luthor" + "Bart Allen,The Flash,Speed,Central City,Kid Flash,Professor Zoom";

to

List<String> message = new ArrayList<>(); message.add("Bruce Wayne,Batman,None,Gotham City,Robin,The Joker"); .....

and

for(int i = 0; i < message.length(); i++) {

to

for(String line : message){

then use line for the counting stuff....