×

Java Programs

Java Practice

Java program to convert a short integer into a string

Given/input a short integer, we have to convert it into a string.
Submitted by Nidhi, on March 15, 2022

Problem statement

In this program, we will read a short value from the user and convert the input short value into the string Short.toString() method.

Java program to convert a short integer into a string

The source code to convert a short integer into the string is given below. The given program is compiled and executed successfully.

// Java program to convert a short integer 
// into a string

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner X = new Scanner(System.in);
    String str;
    short shortVal = 0;

    System.out.print("Enter short value: ");
    shortVal = X.nextShort();

    //Convert short value into string.
    str = Short.toString(shortVal);

    System.out.println("String value: " + str);
  }
}

Output

Enter short value: 12345
String value: 12345

Explanation

In the above program, we imported "java.util.Scanner" to read input from the user. And, created a Main class that contains a method main().

The main() method is the entry point for the program, here we read a short value from the user using the Scanner class. Then we converted the input short value into a string using the Short.toString() method and printed the result.

Java Conversion Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.