Question: Having a problem figuring out where my errors are. The code is not running how I need it to so I added a few print
Having a problem figuring out where my errors are. The code is not running how I need it to so I added a few print statements to help me figure it out but to no avail. Any help would be greatly appreciated.
import java.io.File; import java.util.Scanner; import java.io.FileWriter; import java.io.IOException;
public class Main { public static void main(String[] args) { if(args.length != 1) { System.out.println("Usage: java Main "); return; } String inputFileName = args[0]; PersonSet orderedSet = new PersonOrderedSet(); PersonSet imperialSet = new PersonImperialSet(); try(Scanner sc = new Scanner(new File(inputFileName))) { if(sc.hasNextLine() && sc.nextLine().startsWith("Name\tHeight (cm)\t\tWeight (kg)")) { while(sc.hasNextLine()) { String line = sc.nextLine(); System.out.println("Line:" + line); String[] parts = line.split("\t"); if(parts.length == 3) { String name = parts[0]; double height; double weight; try { height = Double.parseDouble(parts[1]); weight = Double.parseDouble(parts[2]); Person person = new Person(name, height, weight); orderedSet.add(person); imperialSet.add(person); } catch (NumberFormatException e) { // skip this line } } } } } catch (IOException e) { System.out.println("An error occurred while reading the file."); e.printStackTrace(); return; } try(FileWriter writer = new FileWriter("hr_ordered_set_output.txt")) //write ordered set to file { writer.write("Name\tHeight (cm)\t\tHeight (kg) "); writer.write(orderedSet.toText()); } catch(IOException e) { System.out.println("An error occured while writing the ordered set output file."); e.printStackTrace(); return; } try(FileWriter writer = new FileWriter("hr_imperial_set_output.txt")) //write imperial set to file { writer.write("Name\tHeight (in)\t\tWeight (lb) "); writer.write(imperialSet.toText()); } catch(IOException e) { System.out.println("An error occured while writing the imperial set output file."); e.printStackTrace(); return; }
// Output the ordered data and the imperial data to the screen/console, nicely formatted System.out.println("Ordered data:"); System.out.println("Name Height (cm) Weight (kg)"); System.out.println("-------------------------------------------"); System.out.print(orderedSet.toString()); System.out.println("Imperial data:"); System.out.println("Name Height (in) Weight (lb)"); System.out.println("------------------------------------------"); System.out.print(imperialSet.toString()); }
}
import java.util.ArrayList;
public class Person implements Comparable //1 { private String name; private double height; //in centimeters private double weight; // in kilagrams public Person(String name, double height, double weight)//class that has constructors that initialize name, height, and weight { this.name = name; this.height = height; this.weight = weight; } @Override public String toString() //returns a string representation of the person's name, height, and weight { return String.format("%s\t%.1f\t\t%.1f", name, height, weight); } //Below are the getter and setter methods for name, height, and weight variables public String getName() { return name; } public void setName(String name) { this.name = name; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWeight() { return weight; } public void setWeight(double weight) { this.weight = weight; } @Override public boolean equals(Object o) { if(o == null) { return false; } if(o == this) { return true; } if(!(o instanceof Person)) { return false; } Person p = (Person) o; return this.name.equals(p.getName()) && Double.compare(this.height, p.getHeight()) == 0 && Double.compare(this.weight, p.getWeight()) == 0; } @Override public int compareTo(Person p) { return this.name.compareTo(p.getName()); } }
public interface PersonList //2 { public void add(Person person); //a. takes person as an argument and adds it to the list of persons public Person get(int index); //b. takes an integer index as an argument and returns the person object at that index in the list }
import java.util.ArrayList;
public class PersonSet implements PersonList //3 { protected ArrayList personList; public PersonSet() { personList = new ArrayList(); } public void add(Person person) { System.out.println("Adding person: " + person); if(!personList.contains(person)) //if person is not already in list, add it { personList.add(person); } } public Person get(int index) { return personList.get(index); //implement the get method } public String toText() { System.out.println("Converting PersonSet to text..."); StringBuilder sb = new StringBuilder(); for(Person person: personList) { sb.append(person.toString() + " "); } return sb.toString(); } }
import java.util.Collections; import java.util.ArrayList;
public class PersonOrderedSet extends PersonSet { public PersonOrderedSet() { super(); } @Override public void add(Person person) //add the person object { super.add(person); Collections.sort(personList); //sorts the list in alphabetical order by name } @Override public String toString() { ArrayList sortedList = new ArrayList(personList); Collections.sort(sortedList); StringBuilder sb = new StringBuilder(); for(Person person: sortedList) { sb.append(person.toString() + " "); } return sb.toString(); } }
import java.util.ArrayList;
public class PersonImperialSet extends PersonSet { public PersonImperialSet() { super(); } @Override public void add(Person person) { double heightInInches = person.getHeight() / 2.54; double weightInPounds = person.getWeight() / 0.45359237; Person imperialPerson = new Person(person.getName(), heightInInches, weightInPounds); super.add(imperialPerson); } public String toText() { StringBuilder sb = new StringBuilder(); for (Person person : personList) { double heightInInches = person.getHeight() / 2.54; double weightInPounds = person.getWeight() / 0.45359237; sb.append(person.getName() + "\t" + String.format("%.2f", heightInInches) + "\t\t" + String.format("%.2f", weightInPounds) + " "); } return sb.toString(); } }