Tag: How to in Java

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 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 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 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…