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

Ok so when I run my program, it works and everything. The onlyproblem I am havin

ID: 3617499 • Letter: O

Question

Ok so when I run my program, it works and everything. The onlyproblem I am having is that the output is being printed after eachloop. How do I make the output print JUST once??

Code:

import java.io.*;

import java.util.*;

import javax.swing.*;

public class readfile

{

private Scanner x;

String m = JOptionPane.showInputDialog("enter name of file:");

public void openFile()

{

    try

    {

      x = new Scanner(new File(m));

    }

    catch(FileNotFoundException e)

    {

      JOptionPane.showMessageDialog(null, "Error 504: File Notfound");

    }

}

int a;

int tot;

ArrayList<Integer> arr = newArrayList<Integer>();

public void readFile()

{

    int place;

    while(x.hasNext())

    {

      place = 0;

      a = x.nextInt();

      arr.add(a);

    }

    JOptionPane.showMessageDialog(null,"This is thearray before the sort " + arr);

    tot = arr.size();

}

public void mergesort()
{

    mergesort(arr, 0, arr.size());

}

private static void mergesort(ArrayList<Integer> list,int start, int end)

{

    // a list of size 0 or 1 is merged

    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);

}
public void merge()
{
merge(arr,0,arr.size()/2,arr.size());
}
private static void merge(ArrayList<Integer> list, intstart, int mid, int end)

{

    // temp storage

    Integer[] temp = new Integer[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)

      {

        // add element from 2ndlist

        temp[loc++] =list.get(j++);

      }

      else

      {

        temp[loc++] =list.get(i++);

      }

    }

  

    // add remaining elements to list

    while(i < mid)

    {

      temp[loc++] = list.get(i++);

    }

    while(j < end)
    {
      temp[loc++] = list.get(j++);
    }

    // put elements back into list from temp

    for(int t = 0; t < temp.length; t++)

    {

      list.set(t + start, temp[t]);
    }
    JOptionPane.showMessageDialog(null,"This is thearray after the sort " + list);    //<--------------------------------OUTPUT
}

public void closeFile()

{
    x.close();

}
}

Explanation / Answer

please rate - thanks add another method--since it's recursive it's difficult class read{ //established name public static void main(String[] args) { //main args        readfile r = new readfile(); //establishsthat we will be reading a file     r.openFile(); //opens the file     r.readFile(); //reads it     r.closeFile(); //safely closes it     r.mergesort();     r.merge();       r.print(); } } --------------------------------- import java.io.*; import java.util.*; import javax.swing.*; public class readfile { private Scanner x; String m = JOptionPane.showInputDialog("enter name of file:"); public void openFile() {     try     {       x = new Scanner(new File(m));     }     catch(FileNotFoundException e)     {       JOptionPane.showMessageDialog(null, "Error 504: File Notfound");     } } int a; int tot; ArrayList arr = newArrayList(); public void readFile() {     int place;     while(x.hasNext())     {       place = 0;       a = x.nextInt();       arr.add(a);     }     JOptionPane.showMessageDialog(null,"This is thearray before the sort " + arr);     tot = arr.size(); } public void mergesort() {     mergesort(arr, 0, arr.size()); } private static void mergesort(ArrayList list,int start, int end) {     // a list of size 0 or 1 is merged     if(end-start
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