Ok so I have been having this problem for a few assignments now. Iam doing java
ID: 3617400 • Letter: O
Question
Ok so I have been having this problem for a few assignments now. Iam doing java I/O. So my first program has no public static voidmain because that program reads a file and runs my program. Myother program has a public static void main and runs all of thevoids in the first program. The only thing is that even though myfirst program compiles with no error, my second program(which runsthe first one) can't execute some of the code which is in the firstprogram. Here they are:First Program:
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();
JOptionPane.showMessageDialog(null, tot);
}
public static void mergesort(ArrayList<Integer>list)
{
mergesort(list, 0, list.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);
}
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 < mid)
{
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]);
}
}
public void closeFile()
{
x.close();
}
My Second Program:
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();
}
}
To execute these programs have them in the samewindow but in different panes.
Explanation / Answer
please rate - thanks not debugged, but running 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(); } } ----------------------------------------- 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(); JOptionPane.showMessageDialog(null, tot); } 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-startRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.