Write Java program to Convert a short integer into a string


This Java program prompts the user to input a short integer value and then converts the short integer value to a string using the Short.toString() method. The resulting string is then printed to the console.

Here's a brief explanation of the program:

  • The program starts by importing the java.util.Scanner class for input and creating a Scanner object.
  • The main() method declares a String variable str and a short variable short_val and initializes the latter to 0.
  • The program prompts the user to enter a short integer value using the System.out.print() method and stores the user's input in short_val using the Scanner.nextShort() method.
  • The Short.toString() method is used to convert the short value to a String and store it in the str variable.
  • Finally, the program prints the resulting string to the console using the System.out.println() method.

Note that the Short.toString() method is a static method that takes a short value as its argument and returns a String representation of that value.

Source Code

import java.util.Scanner;
public class ShortInt_ToString
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		String str;
		short short_val = 0;
 
		System.out.print("Enter the Short Value : ");
		short_val = input.nextShort();
 
		//Convert short integer to string
		str = Short.toString(short_val);
		System.out.println("String Value : " + str);
	}
}

Output

Enter the Short Value : 124
String Value : 124