Why is my Merge Sort not saving the sorted arrays?

I realize i’m not returning my tempPoints to anything but what can I do to fix this?

Right now it sorts correctly, but when it sorts the next tier of halves they remain unsorted.

UNSORTED POINTS:

(-2, -42)
(-15, 2)
(32, 8)
(-26, 21)
(39, -42)
(-40, -18)
(-30, 7)
(-12, -28)
(19, -16)
(-16, -38)

SORTED POINTS :

(-2, -42)
(-15, 2)
(32, 8)
(-26, 21)
(39, -42)
(-40, -18)
(-30, 7)
(-12, -28)
(19, -16)
(-16, -38)

If i step through my program using the debugger, I can see it sorting each partition correctly but it doesn’t carry over to the next merge.

private void mergeSortRec(Point[] pts)
{
    int middle = pts.length / 2;


    if( pts.length ==1)
    {
        return;
    }

    Point[] left = new Point[middle];
    Point[] right = new Point[pts.length - middle];


    for(int i = 0; i < middle; i++)
        left[i] = pts[i];

    for( int j = 0; j < pts.length - middle; j++ )
        right[j] = pts[middle+j];

    mergeSortRec(left);
    mergeSortRec(right);

    merge(left, right);
}

private Point[] merge(Point[] left, Point[] right)
{
    int x = 0;
    int i = 0;
    int j = 0;
    Point[] tempPoints = new Point[(left.length) + (right.length)];

    while( i < left.length || j < right.length )
    {
        if( i < left.length && j < right.length)
        {   
            if( pointComparator.compare(left[i], right[j]) == -1 )
            {
                tempPoints[x] = left[i];
                x++;
                i++;
            }
            else
            {
                tempPoints[x] = right[j];
                x++;
                j++;
            }
        }
        else if (i == left.length)
        {
            tempPoints[x] = right[j];
            x++;
            j++;
        }
        else if( j == right.length)
        {
            tempPoints[x] = left[i];
            x++;
            i++;
        }

    }

    return tempPoints;
}

ResultSetMetadata getColumnName starts with 1 index Instead of 0

Why ResultSetMetadata getColumnName starts with 1 index instead of 0. I have the below code.

ResultSetMetaData mData = resultSet.getMetaData();

int count = mData.getColumnCount();
for (int i = 1; i <= count; i++)
{
    System.out.println(metaData.getColumnName(i));

}

Just wanted to know why it starts from 1 instead of 0. Just curious.

Thanks in advance.

Checking String using Regex in Java

I am trying to check that a string follows a particular format using Regex. For example the user enters in the console:

next 33

Which then activates an if statement which checks if it follows the format “next (digit)(digit)”.

if ("next \d\d".equalsIgnoreCase(command)) {
   //further code
}

I have also tried:

if (command.matches("next //d//d")) {
   //further code
}

From what I have read //d can be any digit 0-9. So I can’t understand why this does not work. It seems like I should be able to checking a line like above but I have very little experience with Regex.

I am sorry for this elemetary quesion. Thank you.

LDAP IN STRUTS FRAMEWORK

I am new to the Struts2 Framework. I need to implement LDAP Authorization logic in to my existing web application using Struts2 framework. Basically I need to verify user Id existence in LDAP in Login page.

Java error, duplicate local variable

I’m trying to debug a portion of code for an assignment (I’m still very new to Java) and have combed through many resources to solve this conflict but still can’t quite work it out.

public static void main(String [] args){
    Scanner keyboard = new Scanner(System.in);
    String input = null;
    do
    {
      System.out.println("Enter 'A' for option A or 'B' for option B.");
      String input = keyboard.next();
      input.toLowerCase();
      input.charAt(0);  
    }
    while ((input != "a") || (input != "b"));
}

I always get a Duplicate Local Variable error with the input String.

Any help would be greatly appreciated!

java aws sdk to get the list of all non glaciered objects from s3

I am using java aws sdk to get the list of all objects from the s3 bucket my s3 bucket has around 600 objects, while i get the object summaries is there any way i can get the non glacired one’s directly.

here is the code that I am using, but it gives me the list of glacier objects too

ObjectListing listing = s3Client.listObjects("bucketname","directoryname");
List<S3ObjectSummary> summaries = listing.getObjectSummaries();

here i wanted to get only non glaciered objects in my summaries, how would i do that?

How to catch and ignore IncompatibleType Error in Java

I have been searching for about 2 hours now and cannot find a way to catch and ignore an incompatible type error in Java. This is compile-time error caused by attempting to assign a float value to an integer. I know why this error is occurring (narrow conversion) and specifically WANT this error to occur as it is for a demonstration of the error itself (needed for a course I am in), but in attempting to catch the error using a try-catch statement I am having no luck.

