Found 9150 Articles for Object Oriented Programming

How to resolve "Could not find or load main class package" in Java?

Maruthi Krishna
Updated on 14-Oct-2019 07:59:08

5K+ Views

Once you write a Java program you need to compile it using the javac command, this shows you the compile time errors occurred (if any).Once you resolve them and compile your program success fully, an executable file with the same name as your class name is generated in your current folder, with the .class extension.Then you need to execute it using the java command as −java class_nameWhile executing, when JVM does not find a .class file with the specified name then a run time error occurs saying “Could not found or load main class” error as −D:\sample>java Example Error: Could ... Read More

How to overwrite a line in a .txt file using Java?

Maruthi Krishna
Updated on 12-May-2025 19:07:46

20K+ Views

The given task is to overwrite a line in a .txt file with new content. Assume we have a .txt file with the following content - Line 1: Hello World Line 2: This is a test file. Line 3: This line will be overwritten. Line 4: Goodbye! We want to overwrite line 3 with "This line has been changed". Following will be the resultant content - line 1: Hello World line 2: This is a test file. line 3: This line has been changed. line 4: Goodbye! Overwriting a Line in a ".txt" File There are no direct methods ... Read More

Program to replace all the characters in of a file with '#' except a particular word in Java

Maruthi Krishna
Updated on 14-Oct-2019 07:51:55

622 Views

The split() method of the String class. splits the current string around matches of the given regular expression. The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string.The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String.To replace all the characters in of a file with '#' except a particular word (one way) −Read the contents of a file to a String.Create ... Read More

How to parse Date from String in the format: dd/MM/yyyy to dd/MM/yyyy in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:46:50

15K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).One of the constructors of this class accepts a String value representing the desired date format and constructors SimpleDateFormat object.The format() method of this class accepts a java.util.Date object and returns a date/time string in the format represented by the current object.Therefore, to parse a date String to another date format −Get the input date string.Convert it into java.util.Date object.Instantiate the SimpleDateFormat class by passing the desired (new) format as string to its constructor.Invoke the format() method by passing the above ... Read More

Is it possible to check if a String only contains ASCII in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:37:37

2K+ Views

Using regular expressionYou can find whether a particular String value contains ASCII characters using the following regular expression −\A\p{ASCII}*\zThe matches() method of the String class accepts a regular expression and verifies whether the current string matches the given expression if so, it returns true, else it returns false.Therefore, Invoke the matches() method on the input/required string by passing the above specified regular expression as a parameter.Example Live Demoimport java.util.Scanner; public class OnlyASCII {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string value: ");       String input ... Read More

How do we initialize an array within object parameters in java?

Maruthi Krishna
Updated on 14-Oct-2019 07:33:00

5K+ Views

You can initialize the array variable which is declared inside the class just like any other value, either using constructor or, using the setter method.ExampleIn the following Java example, we are declaring an instance variable of array type and initializing it from the constructor. Live Demopublic class Student {    String name;    int age;    String subs[];    Student(String name, int age, String subs[]){    this.name = name;    this.age = age;    this.subs = subs; } public void display() {    System.out.println("Name: "+this.name);    System.out.println("Age :"+this.age);    System.out.print("Subjects: ");    for(int i = 0; i < subs.length; i++) { ... Read More

How to get ArrayList<String> to ArrayList<Object> and vice versa in java?

Maruthi Krishna
Updated on 07-Jun-2024 10:28:55

949 Views

ArrayList to ArrayList Instead of the typed parameter in generics (T) you can also use “?”, representing an unknown type. These are known as wild cards you can use a wild card as − Type of parameter or, a Field or, a Local field. Using wild cards, you can convert ArrayList to ArrayList as − ArrayList stringList = (ArrayList)(ArrayList)(list); Example import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListExample { public static void main(String args[]) { //Instantiating an ArrayList object ... Read More

How can we convert a JSON array to a list using Jackson in Java?

Aishwarya Naglot
Updated on 05-Jun-2025 14:58:10

9K+ Views

Jackson is a Java-based library, and it can be useful for converting Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method,  readValue(), which takes a JSON string and converts it to the object class specified in the second argument. Before proceeding further, first of all, we need to import the Jackson library into our project. To do this, you can add the following dependency to ... Read More

How can we serialize a list of objects using flexjson in Java?

raja
Updated on 06-Jul-2020 12:26:47

1K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");       Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");   ... Read More

How can we create a JSON using JsonGenerator in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:43:25

7K+ Views

JSON is a format for storing and exchanging data. It is easily readable and also easy to parse. Creating JSON Using Java JsonGenerator In Java, we can create a JSON object using the JsonGenerator class. JsonGenerator is class in the javax.json.stream package that is used to create JSON data. It provides methods to write JSON objects, arrays, and values. To use the JsonGenerator class, we need to add the javax.json library to our project. If you are using Maven, add this to your pom.xml file: javax.json javax.json-api 1.1.4 If you are not using Maven, you can download ... Read More

Advertisements