Question
import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; class SeaTurtle { private String name; private String type; private int daysTracked; private double milesTraveled; public SeaTurtle(String
import java.io.File;
import java.io.FileNotFoundException;
import java.util.InputMismatchException;
import java.util.Scanner;
class SeaTurtle {
private String name;
private String type;
private int daysTracked;
private double milesTraveled;
public SeaTurtle(String name, String type, int daysTracked, double milesTraveled) {
this.daysTracked = daysTracked;
this.milesTraveled = milesTraveled;
this.name = name;
this.type = type;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public int getDaysTracked() {
return daysTracked;
}
public double getMilesTraveled() {
return milesTraveled;
}
public String threatsToSurvival() {
return null;
}
}
class Loggerhead extends SeaTurtle {
public Loggerhead(String name, int daysTracked, double milesTraveled) {
super(name,"Loggerhead", daysTracked, milesTraveled);
}
@Override
public String threatsToSurvival() {
return "Lost of nesting habitat";
}
}
class GreenTurtle extends SeaTurtle {
public GreenTurtle(String name, int daysTracked, double milesTraveled) {
super(name,"Green Turtle", daysTracked, milesTraveled);
}
@Override
public String threatsToSurvival() {
return "Comercial Havest for Egg & Food";
}
}
class Hawksbill extends SeaTurtle {
public Hawksbill(String name, int daysTracked, double milesTraveled) {
super(name, "Hawksbill", daysTracked, milesTraveled);
}
@Override
public String threatsToSurvival() {
return "Harvesting for their shell";
}
}
class Leatherback extends SeaTurtle {
public Leatherback(String name, int daysTracked, double milesTraveled) {
super(name,"Leatherback", daysTracked, milesTraveled);
}
@Override
public String threatsToSurvival() {
return "Plastic bag mistaken for Jellyfish";
}
}
public class Main {
public static void main(String [] args) {
// Create a polymophic array of Turtle
SeaTurtle [] turtles;
try {
//Open file to read
Scanner scanFile = new Scanner(new File("SeaTurtles.txt"));
//read array size from file
int size = Integer.parseInt(scanFile.next());
//Initialize array
turtles = new SeaTurtle[size];
//Read the provided file
int i=0;
while (scanFile.hasNextLine()){
String type = scanFile.next();
String name = scanFile.next();
double milesTraveled = Double.parseDouble(scanFile.next());
int daysTracked = Integer.parseInt(scanFile.next());
if(type.equalsIgnoreCase("lh"))
{
turtles[i] = new Loggerhead(name,daysTracked, milesTraveled);
}
if(type.equalsIgnoreCase("hawksbill"))
{
turtles[i] = new Hawksbill(name,daysTracked, milesTraveled);
}
if(type.equalsIgnoreCase("gt"))
{
turtles[i] = new GreenTurtle(name,daysTracked, milesTraveled);
}
i++;
}
//close the stream
scanFile.close();
//Print stats
System.out.println("------------------------------------------------------------------------------------------------------");
System.out.println(String.format("%10s %20s %15s %15s","Type","Name","daysTracked","milesTraveled","Benefit"));
System.out.println("------------------------------------------------------------------------------------------------------");
for (int j= 0; j
System.out.println(String.format("%10s %25s %10s %15s",turtles[j].getType(),turtles[j].getName(),String.format("%.2f",turtles[j].getDaysTracked()),turtles[j].getMilesTraveled(),turtles[j].threatsToSurvival()));
}
}
catch (FileNotFoundException ex)
{
System.err.println("File not found...");
}
catch (InputMismatchException ex)
{
System.err.println("Bad input occurs...");
}
}
}
Exception in thread "main" java.lang.NumberFormatException: For input string: "Bean"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:668)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at Main.main(Main.java:98)
Please help to fix this issues
SeaTurtles.txt look like bellow:
9 lh 304 1642 Bean hb 326 1431 Colada lb 374 11814 Lucaya lh 351 1684 Bortie lh 312 1374 Turtlette gt 336 913 Betsy lb 357 9862 Stella hb 324 1301 Kali gt 304 1908 Vida
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started