SlideShare a Scribd company logo
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 <input_file>");
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("NametHeight (cm)ttWeight (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("NametHeight (cm)ttHeight (kg)n");
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("NametHeight (in)ttWeight (lb)n");
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<Person> //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("%st%.1ftt%.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<Person> personList;
public PersonSet()
{
personList = new ArrayList<Person>();
}
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() + "n");
}
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<Person> sortedList = new ArrayList<Person>(personList);
Collections.sort(sortedList);
StringBuilder sb = new StringBuilder();
for(Person person: sortedList)
{
sb.append(person.toString() + "n");
}
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) + "tt" +
String.format("%.2f", weightInPounds) + "n");
}
return sb.toString();
}
}

More Related Content

Similar to Having a problem figuring out where my errors are- The code is not run.pdf (20)

PDF
can you add a delete button and a add button to the below program. j.pdf
sales88
 
PDF
AJUG April 2011 Raw hadoop example
Christopher Curtin
 
PDF
This is to test a balanced tree. I need help testing an unbalanced t.pdf
akaluza07
 
DOCX
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
whitneyleman54422
 
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
PDF
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
pnaran46
 
PDF
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
PDF
Pragmatic Real-World Scala
parag978978
 
PDF
Creating a Facebook Clone - Part XLVI - Transcript.pdf
ShaiAlmog1
 
PDF
Scala vs Java 8 in a Java 8 World
BTI360
 
PDF
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf
calderoncasto9163
 
PDF
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
aarokyaaqua
 
PDF
Understanding JavaScript Testing
jeresig
 
PDF
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
wasemanivytreenrco51
 
PDF
HELP IN JAVACreate a main method and use these input files to tes.pdf
fatoryoutlets
 
PDF
Stop Making Excuses and Start Testing Your JavaScript
Ryan Anklam
 
PDF
Better Software: introduction to good code
Giordano Scalzo
 
