Use Integer.parseInt() Method
Add the following code to use the parseInt()
method to accept integer input as a string:
public class ToUnsignedLong {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer value: ");
String strNum = input.nextLine();
int num = Integer.parseInt(strNum);
long ulong = Integer.toUnsignedLong(num);
System.out.println("Unsigned long value is: " + ulong);
}
}
The above code accepts the integer input as a string using the nextLine()
method. The parseInt()
method parses the string and returns the integer value. The toUnsignedLong()
method then converts the integer value to it's equivalent unsigned long value which is then printed to the console.