如何在java中连接int值?

时间:2021-04-01 12:02:11

I have the following values:

我有以下价值观:

int a=1; 
int b=0;
int c=2;
int d=2;
int e=1;

How do i concatenate these values so that i end up with a String that is 10221; please note that multiplying a by 10000, b by 1000.....and e by 1 will not working since b=0 and therefore i will lose it when i add the values up.

如何将这些值连接起来,最后得到一个10221的字符串;请注意,a乘以10000,b乘以1000。从b=0开始,e除以1,所以当我把值加起来时,我就失去了它。

20 个解决方案

#1


35  

The easiest (but somewhat dirty) way:

最简单(但有点脏)的方法:

String result = "" + a + b + c + d + e

Edit: I don't recommend this and agree with Jon's comment. Adding those extra empty strings is probably the best compromise between shortness and clarity.

编辑:我不推荐这个,同意Jon的评论。添加这些额外的空字符串可能是在短和清晰之间最好的折衷。

#2


29  

Michael Borgwardt's solution is the best for 5 digits, but if you have variable number of digits, you can use something like this:

Michael Borgwardt的解决方案是最好的5位数字,但是如果你的数字是可变的,你可以使用如下的方法:

public static String concatenateDigits(int... digits) {
   StringBuilder sb = new StringBuilder(digits.length);
   for (int digit : digits) {
     sb.append(digit);
   }
   return sb.toString();
}

#3


19  

This worked for me.

这为我工作。

int i = 14;
int j = 26;
int k = Integer.valueOf(String.valueOf(i) + String.valueOf(j));
System.out.println(k);

It turned out as 1426

结果是1426年。

#4


11  

Actually,

实际上,

int result = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
String s = Integer.toString(result);

will work.

将工作。

Note: this will only work when a is greater than 0 and all of b, c, d and e are in [0, 9]. For example, if b is 15, Michael's method will get you the result you probably want.

注意:只有当a大于0且所有的b、c、d和e都在[0,9]时,这才会起作用。例如,如果b是15,Michael的方法会得到你想要的结果。

#5


8  

just to not forget the format method

不要忘记格式方法。

String s = String.format("%s%s%s%s%s", a, b, c, d, e);

(%1.1s%1.1s%1.1s%1.1s%1.1s if you only want the first digit of each number...)

(如果你只需要每个数字的第一个数字……)

#6


4  

StringBuffer sb = new StringBuffer();
sb.append(a).append(b).append(c)...

Keeping the values as an int is preferred thou, as the other answers show you.

将这些值保持为整数,就像其他答案显示的那样。

#7


4  

Others have pointed out that multiplying b by 1000 shouldn't cause a problem - but if a were zero, you'd end up losing it. (You'd get a 4 digit string instead of 5.)

另一些人指出,b乘1000应该不会造成问题,但如果a是零,你最终会失去它。(你得到的是4位数而不是5。)

Here's an alternative (general purpose) approach - which assumes that all the values are in the range 0-9. (You should quite possibly put in some code to throw an exception if that turns out not to be true, but I've left it out here for simplicity.)

这里是另一种(通用的)方法——假定所有的值都在0-9范围内。(如果这不是真的,你应该在一些代码中添加一个异常,但是为了简单起见,我省略了它。)

public static String concatenateDigits(int... digits)
{
    char[] chars = new char[digits.length];
    for (int i = 0; i < digits.length; i++)
    {
        chars[i] = (char)(digits[i] + '0');
    }
    return new String(chars);
}

In this case you'd call it with:

在这种情况下,你可以这样称呼它:

String result = concatenateDigits(a, b, c, d, e);

#8


4  

If you multiply b by 1000, you will not lose any of the values. See below for the math.

如果用b乘以1000,就不会失去任何值。看下面的数学。

10000
    0
  200
   20
    1
=====
10221

#9


3  

For fun... how NOT to do it ;-)

为了好玩……如何不去做;-)

String s = Arrays.asList(a,b,c,d,e).toString().replaceAll("[\\[\\], ]", "");

Not that anyone would really think of doing it this way in this case - but this illustrates why it's important to give access to certain object members, otherwise API users end up parsing the string representation of your object, and then you're stuck not being able to modify it, or risk breaking their code if you do.

不,有人真的想这样做,在这种情况下,但这说明了为什么重要的是要给访问特定对象成员,否则API用户最终解析对象的字符串表示形式,然后你被困无法修改它,否则如果你打破他们的代码。

#10


2  

Assuming you start with variables:

假设你从变量开始:

int i=12;
int j=12;

This will give output 1212:

这将输出1212:

System.out.print(i+""+j); 

And this will give output 24:

这将给出输出24:

System.out.print(i+j);

#11


1  

I would suggest converting them to Strings.

我建议把它们转换成字符串。

StringBuilder concatenated = new StringBuilder();
concatenated.append(a);
concatenated.append(b);
/// etc...
concatenated.append(e);

Then converting back to an Integer:

然后转换回整数:

Integer.valueOf(concatenated.toString());

#12


1  

Use StringBuilder

使用StringBuilder

StringBuilder sb = new StringBuilder(String.valueOf(a));
sb.append(String.valueOf(b));
sb.append(String.valueOf(c));
sb.append(String.valueOf(d));
sb.append(String.valueOf(e));
System.out.print(sb.toString());

#13


1  

People were fretting over what happens when a == 0. Easy fix for that...have a digit before it. :)

人们对a == 0时的情况感到焦虑。容易解决……在它前面有一个数字。:)

int sum = 100000 + a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.valueOf(sum).substring(1));

Biggest drawback: it creates two strings. If that's a big deal, String.format could help.

最大的缺点:它创建了两个字符串。如果这是一件大事,细绳。格式可以帮助。

int sum = a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.format("%05d", sum));

#14


1  

You can Use

您可以使用

String x = a+"" +b +""+ c+""+d+""+ e;
int result = Integer.parseInt(x);

#15


1  

After Java 8 you can use the StringJoiner, a very clean and more flexible way (especially if you have a list as input instead of known set of variables a-e):

在Java 8之后,您可以使用StringJoiner,一种非常干净和更灵活的方法(特别是如果您有一个列表作为输入,而不是已知的变量a-e):

int a = 1;
int b = 0;
int c = 2;
int d = 2;
int e = 1;
List<Integer> values = Arrays.asList(a, b, c, d, e);
String result = values.stream().map(i -> i.toString()).collect(Collectors.joining());
System.out.println(result);

If you need a separator use:

如果您需要分隔符使用:

String result = values.stream().map(i -> i.toString()).collect(Collectors.joining(","));

To get the following result:

得到以下结果:

1,0,2,2,1

1 0、2、2、1

#16


0  

Best solutions are already discussed. For the heck of it, you could do this as well: Given that you are always dealing with 5 digits,

最好的解决方案已经讨论过了。对于它来说,你也可以这样做:假设你总是处理5位数字,

(new java.util.Formatter().format("%d%d%d%d%d", a,b,c,d,e)).toString()

I am not claiming this is the best way; just adding an alternate way to look at similar situations. :)

我不是说这是最好的方法;只是增加一种不同的方式来看待类似的情况。:)

#17


0  

How about not using strings at all...

如果不使用字符串呢…

This should work for any number of digits...

这应该适用于任意数量的数字……

int[] nums = {1, 0, 2, 2, 1};

int retval = 0;

for (int digit : nums)
{
    retval *= 10;
    retval += digit;
}

System.out.println("Return value is: " + retval);

#18


0  

Noting the discussion here and following along the general solution in the currently most widely accepted answer I would probably suggest the following alternative:

注意到这里的讨论,并遵循目前最普遍接受的答案,我可能提出以下备选方案:

 public static String concatenateDigits(int... digits) {
   String result = "";
   for (int digit : digits) {
     result += digit;
   }
   return result;
}

I would defer to my first link for the discussion of whether this is in fact compiled to the same byte-code as the original solution, but I do find it a bit more intuitive, quicker to read and less bloated to stick to language features rather than API calls. But that is a matter of opinion clearly.

我将遵从我的第一个链接,来讨论这是否实际上是与原始的解决方案相同的字节码编译的,但是我确实发现它更直观,更快速的阅读和更少的内容,以坚持语言特性,而不是API调用。但这显然是一个观点问题。

#19


0  

Couldn't you just make the numbers strings, concatenate them, and convert the strings to an integer value?

你不能只做数字串,连接它们,然后把字符串转换成整数值吗?

#20


-1  

public class joining {

    public static void main(String[] args) {
        int a=1; 
        int b=0;
        int c=2;
        int d=2;
        int e=1;

        String j = Long.toString(a);
        String k = Long.toString(b);
        String l = Long.toString(c);
        String m = Long.toString(d);
        String n = Long.toString(e);

       /* String s1=Long.toString(a);    // converting long to String
        String s2=Long.toString(b);
        String s3=s2+s1;
        long c=Long.valueOf(s3).longValue();    // converting String to long
        */

        System.out.println(j+k+l+m+n);  
    }
}

#1


35  

The easiest (but somewhat dirty) way:

最简单(但有点脏)的方法:

String result = "" + a + b + c + d + e

Edit: I don't recommend this and agree with Jon's comment. Adding those extra empty strings is probably the best compromise between shortness and clarity.

编辑:我不推荐这个,同意Jon的评论。添加这些额外的空字符串可能是在短和清晰之间最好的折衷。

#2


29  

Michael Borgwardt's solution is the best for 5 digits, but if you have variable number of digits, you can use something like this:

Michael Borgwardt的解决方案是最好的5位数字,但是如果你的数字是可变的,你可以使用如下的方法:

public static String concatenateDigits(int... digits) {
   StringBuilder sb = new StringBuilder(digits.length);
   for (int digit : digits) {
     sb.append(digit);
   }
   return sb.toString();
}

#3


19  

This worked for me.

这为我工作。

int i = 14;
int j = 26;
int k = Integer.valueOf(String.valueOf(i) + String.valueOf(j));
System.out.println(k);

It turned out as 1426

结果是1426年。

#4


11  

Actually,

实际上,

int result = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
String s = Integer.toString(result);

will work.

将工作。

Note: this will only work when a is greater than 0 and all of b, c, d and e are in [0, 9]. For example, if b is 15, Michael's method will get you the result you probably want.

注意:只有当a大于0且所有的b、c、d和e都在[0,9]时,这才会起作用。例如,如果b是15,Michael的方法会得到你想要的结果。

#5


8  

just to not forget the format method

不要忘记格式方法。

String s = String.format("%s%s%s%s%s", a, b, c, d, e);

(%1.1s%1.1s%1.1s%1.1s%1.1s if you only want the first digit of each number...)

(如果你只需要每个数字的第一个数字……)

#6


4  

StringBuffer sb = new StringBuffer();
sb.append(a).append(b).append(c)...

Keeping the values as an int is preferred thou, as the other answers show you.

将这些值保持为整数,就像其他答案显示的那样。

#7


4  

Others have pointed out that multiplying b by 1000 shouldn't cause a problem - but if a were zero, you'd end up losing it. (You'd get a 4 digit string instead of 5.)

另一些人指出,b乘1000应该不会造成问题,但如果a是零,你最终会失去它。(你得到的是4位数而不是5。)

Here's an alternative (general purpose) approach - which assumes that all the values are in the range 0-9. (You should quite possibly put in some code to throw an exception if that turns out not to be true, but I've left it out here for simplicity.)

这里是另一种(通用的)方法——假定所有的值都在0-9范围内。(如果这不是真的,你应该在一些代码中添加一个异常,但是为了简单起见,我省略了它。)

public static String concatenateDigits(int... digits)
{
    char[] chars = new char[digits.length];
    for (int i = 0; i < digits.length; i++)
    {
        chars[i] = (char)(digits[i] + '0');
    }
    return new String(chars);
}

In this case you'd call it with:

在这种情况下,你可以这样称呼它:

String result = concatenateDigits(a, b, c, d, e);

#8


4  

If you multiply b by 1000, you will not lose any of the values. See below for the math.

如果用b乘以1000,就不会失去任何值。看下面的数学。

10000
    0
  200
   20
    1
=====
10221

#9


3  

For fun... how NOT to do it ;-)

为了好玩……如何不去做;-)

String s = Arrays.asList(a,b,c,d,e).toString().replaceAll("[\\[\\], ]", "");

Not that anyone would really think of doing it this way in this case - but this illustrates why it's important to give access to certain object members, otherwise API users end up parsing the string representation of your object, and then you're stuck not being able to modify it, or risk breaking their code if you do.

不,有人真的想这样做,在这种情况下,但这说明了为什么重要的是要给访问特定对象成员,否则API用户最终解析对象的字符串表示形式,然后你被困无法修改它,否则如果你打破他们的代码。

#10


2  

Assuming you start with variables:

假设你从变量开始:

int i=12;
int j=12;

This will give output 1212:

这将输出1212:

System.out.print(i+""+j); 

And this will give output 24:

这将给出输出24:

System.out.print(i+j);

#11


1  

I would suggest converting them to Strings.

我建议把它们转换成字符串。

StringBuilder concatenated = new StringBuilder();
concatenated.append(a);
concatenated.append(b);
/// etc...
concatenated.append(e);

Then converting back to an Integer:

然后转换回整数:

Integer.valueOf(concatenated.toString());

#12


1  

Use StringBuilder

使用StringBuilder

StringBuilder sb = new StringBuilder(String.valueOf(a));
sb.append(String.valueOf(b));
sb.append(String.valueOf(c));
sb.append(String.valueOf(d));
sb.append(String.valueOf(e));
System.out.print(sb.toString());

#13


1  

People were fretting over what happens when a == 0. Easy fix for that...have a digit before it. :)

人们对a == 0时的情况感到焦虑。容易解决……在它前面有一个数字。:)

int sum = 100000 + a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.valueOf(sum).substring(1));

Biggest drawback: it creates two strings. If that's a big deal, String.format could help.

最大的缺点:它创建了两个字符串。如果这是一件大事,细绳。格式可以帮助。

int sum = a*10000 + b*1000 + c*100 + d*10 + e;
System.out.println(String.format("%05d", sum));

#14


1  

You can Use

您可以使用

String x = a+"" +b +""+ c+""+d+""+ e;
int result = Integer.parseInt(x);

#15


1  

After Java 8 you can use the StringJoiner, a very clean and more flexible way (especially if you have a list as input instead of known set of variables a-e):

在Java 8之后,您可以使用StringJoiner,一种非常干净和更灵活的方法(特别是如果您有一个列表作为输入,而不是已知的变量a-e):

int a = 1;
int b = 0;
int c = 2;
int d = 2;
int e = 1;
List<Integer> values = Arrays.asList(a, b, c, d, e);
String result = values.stream().map(i -> i.toString()).collect(Collectors.joining());
System.out.println(result);

If you need a separator use:

如果您需要分隔符使用:

String result = values.stream().map(i -> i.toString()).collect(Collectors.joining(","));

To get the following result:

得到以下结果:

1,0,2,2,1

1 0、2、2、1

#16


0  

Best solutions are already discussed. For the heck of it, you could do this as well: Given that you are always dealing with 5 digits,

最好的解决方案已经讨论过了。对于它来说,你也可以这样做:假设你总是处理5位数字,

(new java.util.Formatter().format("%d%d%d%d%d", a,b,c,d,e)).toString()

I am not claiming this is the best way; just adding an alternate way to look at similar situations. :)

我不是说这是最好的方法;只是增加一种不同的方式来看待类似的情况。:)

#17


0  

How about not using strings at all...

如果不使用字符串呢…

This should work for any number of digits...

这应该适用于任意数量的数字……

int[] nums = {1, 0, 2, 2, 1};

int retval = 0;

for (int digit : nums)
{
    retval *= 10;
    retval += digit;
}

System.out.println("Return value is: " + retval);

#18


0  

Noting the discussion here and following along the general solution in the currently most widely accepted answer I would probably suggest the following alternative:

注意到这里的讨论,并遵循目前最普遍接受的答案,我可能提出以下备选方案:

 public static String concatenateDigits(int... digits) {
   String result = "";
   for (int digit : digits) {
     result += digit;
   }
   return result;
}

I would defer to my first link for the discussion of whether this is in fact compiled to the same byte-code as the original solution, but I do find it a bit more intuitive, quicker to read and less bloated to stick to language features rather than API calls. But that is a matter of opinion clearly.

我将遵从我的第一个链接,来讨论这是否实际上是与原始的解决方案相同的字节码编译的,但是我确实发现它更直观,更快速的阅读和更少的内容,以坚持语言特性,而不是API调用。但这显然是一个观点问题。

#19


0  

Couldn't you just make the numbers strings, concatenate them, and convert the strings to an integer value?

你不能只做数字串,连接它们,然后把字符串转换成整数值吗?

#20


-1  

public class joining {

    public static void main(String[] args) {
        int a=1; 
        int b=0;
        int c=2;
        int d=2;
        int e=1;

        String j = Long.toString(a);
        String k = Long.toString(b);
        String l = Long.toString(c);
        String m = Long.toString(d);
        String n = Long.toString(e);

       /* String s1=Long.toString(a);    // converting long to String
        String s2=Long.toString(b);
        String s3=s2+s1;
        long c=Long.valueOf(s3).longValue();    // converting String to long
        */

        System.out.println(j+k+l+m+n);  
    }
}