PDF
There is something wrong with my program-- (once I do a for view all t.pdf
aashienterprisesuk
 
PDF
Manual tecnic sergi_subirats
Sergi Subirats Cugat
 
PPTX
Migrating to JUnit 5
Rafael Winterhalter
 
can you add a delete button and a add button to the below program. j.pdf
sales88
 
AJUG April 2011 Raw hadoop example
Christopher Curtin
 
This is to test a balanced tree. I need help testing an unbalanced t.pdf
akaluza07
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
whitneyleman54422
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
pnaran46
 
Pragmatic Real-World Scala (short version)
Jonas Bonér
 
Pragmatic Real-World Scala
parag978978
 
Creating a Facebook Clone - Part XLVI - Transcript.pdf
ShaiAlmog1
 
Scala vs Java 8 in a Java 8 World
BTI360
 
JAVA...With N.E.T_B.E.A.N.S___________________________________.pdf
calderoncasto9163
 
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
aarokyaaqua
 
Understanding JavaScript Testing
jeresig
 
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
wasemanivytreenrco51
 
HELP IN JAVACreate a main method and use these input files to tes.pdf
fatoryoutlets
 
Stop Making Excuses and Start Testing Your JavaScript
Ryan Anklam
 
Better Software: introduction to good code
Giordano Scalzo
 
There is something wrong with my program-- (once I do a for view all t.pdf
aashienterprisesuk
 
Manual tecnic sergi_subirats
Sergi Subirats Cugat
 
Migrating to JUnit 5
Rafael Winterhalter
 

More from NicholasflqStewartl (20)

PDF
he amount of income taxesThe amount of income taxes A- the corporation.pdf
NicholasflqStewartl
 
PDF
Having a hard time with this one- Fill in the blanks with the follow.pdf
NicholasflqStewartl
 
PDF
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
NicholasflqStewartl
 
PDF
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
NicholasflqStewartl
 
PDF
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
NicholasflqStewartl
 
PDF
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
NicholasflqStewartl
 
PDF
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
NicholasflqStewartl
 
PDF
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
NicholasflqStewartl
 
PDF
Grouper Corporation is authorized to issue both preferred and commonst.pdf
NicholasflqStewartl
 
PDF
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
NicholasflqStewartl
 
PDF
Given the following XML fragment- what XPath expression would select a.pdf
NicholasflqStewartl
 
PDF
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
NicholasflqStewartl
 
PDF
Given the following for the Titan Company- the company began operation.pdf
NicholasflqStewartl
 
PDF
Given the following errors and class in Java- How are these errors fix.pdf
NicholasflqStewartl
 
PDF
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
NicholasflqStewartl
 
PDF
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
NicholasflqStewartl
 
PDF
Give concise and substantial answers by relating your answers to your.pdf
NicholasflqStewartl
 
PDF
Given a stream of strings- remove all empty strings- import java-uti.pdf
NicholasflqStewartl
 
PDF
Given an IntNode struct and the operating functions for a linked list-.pdf
NicholasflqStewartl
 
PDF
Given a string- does -oce- appear in the middle of the string- To defi.pdf
NicholasflqStewartl
 
he amount of income taxesThe amount of income taxes A- the corporation.pdf
NicholasflqStewartl
 
Having a hard time with this one- Fill in the blanks with the follow.pdf
NicholasflqStewartl
 
Harriet- Herm- and Ronde formed an S corporation called Innovet- Harri.pdf
NicholasflqStewartl
 
Hardworking Americans Should Not Be Living in Poverty has fallen to $6.pdf
NicholasflqStewartl
 
Government and Not for profit accounting 2022 CAFR (City of Anaheim) S.pdf
NicholasflqStewartl
 
Hammond Manufacturing Inc- was legally incorporated on January 2- 2020.pdf
NicholasflqStewartl
 
Hacer un programa en c++ que lea la frase y determine que caracteres s.pdf
NicholasflqStewartl
 
Given the regular expression- 0(01)0 (a) Construct and -NFA and draw i.pdf
NicholasflqStewartl
 
Grouper Corporation is authorized to issue both preferred and commonst.pdf
NicholasflqStewartl
 
Guessing or knowing the initial TCP sequence number (ISN) that a serve.pdf
NicholasflqStewartl
 
Given the following XML fragment- what XPath expression would select a.pdf
NicholasflqStewartl
 
Greener Pastures Corporation borrowed $1-100-000 on November 1- 2021-.pdf
NicholasflqStewartl
 
Given the following for the Titan Company- the company began operation.pdf
NicholasflqStewartl
 
Given the following errors and class in Java- How are these errors fix.pdf
NicholasflqStewartl
 
Given the following class in Java- public class ThreeTenDynArray-T- {.pdf
NicholasflqStewartl
 
Given Information #1- Period 1 is when Devah is working and earning mo.pdf
NicholasflqStewartl
 
Give concise and substantial answers by relating your answers to your.pdf
NicholasflqStewartl
 
Given a stream of strings- remove all empty strings- import java-uti.pdf
NicholasflqStewartl
 
Given an IntNode struct and the operating functions for a linked list-.pdf
NicholasflqStewartl
 
Given a string- does -oce- appear in the middle of the string- To defi.pdf
NicholasflqStewartl
 
Ad

Recently uploaded (20)

PPTX
Presentation: Climate Citizenship Digital Education
Karl Donert
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPTX
PPT on the Development of Education in the Victorian England
Beena E S
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
PPTX
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
PPTX
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
PPTX
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
PPTX
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
PPTX
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
Latest Features in Odoo 18 - Odoo slides
Celine George
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Presentation: Climate Citizenship Digital Education
Karl Donert
 
community health nursing question paper 2.pdf
Prince kumar
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
PPT on the Development of Education in the Victorian England
Beena E S
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Explorando Recursos do Summer '25: Dicas Essenciais - 02
Mauricio Alexandre Silva
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
CONVULSIVE DISORDERS: NURSING MANAGEMENT.pptx
PRADEEP ABOTHU
 
Optimizing Cancer Screening With MCED Technologies: From Science to Practical...
i3 Health
 
Gall bladder, Small intestine and Large intestine.pptx
rekhapositivity
 
How to Define Translation to Custom Module And Add a new language in Odoo 18
Celine George
 
SCHOOL-BASED SEXUAL HARASSMENT PREVENTION AND RESPONSE WORKSHOP
komlalokoe
 
How to Configure Storno Accounting in Odoo 18 Accounting
Celine George
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Latest Features in Odoo 18 - Odoo slides
Celine George
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
Accounting Skills Paper-I, Preparation of Vouchers
Dr. Sushil Bansode
 
Ad

Having a problem figuring out where my errors are- The code is not run.pdf

  • 1. 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 <input_file>"); 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("NametHeight (cm)ttWeight (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); }
  • 2. 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("NametHeight (cm)ttHeight (kg)n"); 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("NametHeight (in)ttWeight (lb)n"); 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)");
  • 3. System.out.println("------------------------------------------"); System.out.print(imperialSet.toString()); } } import java.util.ArrayList; public class Person implements Comparable<Person> //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("%st%.1ftt%.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; }
  • 4. 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()); } }
  • 5. 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<Person> personList; public PersonSet() { personList = new ArrayList<Person>(); } 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() + "n"); } return sb.toString(); } }
  • 6. 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<Person> sortedList = new ArrayList<Person>(personList); Collections.sort(sortedList); StringBuilder sb = new StringBuilder(); for(Person person: sortedList) { sb.append(person.toString() + "n"); } 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;
  • 7. 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) + "tt" + String.format("%.2f", weightInPounds) + "n"); } return sb.toString(); } }