Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Ok. I am having two problems with this code. 1. It does not merge sort 2. It giv

ID: 3617454 • Letter: O

Question

Ok. I am having two problems with this code.

1. It does not merge sort
2. It gives a nullpointerexception (even though I try to catchit)

To run this code, have the two codes in the same window, but twodifferent panes.
Make a txt file with integers and use that as the input.

Code:

class read2{ //established name

public static void main(String[] args) { //main args

  

    readfile2 r = new readfile2(); //establishs that we will be reading a file

    r.openFile(); //opens the file

    r.readFile(); //reads it
    r.merge();
    r.mergesort();

    r.closeFile(); //safely closes it

}

}


Code 2:

import java.io.*;
import java.util.*;
import javax.swing.*;
public class readfile2
{
private Scanner x;
    String m = JOptionPane.showInputDialog("entername of file: ");
    public void openFile()
    {
      try
      {
        x = new Scanner(newFile(m));
      }
      catch(FileNotFoundException e)
      {
       JOptionPane.showMessageDialog(null, "File not found");
      }
    }
    int a;
    int tot;
    ArrayList<Integer> list = newArrayList<Integer>();
    public void readFile()
    {
      int place;
      while(x.hasNext())
      {
        place = 0;
        a = x.nextInt();
        list.add(a);
      }
     JOptionPane.showMessageDialog(null,"This is the array before thesort " + list);
      tot = list.size();
      JOptionPane.showMessageDialog(null,tot);
    }
  
    public void mergesort()
    {
      mergesort(list, 0, list.size());
    }
    private static voidmergesort(ArrayList<Integer> list, int start, int end)
    {
      // a list of size 0 or 1 ismerged
      if(end-start<2)
      {
        return;
      }
    
      // divide into 2 pieces
      int mid = (start+end)/2;
      mergesort(list, start, mid);
      mergesort(list, mid, end);
    
      // merge together
      merge(list, start, mid, end);
      JOptionPane.showMessageDialog(null,list);
    }
  
    public void merge()
    {
     merge(list,0,list.size()/2,list.size());
    }
    private static voidmerge(ArrayList<Integer> list, int start, int mid, intend)
    {
      // temp storage
      Integer[] temp = newInteger[end-start];
      int loc = 0;
      int i = start;
      int j = mid;
    
      // add elements to temp
      while(i < mid && j <end)
      {
       if(list.get(j).compareTo(list.get(i)) < 0)
        {
          // addelement from 2nd list
          temp[loc++]= list.get(j++);
        }
        else
        {
          temp[loc++]= list.get(i++);
        }
      }
      // add remaining elements tolist
      while(i < mid)
      {
        temp[loc++] =list.get(i++);
      }
      while(j < mid)
      {
        temp[loc++] =list.get(j++);
      }
    
      // put elements back into list fromtemp
      for(int t = 0; t < temp.length;t++)
      {
        list.set(t + start,temp[t]);
      }
    }
    public void closeFile()
    {
      x.close();
    }
}

Explanation / Answer

HELP please. Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote