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.
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.