java将字母转换为数字然后将所有值放入数组中

时间:2014-04-17 08:45:59

标签: java arrays

我试图找出将字母转换为数字的方法,然后根据n位数将其拆分。

例如,我有话:TOMORROW BEGIN

然后,我将其转换为ASCII格式的数字。因此,转换号码必须为:8479777982827987326669717378

之后,所有数字都将转换为n位数,并将其放在一维数组中。

让我们说每个街区的数字分成3位数。结果必须为:847 977 798 282 798 732 666 971 737 8

他们全部在阵列中:arr[0] = 847, arr[1] = 977, arr[2] = 798,....arr[n]

我已经尝试在java中解决它。但到目前为止,我可以将字符串转换为数字。

以下是我尝试过的代码的一部分:

    String words = "TOMORROW BEGIN"; 
    int sa;
    char c; 
    for(int i = 0; i < words.length(); i++){ 
      c = words.charAt(i); 
      sa = (int) c;
      String kt = Integer.toString(sa);
      System.out.print(kt);  
    }

好吧,它已成功转换为数字:8479777982827987326669717378

但是,我混淆了如何将数字分成n个数字的块(让每个块有3个数字),然后将所有块的值放入数组中。

我该怎么做才能解决这个问题?

6 个答案:

答案 0 :(得分:2)

一种解决方案是每三个字符使用substring并使用Integer.parseInt()转换每个子字符串。

public class Main {
public static void main(String[] args) {
    String input = "8479777982827987326669717378";
    int n = 3;
    int[] output = new int[(input.length() + n - 1) / n];
    for (int i = 0; i < output.length; i++) {
        output[i] = Integer.parseInt(input.substring(i*n, Math.min((i+1)*n, input.length())));
       // System.out.println(output[i]);
    }
}
}

答案 1 :(得分:2)

您可以创建此类方法:

public static int[] splitString(String str, int length){

    int size=str.length()/length;
    if( str.length() % length != 0)
       size++;

    int[] arr = new int[size];
    int a=0;
    for (int i= 0; i < str.length(); i+=length) {
        if(i+length < str.length())
            arr[a++]=Integer.parseInt(str.substring(i, i+length));
        else  
            arr[a++]=Integer.parseInt(str.substring(i,  str.length()));
    }
    return arr;
}

在您的主要方法中,您可以致电:

String str = "8479777982827987326669717378";
System.out.println(Arrays.toString(splitString(str,3)));

输出:

[847, 977, 798, 282, 798, 732, 666, 971, 737, 8]

您甚至可以指定将2位数int存储到数组

System.out.println(Arrays.toString(splitString(str, 2)));

输出:

[84, 79, 77, 79, 82, 82, 79, 87, 32, 66, 69, 71, 73, 78]

答案 2 :(得分:1)

您可以使用substring方法分割字符串

例如:

  for (int start = 0; start < kt.length(); start += size) {

           system.out.println(kt.substring(start, Math.min(kt.length(), start + size)))
  }

答案 3 :(得分:1)

你可以这样试试:

public static void main(String[] args) {
    String words = "TOMORROW BEGIN";
    StringBuilder builder = new StringBuilder();
    for (int idx = 0; idx < words.length(); idx++) {
      builder.append((int) words.charAt(idx));
    }
    int[] ints = new int[(builder.length() + 2) / 3];
    for (int idx = 0; idx < ints.length; idx++) {
        int from = idx * 3;
        int to = Math.min(from + 3, builder.length());
        ints[idx] = Integer.parseInt(builder.substring(from, to));
    }
    System.out.println(builder);
    System.out.println(Arrays.toString(ints));
}

输出:

8479777982827987326669717378
[847, 977, 798, 282, 798, 732, 666, 971, 737, 8]

答案 4 :(得分:1)

将ASCII连接到String或StringBuffer,然后遍历它。

根据您的代码:

String words = "TOMORROW BEGIN"; 
int sa;
char c; 

StringBuffer asciiBuffer = new StringBuffer();
for(int i = 0; i < words.length(); i++){ 
    c = words.charAt(i); 
    sa = (int) c;
    asciiBuffer.append(Integer.toString(sa));
}

System.out.println("\nBuffer: " + asciiBuffer);

int from = 0;
int arrayStringLength = 3;
int bufferLength = asciiBuffer.length();
int arraySize = bufferLength / arrayStringLength + 1;
CharSequence[] result = new CharSequence[arraySize];
int i=0;

while(from < bufferLength){
    result[i] = asciiBuffer.subSequence(from, Math.min(from+arrayStringLength, bufferLength));
    from = from+arrayStringLength;
    i++;
}
System.out.println(Arrays.toString(result));

输出:

  

缓冲区:8479777982827987326669717378

     

[847,977,798,282,798,732,666,971,737,8]

请参阅this ideone

编辑:与整数数组中的其他答案存储结果相比,在CharSequence数组中将保留0。整数解决方案的潜在问题示例,以“TPMORROW BFGIN”作为条目:

  

缓冲区:8480777982827987326670717378

     

[848,077,798,282,798,732,667,071,737,8]

答案 5 :(得分:1)

我使用了你的代码并添加了几行代码。这应该有效:

public static void main(String[] args) {

      String words = "TOMORROW BEGIN"; 
        int sa;
        int n = 3; // put any value for n
        char c; 
        int[] array;
        String kt = "";
        for(int i = 0; i < words.length(); i++){ 
          c = words.charAt(i); 
          sa = (int) c;
          kt += sa;
        }
        System.out.println(kt);
        int nbPartitions = kt.length() / n + kt.length() % n;
        array = new int[nbPartitions];

        for(int i = 0; i < nbPartitions; i++){
            int begin = i * n;
            int end = (i + 1) * n;
            if(end > kt.length()){
                array[i] = Integer.parseInt(kt.substring(begin, kt.length()));
                break;
            }
            array[i] = Integer.parseInt(kt.substring(begin, end));
        }
        System.out.println(Arrays.toString(array));
}