belongs to collection: Java Conversion Programs
In this program, we will read a numeric string from the user and convert the input string into a byte using 2 different methods.
Program/Source Code:
The source code to convert string to byte is given below. The given program is compiled and executed successfully.
// Java program to convert // string to byte import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner X = new Scanner(System.in); String str; System.out.print("Enter string: "); str = X.next(); byte byteValue = 0; //Using byteValue() method. byteValue = Byte.valueOf(str).byteValue(); System.out.println("Byte value: " + byteValue); //Using parseByte() method. byteValue = Byte.parseByte(str); System.out.println("Byte value: " + byteValue); } }
Output:
Enter string: 123 Byte value: 123 Byte value: 123
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
In this program, we will read a numeric string from the user and convert the input string into a byte using 2 different methods.
Program/Source Code:
The source code to convert string to byte is given below. The given program is compiled and executed successfully.
Output: