JAVA Netbeans What is wrong with this code? public static void main(String[] arg
ID: 3765935 • Letter: J
Question
JAVA Netbeans
What is wrong with this code?
public static void main(String[] args) throws Exception
{
/* creating a array */
myCourse[] mycourse1 = new myCourse[24];
myCourse[] mycourse2 = new myCourse[24];
for(int a = 0; a<24; a++)
{
mycourse1[a] = new myCourse();
}
/* reading the data from the text */
readLine(mycourse1);
/* displaying original array */
System.out.println( "original array: ");
for ( int a=0; a<24; a++)
System.out.println( " " + mycourse1[a].toString() );
/* writing the array to the bin */
System.out.println(" Writing the array to the bin");
mywriteToFile(mycourse1);
mycourse2 = myreadBinaryLines();
System.out.println( "New array: ");
for ( int a=0; a<24; a++)
System.out.println( " " + mycourse2[a].toString() );
System.out.println(" Writing the array to the bin");
mywriteCSV(mycourse2);
}
/* class readLine */
public static void readLine(myCourse[] c1) throws Exception
{
File myUnSorted = new File("myUnSorted.txt");
Scanner myInfile = new Scanner(myUnSorted);
System.out.println("reading");
for(int mycount = 0; mycount<24; mycount++)
{
System.out.println(mycount);
c1[mycount].setCampus(myInfile.next());
c1[mycount].setCourse(myInfile.next() +" "+ myInfile.next());
c1[mycount].setSection(myInfile.next());
c1[mycount].setCRN(myInfile.next());
c1[mycount].setCredits(myInfile.nextInt());
c1[mycount].setTime(myInfile.next());
c1[mycount].setDays(myInfile.next());
System.out.println(c[mycount].toString());
}
myInfile.close();
}
public static void mywriteToFile(myCourse[] mycourse1) throws Exception
{
/* declaring file objects */
FileOutputStream mynfile = null;
ObjectOutputStream myoutfile = null;
try
{
mynfile = new FileOutputStream("fall2014.bin");
myoutfile = new ObjectOutputStream(mynfile);
myoutfile.writeObject(mycourse1);
}
catch (IOException myE)
{
myE.printStackTrace();
}
finally
{
myoutfile.close();
}
}
public static myCourse[] myreadBinaryLines() throws Exception
{
myCourse[] c1 = null;
FileInputStream myCourseFile = null;
ObjectInputStream myInfile = null;
try
{
myCourseFile = new FileInputStream("fall2014.bin");
myInfile = new ObjectInputStream(myCourseFile);
c1 = (myCourse[])myInfile.readObject();
}
catch (IOException myE)
{
myE.printStackTrace();
}
finally
{
myInfile.close();
}
return c1;
}
public static void mywriteCSV (myCourse [] c1) throws Exception
{
java.io.File myCSV = new java.io.File("course.csv");
java.io.PrintWriter myoutfile = new java.io.PrintWriter(myCSV);
for(int a=0; a<24; a++)
{
myoutfile.write(c1[a].toCSVstring());
}
myoutfile.close();
}
class myCourse implements Serializable
{
private String campus;
private String course;
private String section;
private String crn;
private int credits;
private String time;
private String days;
myCourse()
{}
myCourse(String course, String section, String crn, int credits)
{
this.course = course;
this.section = section;
this.crn = crn;
this.credits = credits;
}
public void setCampus(String cmp)
{
this.campus = cmp;
}
public void setCourse(String crse)
{
this.course = crse;
}
public void setSection(String sect)
{
this.section = sect;
}
public void setCRN(String crn)
{
this.crn = crn;
}
public void setCredits(int cr)
{
this.credits = cr;
}
public void setTime(String tm)
{
this.time = tm;
}
public void setDays(String days)
{
this.days = days;
}
public String getCampus()
{
return campus;
}
public String getCourse()
{
return course;
}
public String getSection()
{
return section;
}
public String getCRN()
{
return crn;
}
public int getCredits()
{
return credits;
}
public String getTime()
{
return time;
}
public String getDays()
{
return days;
}
public int compareTo(myCourse other)
{
return this.crn.compareTo(other.getCRN());
}
public String toString()
{
return campus + " "
+ course + " "
+ section + " "
+ crn + " "
+ credits + " "
+ time + " "
+ days;
}
public String toCSVstring()
{
String record = campus + ","
+ course + ","
+ section + ","
+ crn + ","
+ credits + ","
+ time + ","
+ days + " ";
return record;
}
}
Explanation / Answer
Error Free code:
import java.io.*;
import java.util.*;
public class HelloWorld{
public static void main(String[] args) throws Exception
{
/* creating a array */
myCourse[] mycourse1 = new myCourse[24];
myCourse[] mycourse2 = new myCourse[24];
for(int a = 0; a<24; a++)
{
mycourse1[a] = new myCourse();
}
/* reading the data from the text */
readLine(mycourse1);
/* displaying original array */
System.out.println( "original array: ");
for ( int a=0; a<24; a++)
System.out.println( " " + mycourse1[a].toString() );
/* writing the array to the bin */
System.out.println(" Writing the array to the bin");
mywriteToFile(mycourse1);
mycourse2 = myreadBinaryLines();
System.out.println( "New array: ");
for ( int a=0; a<24; a++)
System.out.println( " " + mycourse2[a].toString() );
System.out.println(" Writing the array to the bin");
mywriteCSV(mycourse2);
}
/* class readLine */
public static void readLine(myCourse[] c1) throws Exception
{
File myUnSorted = new File("myUnSorted.txt");
Scanner myInfile = new Scanner(myUnSorted);
System.out.println("reading");
for(int mycount = 0; mycount<24; mycount++)
{
System.out.println(mycount);
c1[mycount].setCampus(myInfile.next());
c1[mycount].setCourse(myInfile.next() +" "+ myInfile.next());
c1[mycount].setSection(myInfile.next());
c1[mycount].setCRN(myInfile.next());
c1[mycount].setCredits(myInfile.nextInt());
c1[mycount].setTime(myInfile.next());
c1[mycount].setDays(myInfile.next());
System.out.println(c1[mycount].toString());
}
myInfile.close();
}
public static void mywriteToFile(myCourse[] mycourse1) throws Exception
{
/* declaring file objects */
FileOutputStream mynfile = null;
ObjectOutputStream myoutfile = null;
try
{
mynfile = new FileOutputStream("fall2014.bin");
myoutfile = new ObjectOutputStream(mynfile);
myoutfile.writeObject(mycourse1);
}
catch (IOException myE)
{
myE.printStackTrace();
}
finally
{
myoutfile.close();
}
}
public static myCourse[] myreadBinaryLines() throws Exception
{
myCourse[] c1 = null;
FileInputStream myCourseFile = null;
ObjectInputStream myInfile = null;
try
{
myCourseFile = new FileInputStream("fall2014.bin");
myInfile = new ObjectInputStream(myCourseFile);
c1 = (myCourse[])myInfile.readObject();
}
catch (IOException myE)
{
myE.printStackTrace();
}
finally
{
myInfile.close();
}
return c1;
}
public static void mywriteCSV (myCourse [] c1) throws Exception
{
java.io.File myCSV = new java.io.File("course.csv");
java.io.PrintWriter myoutfile = new java.io.PrintWriter(myCSV);
for(int a=0; a<24; a++)
{
myoutfile.write(c1[a].toCSVstring());
}
myoutfile.close();
}
class myCourse implements Serializable
{
private String campus;
private String course;
private String section;
private String crn;
private int credits;
private String time;
private String days;
myCourse()
{}
myCourse(String course, String section, String crn, int credits)
{
this.course = course;
this.section = section;
this.crn = crn;
this.credits = credits;
}
public void setCampus(String cmp)
{
this.campus = cmp;
}
public void setCourse(String crse)
{
this.course = crse;
}
public void setSection(String sect)
{
this.section = sect;
}
public void setCRN(String crn)
{
this.crn = crn;
}
public void setCredits(int cr)
{
this.credits = cr;
}
public void setTime(String tm)
{
this.time = tm;
}
public void setDays(String days)
{
this.days = days;
}
public String getCampus()
{
return campus;
}
public String getCourse()
{
return course;
}
public String getSection()
{
return section;
}
public String getCRN()
{
return crn;
}
public int getCredits()
{
return credits;
}
public String getTime()
{
return time;
}
public String getDays()
{
return days;
}
public int compareTo(myCourse other)
{
return this.crn.compareTo(other.getCRN());
}
public String toString()
{
return campus + " "
+ course + " "
+ section + " "
+ crn + " "
+ credits + " "
+ time + " "
+ days;
}
public String toCSVstring()
{
String record = campus + ","
+ course + ","
+ section + ","
+ crn + ","
+ credits + ","
+ time + ","
+ days + " ";
return record;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.