This is in Java, I don\'t know what I\'m doing wrong. 1. I\'m doing something wr
ID: 3787314 • Letter: T
Question
This is in Java, I don't know what I'm doing wrong.
1. I'm doing something wrong with Integer.parseInt
package writefiles;
import java.io.*;
public class Courses implements Serializable {
private String title;
private String prefixNumber;
private String timeDay;
private int sectionNumber;
private int creditHours;
public Courses(String title, String prefixNumber, String timeDay, int sectionNumber, int creditHours){
this.title = title;
this.prefixNumber = prefixNumber;
this.timeDay = timeDay;
this.sectionNumber = sectionNumber;
this.creditHours = creditHours;
}
public Courses() {
setTitle("");
setPrefixNumber("");
setTimeDay("");
setSectionNumber(0);
setCreditHours(0);
}
public String getTitle() {
return title;
}
public String getPrefixNumber() {
return prefixNumber;
}
public String getTimeDay() {
return timeDay;
}
public int getSectionNumber() {
return sectionNumber;
}
public int getCreditHours() {
return creditHours;
}
public void setTitle(String title) {
this.title = title;
}
public void setPrefixNumber(String prefixNumber) {
this.prefixNumber = prefixNumber;
}
public void setTimeDay(String timeDay) {
this.timeDay = timeDay;
}
public void setSectionNumber(int sectionNumber) {
this.sectionNumber = sectionNumber;
}
public void setCreditHours(int creditHours) {
this.creditHours = creditHours;
}
@Override
public String toString() {
return "Courses{" + "title=" + title + ", prefixNumber=" + prefixNumber + ", timeDay=" + timeDay + ", sectionNumber=" + sectionNumber + ", creditHours=" + creditHours + '}';
}
}
***********************************************************************************************
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package writefiles;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class WriteFiles {
private static Formatter output;
private static Scanner input;
public static void main(String[] args) {
ArrayList coursesList = new ArrayList();
writeTextFile();
readTextFile(coursesList);
for (Courses c: coursesList){
System.out.println(c.toString());
}
writeSerFile(coursesList);
readSerFile();
}
public static void writeTextFile(){
try
{
output = new Formatter("courses.txt");
output.format("%s;%d;%.2f%n","Python", "INSY 3300", "560-650 MW", 1, 3);
output.format("%s;%d;%.2f%n","Networking", "INSY 3303", "530-650 TR", 1, 3);
output.format("%s;%d;%.2f%n","DBMS", "INSY 3304", "900-950 MWF", 1, 3);
output.format("%s;%d;%.2f%n","Analysis&Design", "INSY 3305", "700-820 TR", 1, 3);
output.format("%s;%d;%.2f%n","Java I", "INSY 4305", "700-820 TR", 1, 3);
output.format("%s;%d;%.2f%n","Java II", "INSY 4306", "530-650 TR", 1, 3);
output.format("%s;%d;%.2f%n","Mobile App", "INSY 4308", "200-320 TR", 1, 3);
output.format("%s;%d;%.2f%n","Web Development", "INSY 4315", "1000-1050 MWF", 1, 3);
output.format("%s;%d;%.2f%n","Resource Management", "INSY 4325", "100-220 TR", 1, 3);
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void readTextFile(ArrayList coursesList){
String line;
String values[];
try{
input = new Scanner(getFile());
while(input.hasNext())
{
line = input.nextLine();
values = line.split(";");
coursesList.add(new Courses(values[0],
coursesList.add(new Courses(values[1],
coursesList.add(new Courses(values[2],
Integer.parseInt(values[3],
Integer.parseInt(values[4])));
}
}
catch (IOException ioe){
ioe.printStackTrace();
}
}
public static void writeSerFile(ArrayListcoursesList){
ObjectOutputStream output;
try{
output = new ObjectOutputStream(new FileOutputStream("courses.ser"));
for (Courses c: coursesList){
output.writeObject(c);
}
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void readSerFile(){
ObjectInputStream input;
Courses obj;
try{
input = new ObjectInputStream(new FileInputStream(getFile()));
while(true){
obj = (Courses)input.readObject();
System.out.println(obj.toString());
}
}
catch(EOFException eof){
}
catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}
public static File getFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
File fileName = fileChooser.getSelectedFile();
if ((fileName == null) || (fileName.getName().equals(""))){
JOptionPane.showMessageDialog(null, "Invalid Option",
"Invalid Option", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
return fileName;
}
}
Explanation / Answer
Try the following changes in this block of code:
try{
input = new Scanner(getFile());
while(input.hasNext())
{
line = input.nextLine();
values = line.split(";");
coursesList.add(new Courses(values[0],values[1],values[2],Integer.parseInt(values[3]),Integer.parseInt(values[4]));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.