Java multithreading optimization

A question of a test I had today was to optimize a class that could be accessed by different threads, it was something like this and I had to write the code for setters and getters:

public class Couple {
String a;
String b;

public Couple() {
    a=null;
    b=null;
}

So I wrote something like this:

public class Couple {
String a;
String b;

public Couple() {
    a=null;
    b=null;
}

public void setA(String v) {
    synchronized(a) {
        while(a!=null)
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        a=v;
        this.notifyAll();           
    }
}

public void setB(String v) {
    synchronized(b) {
        while(b!=null)
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        b=v;
        this.notifyAll();           
    }
}

public String getA() {
    synchronized(a) {
        while(a==null)
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        String result = a;
        a = null;
        this.notifyAll();
        return result;
    }
}

public String getB() {
    synchronized(b) {
        while(b==null)
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        String result = b;
        b = null;
        this.notifyAll();
        return result;
    }
}

}

Is it optimized in a good way or there are runtime errors in your opinion?

Warning:One of the plugins you are using supports Java 8 language features

Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle:
apply plugin: ‘me.tatarka.retrolambda’
To learn more, go to https://d.android.com/r/tools/java-8-support-message.html

Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

How to get items from string in java android Studio

Iam developing an online booking android app. i have a little issue to get the data from the response i am getting from database. my Json object response is like this example:
“Sat”: “1300-1320:1,1325-1345:5,1350-1410:9,1415-1435:1…,

in the above 1300-1320 is time slot, it is actually 13:00 to 13:20 and 1 is number of available tickets.

I need the items on list view be like this:

13:00 to 13:20

The available tickets are 1

13:25 to 13:45

The available tickets are 5

13:50 to 14:10

The available tickets are 9

and … You can get the idea. The problem is i don’t know how to achieve this.

Can you please help me. I will appreciate any help.

Thanks!

Filling objects in object array, null pointer exception [duplicate]

This question already has an answer here:

In my addStudent method I am trying to add students(objects) in an object array, but I receive nullPointerException when I add even one object there.

package task2;

public class StudentGroup {
    String groupSubject;
    Student[] students;
    int freePlaces;

    StudentGroup(){
        Student[] students = new Student[5];
        freePlaces = 5;
    }
    StudentGroup(String subject){
        this();
        this.groupSubject = subject;
    }

    void addStudent(Student s){
        if(freePlaces > 0 && s.subject.equals(this.groupSubject)) {        
            this.students[this.freePlaces-1] = s;                                      //check mistake?
            freePlaces--;
        }
        else{
            System.out.println("Student isn't from same subject or there aren't any free spaces!");
        }
    }

    void emptyGroup(){
        Student[] students = new Student[5];
        freePlaces = 5;
    }

    String bestStudent(){
        double highestGrade = students[0].grade;
        String name = students[0].name;
        for (int i = 1; i < students.length; i++) {
            if(students[i].grade > highestGrade){
                highestGrade = students[i].grade;
                name = students[i].name;
            }
        }
        return name;
    }

    void printStudentsInGroup(){
        for (int i = 0; i < students.length; i++) {
            System.out.println(students[i].name + "-" + students[i].grade);
        }
    }
}

I am also not sure if I can just call 5 times the method and fill every student in the array or I have to loop over the array to do so. Most of the info on Internet is with ArrayList, but I can’t use it.

Cannot determine embedded database driver class for database type NONE for apache derby

I am trying to start my Spring Boot application. This is my Main class:

@SpringBootApplication
public class CourseApiDataApplication {

    public static void main(String[] args) {
        SpringApplication.run(CourseApiDataApplication.class, args);
    }
}

This is my maven configuration in pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.courseapi</groupId>
    <artifactId>course-api-data</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>course-api-data</name>
    <description>API with Spring Data JPA</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

When I start the project I get this:

***************************
APPLICATION FAILED TO START
***************************

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

I google on the internet a couple of solutions. This is the first approach I have tried> I added @EnableAutoConfiguration (exclude = { DataSourceAutoConfiguration.class }) annotation on my main class. But when I started project, i Get this error:

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-17 20:49:58.471 ERROR 9992 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

Then I tried with adding parameter in application.properties file:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

But still I am getting the following error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
    - Bean method 'dataSource' not loaded because @ConditionalOnBean (types: org.springframework.boot.jta.XADataSourceWrapper; SearchStrategy: all) did not find any beans


Action:

Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.

So, what am I doing wrong here?

How to Encode String to display color text( based on the prefixes within String)

I’m new to Java and I’m struggling with the following task.
Im writing an app which encodes the input of a user(depending of one’s choice) into code that is compiled in Command Line interpreter in Quake III Arena ,that enables player to color nick’s letter depending to the following char prefixes(for example “^0Black^3Yellow)
^0 BLACK
^1 WHITE
^2 GREEN
^3 YELLOW
^4 BLUE
^5 CYAN(LIGHT BLUE)
^6 MAGENTA PURPLE
^7 WHITE
That is how app looks in current stage

The code work properly but i want to generate a Color Text based on the input, that will preview the current look of the Nick by clicking generate.

I gave up after several attemps and i have no clear idea how to approach in this subject.Anything would be highly appreciated.I wish i was specific enough.Thank you in advance.

Trying to copy sqlite database in android and getting “No such file or directory”

Okay so I have a database and I want to copy it to sd card to keep a backup and I found this code

private void exportDB(){
    File sd = Environment.getExternalStorageDirectory();
    File data = Environment.getDataDirectory();
    FileChannel source=null;
    FileChannel destination=null;
    String currentDBPath = "/data/"+ "com.example.myapp" +"/databases/"+"mydatabase";
    String backupDBPath = "/storage/extSdCard/mydatabase";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    try {
        source = new FileInputStream(currentDB).getChannel();
        destination = new FileOutputStream(backupDB).getChannel();
        destination.transferFrom(source, 0, source.size());
        source.close();
        destination.close();
    } catch(IOException e) {
        //e.printStackTrace();
        Toast.makeText(this, "Err:"+e, Toast.LENGTH_LONG).show();
    }
}

As I wrote in the title I get the “No such file or directory” ENOENT error when I try to do it.

Apparently the database should be stored right there..
I’ve tried changing the path to a few different things but still nothing…
I tried

"/data/"+ "com.example.myapp" +"/"+"mydatabase.db"

and

"//data//"+ "com.example.myapp" +"//databases//"+"mydatabase"

etc. nothing works and I also can’t find a way to check the path in any way.

I basically have the database made, inserted a few rows into it and have some basic functions like add() and delete() etc. am I missing something?

Can I define an abstract method without the use of abstract keyword in Java?

I know we can define abstract methods inside abstract class only.
I also know we can’t instantiate abstract class on its own, it must be inherited (extended).
And I know that abstract method forces subclasses to provide an implementation for that method.
My question is only if I can define that abstract method without the “abstract” keyword?
Something like:

public abstract Animal
{ 
    public void makeNoise();
}