简体   繁体   English

如何计算字符串中每一行的字符并将数字转换为十六进制

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

I have this String here called message. 我在这里将此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

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 第一行应该是(从布鲁斯到小丑)2b
  • Second line should be (From Oliver to Deathstroke) 32 第二行应该是(从Oliver到Deathstroke)32
  • Third line should be (From Clark to Luthor) 2e 第三行应该是(从克拉克到卢瑟)2e
  • fourth line should be (From Bart to Zoom) 36 第四行应该是(从Bart到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); 到目前为止,这是我所拥有的,但是在第22行lines.add(message.charAt(i), line);得到了ArrayIndexOutOfBoundsException lines.add(message.charAt(i), line);

Can someone help me out? 有人可以帮我吗? Thanks. 谢谢。

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. 注意每行末尾的\\ n,使它们成为行。 Not sure what your second loop was for. 不确定您的第二个循环是干什么的。

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.... 然后用line来计数东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM