在Java中把整数转换成字符串的8种方法

在本教程中,我们将探讨在Java中将整数转换为字符串的不同方法,并附有有趣的编程实例:

我们将涉及使用不同的Java类所提供的以下方法,在Java中把Int转换为String:

  • 字符串串联
  • String.valueOf()
  • String.format()
  • Integer.toString()
  • Integer.String(int)
  • 粘贴()的StringBuilder
  • StringBuffer append ()
  • 十进制格式(DecimalFormat)

我们将逐一详细了解这些方法。

在Java中把整数转换为字符串

在各种情况下,在开发任何应用程序或网站时,需要编写一个Java程序来将整数转换为字符串。

让我们考虑一下我们的Java程序中的一个场景,在对int变量进行某些算术运算后,收到的结果是一个整数值。 然而,这个值需要传递给网页上的一些文本字段或文本区域字段。 在这种情况下,需要首先将这个int值转换为String。

#1)使用字符串连接法

我们已经多次使用了Java加号'+'运算符。 这在使用System.out.println()方法在控制台打印任何输出时非常常用。

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用String连接 * * @作者 */ public class IntStringDemo1 { public static void main(String[] args) { // Assign int 25 to int variable length int length = 25; // Assign int 10 to int variable width int width = 10; // Multiply variable value length and width and assign tocalculatedArea int calculatedArea = length * width; // concatenate calculatedArea to String "Variable calculatedArea Value --->" using plus '+' // operator // print variable int type calculatedArea System.out.println("Variable calculatedArea Value ---> " + calculatedArea); } } 

下面是程序输出:

变量 calculatedArea 值 ->250

在上面的程序中,我们正在连接int 计算面积 与字符串 "变量计算面积值->",如下所示:

"变量calculatedArea值-> "+calculatedArea

这是将int计算的面积转换为String。 然后,这个字符串被作为参数传给System.Action。 出了 .println()方法,在控制台打印,如下所示:

系统。 出来了 .println("Variable calculatedArea Value -> "+ calculatedArea);

这将在控制台上打印出字符串:

变量 calculatedArea 值 ->250

#2)使用String.ValueOf()方法

String类有valueOf()的静态重载方法,这些重载方法的目的是将原始数据类型的参数如int, long, float转换为String数据类型。

让我们来看看下面的int数据类型的方法签名:

public static String valueOf(int i)

这个静态方法接收一个数据类型为int的参数,并返回int参数的字符串表示。

参数:

i:这是一个整数。

返回:

int参数的字符串表示。

让我们通过下面的示例程序来了解如何使用这个String.valueOf()方法。 在这个程序中,我们要将两个数字相加,并将使用String.valueOf()方法将整数sumValue转换成字符串。

下面是一个样本程序供你参考:

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序的示例代码 * 使用String.valueOf()方法 * * @作者 */ public class IntStringDemo2 { public static void main(String[] args) { // Assign int 300 to int variable x int x = 300; // Assign int 200 to int variable y int y = 200; // Add variable value x 和 y and assign to sumValue int sumValue = x+ y; //将sumValue作为参数传给String.valueOf(),将//sumValue转换为String sum = String.valueOf(sumValue); //打印变量字符串sum System.out.println("变量sum值-->" +sum); } } 

下面是程序输出:

变量总和值 ->500

#3)使用String.format()方法

字符串类有一个静态方法将参数转换为指定格式。

让我们看一下下面的方法签名:

public static String format(String format, Object... args)

这是一个字符串类的静态方法,使用指定的字符串格式和对象参数args,并返回格式化的字符串。 如果参数多于格式指定符,额外的参数会被忽略。 参数的数量是可变的,可能是零。

参数:

format: 格式字符串

args:需要按照格式化字符串格式化的参数。

返回:

按照指定的格式字符串格式化的一个字符串

投掷:

该方法会抛出IllegalFormatException、NullPointerException。

让我们了解一下这个String.format()方法的用途。

让我们看看比较2个整数的相同程序代码。 该程序将打印2个数字中较大的数字。 该程序使用String.format()方法来转换整数 大数 到字符串。

下面给出了一个示例程序:

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用String.format()方法 * * @作者 */ public class IntStringDemo3 { public static void main(String[] args) { // 将int 25分配给int变量a int a = 25; // 将int -50分配给int变量b int b = -50; // 比较两个数字a和b int largeNumber = 0; if(a> b) { // if a大于b,则将其分配给largeNumber变量 largeNumber = a; }else { //如果a小于b,则将b分配给largeNumber变量 largeNumber = b; } //将largeNumber作为参数传给String.format() ,将//largeNumber转换为String largeNumberString = String.format("--->" + largeNumberString); } } 

下面是程序输出:

变量 largeNumber 值 ->;

在上述示例代码中,我们使用了String.format()方法。 我们使用了"

String largeNumberString = String. 格式 ("

String.format()按照指定的格式返回格式化的字符串。 并在控制台打印出相同的内容。

系统。 出了 .println("variable largeNumber Value ->" + largeNumberString);

因此,控制台的输出是 变量 largeNumber 值 ->;

现在,让我们看看Integer类所提供的整数到字符串的Java转换方法。

我们将看到以下整数类方法:

      • Integer(int).toString()。
      • Integer.toString(int)

#4) 使用Integer(int).toString()方法

整数 类提供了一个方法 toString ()将int转换为String。

让我们看一下下面的方法签名:

public String toString()

该方法将数值转换为有符号的十进制表示,并返回一个String对象。

让我们了解一下这个的用途 整数(int).toString() 方法。

让我们看看计算两个数字的余数并在控制台打印的示例代码,使用Integer(int).toString()方法将整数 remainderValue 转换为其字符串表示。

这里是下面的示例程序:

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用Integer.toString()方法 * * @作者 */ public class IntStringDemo4 { public static void main(String[] args) { // 将int 33分配给int变量dividentValue int dividentValue = 33; // 将int 5分配给int变量 dividerValue int dividerValue = 5; // 计算剩余的使用模数的除数Value和除数Value int remainderValue = dividentValue % dividerValue; // 将 remainderValue作为参数传给new Integer(),将其转换为Integer对象 Integer remainderIntValue = new Integer( remainderValue); // 对Integer对象 remainderIntValue调用toString()方法将其转换为String String remainder = remainderIntValue.toString(); // 打印变量 Stringremainder System.out.println("variable remainder Value --->" + remainder); } } } 

下面是程序输出:

变量余值 ->3

在上面的程序中,我们已经创建了整数类的实例

整数(remainderValue);

并对其调用toString()方法,如下所示:

字符串 remainder = remainderIntValue.toString();

该语句返回整数类对象 remainderIntValue 的字符串表示。

#5) 使用Integer.toString(int)方法

整数 也提供了一个静态方法 toString ()将int转换为String。

让我们看一下下面的方法签名:

public static String toString(int i)

该静态方法返回指定整数的字符串对象表示。 这里,参数被转换为有符号的十进制表示,并作为字符串返回。 这与重载方法toString(int i, int radix )完全相同,其中radix值为10。

参数:

i: 这是一个需要转换的整数值。

返回:

弧度为10的参数i的字符串表示。

让我们了解一下这个的用途 整数 . toString(int i) 方法。

让我们编写示例程序代码,提示用户输入数字,计算数字的平方,并使用Integer.toString(int i)方法在控制台打印平方,将整数squareValue转换为字符串。

这里是下面的示例程序:

 包 com.softwaretinghelp; import java.util.Scanner; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用Integer.toString(int i )方法 * * @author * */ public class IntStringDemo5 { private static Scanner scanner; public static void main(String[] args) { //提示用户使用Scanner输入,这里System.in是一个标准输入流 scanner =new Scanner(System.in); System.out.print("Please Enter the number"); //扫描用户输入的下一个符号为一个int,并将其分配给变量x int x= scanner.nextInt(); //计算分配给x的数字的平方 int squareValue = x*x; //将 squareValue作为一个参数传给Integer.toString(),将 // squareValue转换成String String square = Integer.toString(squareValue); //打印变量String square System.out.println("Variable square Value --->" + square); } } } 

下面是程序输出:

请输入数字5

变量平方值->25

在上面的程序中,我们通过传递squareValue作为参数调用了Integer类的静态方法toString。

字符串square = 整数。 toString (squareValue);

这将返回int值squareValue的字符串表示。

让我们再看看一些方法,即利用StringBuffer、StringBuilder类的方法。

StringBuffer类用于向String追加多个值。 StringBuilder也做同样的工作,唯一的区别是StringBuffer是线程安全的,而StringBuilder不是。

Java字符串教程

#6) 使用StringBuilder类方法

让我们看看如何在Java中使用StringBuilder方法将int转换为String。

下面是方法的签名:

public StringBuilder append(int i)

该方法将int参数的字符串表示法追加到序列中。

参数:

i:这是一个整数。

返回:

这是对该对象的一个引用。

public String toString()

该方法返回一个字符串,代表该序列中的数据。

下面给出的是一个计算整数值平均值的示例程序,说明了使用StringBuilder将整数avgNumber转换为String。

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序的示例代码 * 使用StringBuilder append() toString() 方法 * * @作者 * */ public class IntStringDemo6 { public static void main(String[] args) { // Assign values to array of type int int[] numArray = {15,25,60,55}; //Find array size int arLength = numArray.length; int arrSum = 0;//计算所有数字的加法 for(int i=0;i" + average); } } 

下面是程序输出:

可变的平均值 ->38

在上面的程序中,我们使用了StringBuilder append()方法,并使用toString()方法将StringBuilder对象转换为String。

strbAvg.append(avgNumber);

String average = strbAvg.toString();

#7) 使用StringBuffer类方法

让我们看看Java使用StringBuffer方法将int转换为String的方法。

下面是方法的签名:

public StringBuffer append(int i)

该方法将int参数的字符串表示法追加到序列中。

参数:

i:这是一个整数。

返回:

这是对该对象的一个引用。

public String toString()

该方法返回一个字符串,代表该序列中的数据。

让我们看看下面的示例程序,我们使用Math.min()方法在2个int值中找到较低的值,使用StringBuffer方法将整数minValue转换为字符串。

 包 com.softwaretinghelp; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用StringBuffer append() toString() 方法 * * @author * */ public class IntStringDemo7 { public static void main(String[] args) { // Assign int 60 to int variable a int a = 60; // Assign int -90000 to int variable b int b = -90000; // Get lower value between int a and b using Math类方法 min() int minValue = Math.min(a, b); //将minValue作为参数传给StringBuffer.append()方法 StringBuffer strbMinValue = new StringBuffer(); strbMinValue.append(minValue); //使用toString()方法将strbMinValue转换为字符串 String minimumValue = strbMinValue.toString(); //打印变量字符串miniumValue System.out.println("变量miniumValue值 --> " +最小值); } } 

下面是程序输出:

变量miniumValue值 ->-90000

在上面的程序中,我们使用了StringBuffer append ()方法,并使用toString ()方法将StringBuffer对象转换为String。

strbMinValue.append(minValue);

String minimumValue = strbMinValue.toString();

#8) 使用DecimalFormat类方法

Java int也可以使用java.text.DecimalFormat类方法转换为String。

下面是该类的format ()方法的方法签名。

NumberFormat.DecimalFormat扩展了NumberFormat类。

public final String format(long number)

该方法返回数据类型为long的参数的格式化字符串。

参数:

number: 这是数据类型为long的值。

返回:

格式化的字符串

下面的示例程序说明了使用DecimalFormat类方法将整数elementValue转换为字符串。

 包 com.softwaretinghelp; import java.text.DecimalFormat; import java.util.Scanner; /** * 本类演示了将int转换为String的Java程序示例代码 * 使用DecimalFormat format()方法 * * @author * */ public class IntStringDemo8 { private static Scanner scanner; public static void main(String[] args) { // Assign values to array of arrays of type int int[] [] numArray = {{15,20,30,60}, {300,600,900} }; //提示用户使用Scanner输入,这里System.in是一个标准输入流 scanner = new Scanner(System.in); System.out.println("Please Enter the array number"); //扫描用户输入的下一个令牌为一个int并将其分配给变量x int x= scanner.nextInt(); System.out.println("Please Enter the element number") //扫描用户的下一个令牌输入一个int并将其分配给变量y int y= scanner.nextInt(); int elementValue = numArray[x][y]; System.out.println(elementValue); // 传递 "#"作为DecimalFormat的格式 DecimalFormat formatElement = new DecimalFormat("#"); //将elementValue作为参数传给format()方法,将其转换成String element = formatElement.format(elementValue); // 打印变量 String elementSystem.out.println("变量元素值--->" + element); } } 

下面是程序输出:

请输入阵列编号

1

请输入元素编号

1

600

变量元素值 ->600

在上面的程序中,我们使用DecimalFormat类的format()方法,将int elementValue转换为String,如下所示:

String element = formatElement.format(elementValue);

因此,我们已经涵盖了将Java整数转换为字符串值的多种方法。 在所有的示例程序中,我们已经看到了需要将整数值转换为字符串值并显示控制台输出的各种情况。

因此,为了在Java中把整数转换为字符串,可以在你的Java程序中使用上述示例代码中的任何方法。

下面是一些关于int到String转换的常见问题。

关于在Java中将Int转换为String的常见问题

问题#1)我们可以在Java中把int转换成String吗?

答案是: , 在Java中我们可以将int转换为String。

我们可以使用以下方法将int转换为String:

  • 字符串串联
  • String.valueOf ()
  • String.format()
  • Integer.toString()
  • Integer.String(int)
  • 粘贴()的StringBuilder
  • StringBuffer append ()
  • 十进制格式(DecimalFormat)

问题#2)我们可以将int类型转换为字符串吗?

答案是: 是的,我们可以使用String和Integer类的方法将int转换为String,如String.valueOf(), Integer.toString()等。

问题#3) 我们如何将一个字符串转换为一个数字?

答案是: 使用Integer类的方法,如Integer.valueOf()和Integer.parseInt(),可以将字符串转换为int类型的数字。

总结

在本教程中,我们探讨了如何在Java中使用以下方法将整数转换为字符串:

  • 字符串串联
  • String.valueOf ()
  • String.format()
  • Integer.toString()
  • Integer.String(int)
  • 粘贴()的StringBuilder
  • StringBuffer append ()
  • 十进制格式(DecimalFormat)

我们详细介绍了每一种方法,并借助一个示例说明了每种方法的使用。

滚动到顶部