Tag: How to in Java
Java – How to Convert String to Long ?
Problem Statement You need to convert a java string to long in your java program. Solution The output of the above program is Abundantcode.com Java Tutorials 75 79
Java – How to Read Integers from the Console for calculation ?
Problem Statement You need to read integers from the console window and use it for calculation within your java program. Solution If you want to know how to read integer value from a console and use it for calculation , you can use the Scanner class as shown below. The output of the above program is Please enter the first number for abundantcode 10 Please…
Java – How to Convert primitive long datatype to Long object?
Problem Statement You need to convert the primitive long datatype to object in Java. Solution Use the Long wrapper class to convert the long primitive to Long object as shown below. The output of the above program is Abundantcode.com Java Tutorials 89
Java – How to read a valid Integer number from Console ?
Problem Statement You need to read a valid integer number from the Console in your java program. If the number is invalid , show appropriate message. Solution To read a valid integer from a console program , you can use the Scanner class and pass the System.in as the parameter to its constructor. The scanner class exposes a method called nextInt() which can be used…
Java – How to convert Long to other primitive numeric data types ?
Problem Statement You need convert a long value to other numeric data types in java. Solution Use the Long wrapper class which exposes methods like byteValue , shortValue , intValue etc. Below is a sample code snipper demonstrating the usage of the same. The output for the above program is Abundantcode.com Java Tutorials 76 76 76.0
Java How to – Compile and run Java program in Command-line
Problem Statement You need to compile and run your java program in command-line. Solution You can use the command-line tools available with Java Development Kit (JDK). The “javac” command tool can be used to compile the java program. The “java” command can be used to run the java program. Let’s build out first program and use the command line to run. 1. Download and install…
Java How to – Find if the Class exists or not
Problem Statement You need to test for the presence of a class from your java program. Solution Use Class.forName method to find if a particular class exists. Below is a sample code snippet demonstrating the usage of Class.forName to find out if the class “test” exists. Note that this method throws an exception incase the class cannot be loaded. Note: You need to pass the…
Java – How to create a random number which is less than 25 ?
Problem Statement You need to create a random number which is less than 25 in Java. Solution Use the Random class’s nextInt method and pass the value within which the random number needs to be generated.
Java – How to Convert Long to String in Java ?
Problem Statement You need to convert long to a string value in your java program. Solution Use the toString method to convert long to string in Java. The output for the above program is Abundantcode.com Java Tutorials 75
Java – How to Convert Numbers to Objects and vice versa ?
Problem Statement You need convert numbers to objects and vice versa in your Java program. Solution There are times when you need to pass an object for a function where you have a numeric values. To convert numbers to objects and vice versa , you can use the corresponsing wrapper classes. For example , if you want to convert an int to Integer object or…
Java – How to decode string from Hexadecimal and Octal to Integer in Java ?
Problem Statement You need to decode a string from hexadecimal to integer in java as well as octal to integer. Solution Use the decode method defined in the Integer class to decode an hexadecimal and octal number to integer as shown below. The output of the above program is 255 46
Java – How to Check if integer is multiple of a number?
Problem Statement You need to check if the integer number is a multiple of a number in Java. Solution Use the modulus operator and check the remainder to find it out. Below is a sample code snippet demonstrating how to find if the number is a multiple of 4.
How to Declare an Octal literal in Java ?
Problem Statement You want to declare an octal literal in Java. Solution Prefix the value with 0. A literal that contains leading zero is an octal number. The output of the above program will be 8 21
Java – How to declare Hexadecimal literal ?
Problem statement You need to declare an Hexadecimal literal in Java and display the result on a console window. Solution Hexadecimal numbers can contain numbers from 0-9 as well as characters A-F where A-F represents 10-15. You can prefix the numbers with 0X to denote it as hexa decimal number as shown below. The output of the above program will be 10 21
Java How to – Get Environment Variables
Problem Statement You need to programmatically get the values of the environment variables from your java program. Solution Use java.lang.System.getenv method to get the environment variables from your java program from Java JDK 1.5 and higher version. You can pass the “PATH” as parameter to the method to retreive the path from the environment variable. When no arguments are passed, it returns all the environment…