Java Articles

Page 286 of 450

How to perform sort using Java?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Feb-2020 150 Views

You can sort an array using sort() method of the Arrays class.ExampleLive Demoimport java.util.Arrays; public class MainClass {    public static void main(String args[]) throws Exception {       int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };       Arrays.sort(array);       printArray("Sorted array", array);       int index = Arrays.binarySearch(array, 1);       System.out.println("Didn't find 1 @ " + index);       int newIndex = -index - 1;       array = insertElement(array, 1, newIndex);       printArray("With 1 added", array);    }   ...

Read More

What is the difference between System.out.println() and System.out.print() in Java?

Sharon Christine
Sharon Christine
Updated on 20-Feb-2020 2K+ Views

The println() terminates the current line by writing the line separator string. The print() method just prints the given content.ExampleLive Demopublic class Sample {    public static void main(String args[]) {       System.out.println("Hello");       System.out.println("how are you");       System.out.print("Hello");       System.out.print("how are you");    } }OutputHello how are you Hellohow are you

Read More

How to capture file not found exception in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 20-Feb-2020 435 Views

While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample {    public static void main(String args[]) throws Exception {       File file = new File("myFile");       FileInputStream fis = new FileInputStream(file);       System.out.println("Hello");    } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified)       at java.io.FileInputStream.open(Native Method)       at java.io.FileInputStream.open(Unknown Source)       at java.io.FileInputStream.(Unknown Source) ...

Read More

How to capture divide by zero exception in Java?

Swarali Sree
Swarali Sree
Updated on 20-Feb-2020 2K+ Views

When you divide a number by zero an Arithmetic Exception number is thrown.ExampleLive Demopublic class DividedByZero {    public static void main(String args[]) {       int a, b;       try {          a = 0;          b = 54/a;          System.out.println("hello");       } catch (ArithmeticException e) {          System.out.println("you cannot divide a number with zero");       }    } }Outputyou cannot divide a number with zero

Read More

Can we define a class inside a Java interface?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 7K+ Views

Yes, you can define a class inside an interface. In general, if the methods of the interface use this class and if we are not using it anywhere else we will declare a class within an interface.Exampleinterface Library {    void issueBook(Book b);    void retrieveBook(Book b);    public class Book {       int bookId;       String bookName;       int issueDate;       int returnDate;    } } public class Sample implements Library {    public void issueBook(Book b) {       System.out.println("Book Issued");    }    public void retrieveBook(Book b) { ...

Read More

What is the Eclipse keyboard shortcut for "public static void main(String[] args) " in Java?

Ramu Prasad
Ramu Prasad
Updated on 20-Feb-2020 9K+ Views

To get public static void main(String[] args) line in eclipse without typing the whole line type main and press Ctrl + space then, you will get the option for the main method select it.

Read More

How to find package explorer in Java eclipse project?

V Jyothi
V Jyothi
Updated on 20-Feb-2020 25K+ Views

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer.There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

Read More

How to count the number of lines in a text file using Java?

Arushi
Arushi
Updated on 20-Feb-2020 2K+ Views

To count the number of lines in a fileInstantiate the FileInputStream class by passing an object of the required file as parameter to its constructor.Read the contents of the file to a bytearray using the read() method of FileInputStream class.Instantiate a String class by passing the byte array obtained, as a parameter its constructor.Now, split the above string into an array of strings using the split() method by passing the regular expression of the new line as a parameter to this method.Now, find the length of the obtained array.Exampleimport java.io.File; import java.io.FileInputStream; public class NumberOfCharacters {    public static void main(String args[]) throws Exception{ ...

Read More

How to count the number of words in a text file using Java?

Paul Richard
Paul Richard
Updated on 20-Feb-2020 6K+ Views

Read the number of words in text fileCreate a FileInputStream object by passing the required file (object) as a parameter to its constructor.Read the contents of the file using the read() method into a byte array. Insatiate a String class by passing the byte array to its constructor.Using split() method read the words of the String to an array.Create an integer variable, initialize it with 0, int the for loop for each element of the string array increment the count.Exampleimport java.io.File; import java.io.FileInputStream; public class Sample {    public static void main(String args[]) throws Exception{       int count =0;       File ...

Read More

What are classpath of projects in Java eclipse projects?

Nikitha N
Nikitha N
Updated on 20-Feb-2020 4K+ Views

You can include Jar files to which you need to set the classpath in the eclipse project using build pathStep 1 − Right click on the project Select Build Path → Configure Build Path.Step 2 − Select libraries select Add External JARs… button.Step3 − Then browse through the folder where the required jar files exits, select them and press open.Selected jar files will be added to the Libraries. Finally, press OK.Now, if you open the Referenced libraries in the project you can observe the added jar file.

Read More
Showing 2851–2860 of 4,496 articles
« Prev 1 284 285 286 287 288 450 Next »
Advertisements