I know you should specify the error type you are catching, but for the purpose of this code it is okay if I take the generic route here, because everything is pretty well contained and very simple. I would never use this kind of error checking for anything more involved than the purposes of this project, but if you know the exact error type causing the message “error: incompatible types: possible lossy conversion from float to int” to be displayed when attempting to compile, I’ll use that error type. I looked through multiple sites and could not find the exact type of this error (such as IOException, for example).

Here is the code I am trying to execute:

import java.lang.*;
public class AssignTests {  

int integer = 10;
float floater = 1234.1234f;

public static void main(String[] args){ 
    AssignTests test = new AssignTests();

    try{
        test.integer = test.floater;
    }
    catch (Exception t)
    {
        System.out.println("An integer cannot be assigned the value of a float.");
    }
  }
}

When I go to compile this code I still see the error message of incompatible types despite trying to ignore this issue via the try-catch statement, and after searching for this long on how to resolve this am pretty stumped.

Any idea on how to get this to compile and give me the output message I am looking for?

Entering Time into SQLite server Android Studio

So I’m trying to use a timepickerdialog to receive the time that the user inputs and place it into a sqlite server on android studio. I’m having trouble once the user inputs the time.

TimePickerFragment.java

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{

private HabitHelper mHelper;

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    final Calendar c = Calendar.getInstance();
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);

    return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));

}


@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
    SQLiteDatabase db = mHelper.getWritableDatabase();

    ContentValues values = new ContentValues();

    String time = String.valueOf(hourOfDay) + ":" + String.valueOf(minute);

    values.put(HabitContract.HabitEntry.COLUMN_TIME, time);

}

HabitHelper.java

public class HabitHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "HabitHamster";
public static final int DB_Version = 3;

public HabitHelper(Context context){
    super(context, DB_NAME, null, DB_Version);
}


@Override
public void onCreate(SQLiteDatabase db) {
    String createTable = "CREATE TABLE "
            + HabitContract.HabitEntry.TABLE + " ( "
            + HabitContract.HabitEntry._ID
            + " INTEGER PRIMARY KEY, "
            + HabitContract.HabitEntry.COLUMN_NAME_TITLE
            + " TEXT NOT NULL, "
            + HabitContract.HabitEntry.COLUMN_TIME
            + " TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
            + ");";

    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + HabitContract.HabitEntry.TABLE);
    onCreate(db);
}

HabitContract.java

public class HabitContract {

private HabitContract() {}

public class HabitEntry implements BaseColumns{
    public static final String TABLE = "habits";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String COLUMN_TIME = "time";
}

Heres the portion of my MainActivity that is using the timepickerdialog

 mHabitView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
            View v = mHabitView.getChildAt(position);
            CheckedTextView ctv = v.findViewById(R.id.habitItem);
            DialogFragment newFragment = new TimePickerFragment();
            newFragment.show(getFragmentManager(), "TimePicker");
            return true;
        }
    });

EDIT: Sorry, the error I receive after a the user inputs the time is:

10-05 18:05:37.281 32708-32708/com.example.jonathan.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.jonathan.myapplication, PID: 32708
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.jonathan.myapplication.db.HabitHelper.getWritableDatabase()' on a null object reference
                                                                                    at com.example.jonathan.myapplication.TimePickerFragment.onTimeSet(TimePickerFragment.java:45)
                                                                                    at android.app.TimePickerDialog.onClick(TimePickerDialog.java:169)
                                                                                    at android.app.TimePickerDialog$1.onClick(TimePickerDialog.java:154)
                                                                                    at android.view.View.performClick(View.java:6256)
                                                                                    at android.view.View$PerformClick.run(View.java:24697)
                                                                                    at android.os.Handler.handleCallback(Handler.java:789)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                    at android.os.Looper.loop(Looper.java:164)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Hint for longest increasing subsequence? [on hold]

Given an unsorted array of integers, find the length of longest increasing subsequence.

For example,
Given [10, 9, 2, 5, 3, 7, 101, 18],
The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length.

Your algorithm should run in O(N^2) complexity.

Hints only.

I know this is dynammic programming. I’m thinking of defining dp[i][j] as the length of the longest subsequence between [i, j] (inclusive).

I’m not sure how to create a memo and derive a formula for dp[i][j] however. Any hints/if I am headed the right direction?

Best IP-based Geolocation from HTTP Request supporting Java Integration (Paid or Open Source)

Am researching options where I could leverage an existing service or a database where one can look up user’s location based on IP address provided in an HTTP Request to Restful Web Service (based on Java 1.7 and Spring Boot, preferably).

Open source APIs or paid APIs or a reference list that can be purchased. All options are open to be explored. Location response could be Lat/Long or city name or neighborhood name.