Sorry for what is probley a simple question but how would i go about setting up a toString() method for an array list?
is it as simple as
points = new ArrayList<Point>();
public String toString() {
return points.toString();}
which does not seem to be working for me, or would it be more complex since it is a array list? Because for some reason when i execute mine like this its only printing the first value or object.
P.S I'm trying to return all the values that i have already added to my list.
More in detail
constructer
public Cloud() {
points = new ArrayList<Point>();
}
add point
public void addPoint(Point p) { // done
if (points.contains(p)) {
// if p is already in the list it does nothing
} else {
points.add(p); // if p was not in the list it adds it to the end
}
}
toString
public String toString() {
return points.toString();
}
main
public static void main(String[] args) {
Cloud cloud = new Cloud();
cloud.setDebug(false);
System.out.println("cloud.debug OFF");
System.out.println("initial cloud: " + cloud.toString());
Point p1 = new Point(3.0, 1.0);
cloud.addPoint(p1);
Point p2 = new Point(2.0, 2.0);
cloud.addPoint(p2);
Point p3 = new Point(1.5, 1.5);
cloud.addPoint(p3);
Point p4 = new Point(3.0, 0.0);
cloud.addPoint(p4);
System.out.println("final cloud: " + cloud);
This is just printing final cloud: (3.0,1.0) while it should be printing final cloud: [(3.0,1.0), (2.0,2.0), (1.5,1.5), (3.0,0.0)]
edit: Points class
public class Point {
private double x;
private double y;
public static final double EPSILON = 1e-5;
public static boolean debug = false;
public Point(double x, double y) {
this.x = x;
this.y = y; // Done sets the x,y private types to the x,y type provided
// in the ()
}
public Point() {
this(0.0, 0.0); // calls the point (double x,double) constructer with
// the given arguments
} // inturn setting x and y == 0.0
public double getX() {
return x; // returns the private value of x when called in the main
// method
} // so it can't be changed by the user
public double getY() {
return y; // return the private value of y when called in the main
// method so it can't be changed
} // by the user
public String toString() {
return "(" + x + "," + y + ")"; // done by teacher sets the toString
// method and implemetns it
}
public boolean equals(Point p) {
if (Math.abs(this.getX()) - Math.abs(p.x) < EPSILON) {
return true; // checks if x - p.x is less than epsilon which covers
// the round off
}
if (Math.abs(this.getY()) - Math.abs(p.y) < EPSILON) {
return true; // checks if y-p.y is less than epsilon which covers
// the round off
}
return false; // both these methods test for equality using epsilon,
// becuae we are dealing with
} // doubles, so roundof can occur
public boolean equals(Object obj) { // this was given to us
if (obj instanceof Point) {
Point p = (Point) obj; // This method overrides the object equals
// method and the calls
return equals(p); // the clas's equals(point) method
}
return false;
}
// TODO Implement Point.euclidDist
/**
*
* #param p
* #return Euclidean distance of this point to point p
*/
public double euclidDist(Point p) {
double distance = 0;
double firstvalue;
double secondvalue;
distance = Math.sqrt(((this.getX() - p.x) * (this.getX() - p.x)) // calculate
// the
// distance
+ ((this.getY() - p.y) * (this.getY() - p.y))); // between the
// two points
// firstvalue= Math.pow(this.getX()-p.x, 2);
// secondvalue= Math.pow(this.getY()-p.y, 2);
// distance = Math.sqrt(firstvalue + secondvalue);
return distance;
}
/**
* #param args
* : no args
*/
public static void main(String[] args) {
// test all methods
if (debug)
System.out.println("debug ON");
else
System.out.println("debug OFF");
System.out.println("EPSILON: " + Point.EPSILON);
Point origin = new Point();
Point p1 = new Point(0.0, 4.0);
Point p2 = new Point(3.0000001, 3.9999999);
Point p3 = new Point(3.0, 4.0);
Point p4 = new Point(0.0, 5.0);
Point p5 = new Point(12.0, 0.0);
System.out.println("origin: " + origin);
System.out.println("p1: " + p1);
System.out.println("p2: " + p2);
System.out.println("p3: " + p3);
System.out.println("p4: " + p4);
System.out.println("p5: " + p5);
if (p2.equals(p3))
System.out.println(p2 + " equals " + p3);
else
System.out.println(p2 + " does not equal " + p3);
System.out.println("Euclidean distance between " + origin + " and "
+ p1 + ": " + origin.euclidDist(p1));
System.out.println("Euclidean distance between " + p1 + " and " + p3
+ ": " + p1.euclidDist(p3));
System.out.println("Euclidean distance between " + p3 + " and "
+ origin + ": " + p3.euclidDist(origin));
System.out.println("Euclidean distance between " + p4 + " and " + p5
+ ": " + p4.euclidDist(p5));
}
}
You can only create toString() method overrides in classes of yours, not in other classes that you're not overriding. ArrayList already has a valid toString() method that is useful. You will just need to make sure that the items held by the List are from a class that also has a valid toString() method.
Note you state:
which does not seem to be working for me...
Because for some reason when i execute mine like this its only printing the first value or object.
This suggests that you don't have a toString() problem, but that you actually have another completely different problem with your program, that you're not adding objects to the list correctly. You need to do more debugging and show more pertinent code.
Edit
I'm guessing that your Point class's contains(...) method is erroneous, that it is returning true when it should be returning false. Please show us the Point class.
Edit 3 (deleted edit 2)
Your equals is wrong:
This is OK:
public boolean equals(Object obj) { // this was given to us
if (obj instanceof Point) {
Point p = (Point) obj; // This method overrides the object equals
// method and the calls
return equals(p); // the clas's equals(point) method
}
return false;
}
But here, you return equals if either x's or y's closely match and that shouldn't be. You should only return true if BOTH closely match:
public boolean equals(Point p) {
if (Math.abs(this.getX()) - Math.abs(p.x) < EPSILON) {
return true; // checks if x - p.x is less than epsilon which covers
// the round off
}
if (Math.abs(this.getY()) - Math.abs(p.y) < EPSILON) {
return true; // checks if y-p.y is less than epsilon which covers
// the round off
}
return false; // both these methods test for equality using epsilon,
// becuae we are dealing with
} // doubles, so roundof can occur
Also you're using Math.abs(...) incorrectly. It should go around the subtraction statement, not around each variable.
Related
For an inheritance practice assignment, I have a class Square which extends rectangle, and here are the relevant methods:
public class Square extends Rectangle {
private Point p;
private int sideLength;
public Square(Point p, int sideLength){
super(p);
this.sideLength = sideLength;
}
public String toString(){
return getClass().getName() + "\nCenter point: (" + p.getX() + ',' + p.getY() + ") \nSide Length: " + sideLength + " \nArea: " + this.getArea() + " \nPerimeter: " + this.getPerimeter();
}
}
there's also a getArea() and getPerimeter() method but neither of those are causing this issue.
I have a separate class to test this one:
public class SquareTest {
public static void main(String[] args){
ArrayList<Square> squareList = new ArrayList<>();
Point p1 = new Point(1, 1);
Point p2 = new Point(1, 2);
Point p3 = new Point(4, 1);
Point p4 = new Point(2, 3);
Point p5 = new Point(5, 4);
Square one = new Square(p1, 1);
Square two = new Square(p2, 2);
Square three = new Square(p3, 4);
Square four = new Square(p4, 4);
Square five = new Square(p5,5);
squareList.add(one);
squareList.add(two);
squareList.add(three);
squareList.add(four);
squareList.add(five);
for (Square each : squareList){
System.out.println(each.toString());
System.out.println();
}
}
}
What it's supposed to do is print out the toString() result for each of the five square objects, where one square looks like this:
Square
Center point: (1,1)
Side Length: 1
Area: 1
Perimeter: 1
but instead i get the following runtime error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.awt.Point.getX()" because "this.p" is null
at Square.toString(Square.java:21)
at SquareTest.main(SquareTest.java:23)
I'm confused as to why it thinks this.p is null when I've clearly instantiated five Point objects. Any ideas?
NullPointerException was caused because you haven't initialized the p in Square class. Yu have just passed it to the Rectangle constructor so it is the p in Rectangle class which is getting initialized.
Since you are not using the point object in your Square rather than just passing it to the super class, there is no need to declare a Point object in the Square class. If your Rectangle class has a getter method for Point p then, you could use that in the Square class.
Assuming that Rectangle class has a getter method for p, You could refactor your Square class to :
public class Square extends Rectangle {
private int sideLength;
public Square(Point p, int sideLength){
super(p);
this.sideLength = sideLength;
}
public String toString(){
return getClass().getName()
+ "\nCenter point: (" + getP().getX() + ',' + getP().getY()
+ ") \nSide Length: " + sideLength
+ " \nArea: " + this.getArea()
+ " \nPerimeter: " + this.getPerimeter();
}
}
I want to make it work to where I run ComplexTest.class and then in that class it runs Complex.class. I'm pretty new at java I have no idea what's wrong. Not sure why the compiler expects to see .class and a semi colon where it thinks they should be.
Main class
public class ComplexTest
{
//private final double re; // the real part
//private final double im; // the imaginary part
public static void main(String[] paramArrayOfString)
{
CreateObjs();
PrintHeader1();
PrintHeader2();
// invoke and Initialize a Complex object
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
Comp.JunctionBox(CompA, CompB);
// multiply.printQuestionResult();
}
public static void CreateObjs()
{
Complex CompA = new Complex(9.5D, 7.7D);
Complex CompB = new Complex(1.2D, 3.1D);
}
public static void PrintHeader1()
{
System.out.printf(" A complex number in the \n form (x, y) is equal to \n x + yi, where i is \n square root of -1.\n");
}
public static void PrintHeader2()
{
System.out.printf("\n *-Complex numbers calculations-*");
}
}
2nd class
/******************************************************************************
* Data type for complex numbers.
*
* The data type is "imagmutable" so once you create and initialize
* a Complex object, you cannot change it. The "final" keyword
* when declaring re and imag enforces this rule, making it a
* compile-timage error to change the .re or .imag fields after
* they've been initialized.
*
* % java Complex
* a = 5.0 + 6.0i
* b = -3.0 + 4.0i
* b + a = 2.0 + 10.0i
* a - b = 8.0 + 2.0i
* a * b = -39.0 + 2.0i
* a / b = 0.36 - 1.52i
******************************************************************************/
public class Complex {
// Constants (final)
private final double re; // the real part
private final double imag; // the imaginaryinary part
// Variables
public double product;
// create a new object with the given real and imaginaryinary parts
public Complex(double real, double imaginary) {
re = real;
imag = imaginary;
}
// return a string representation of the invoking Complex object
public String toString() {
if (imag == 0) return "<" + re + ">";
if (re == 0) return "<" + imag + ">";
if (imag < 0) return "<" + re + " - " + (-imag) + ">";
return "<" + re + ", " + imag + ">";// + "i";
}
// return a new Complex object whose value is (this + b)
public Complex plus(Complex b) {
Complex a = this; // invoking object
double real = a.re + b.re;
double imaginary = a.imag + b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this - b)
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imaginary = a.imag - b.imag;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is (this * b)
public Complex timages(Complex b) {
Complex a = this;
double real = a.re * b.re - a.imag * b.imag;
double imaginary = a.re * b.imag + a.imag * b.re;
return new Complex(real, imaginary);
}
// return a new Complex object whose value is the reciprocal of this
public Complex reciprocal() {
double scale = re*re + imag*imag;
return new Complex(re / scale, -imag / scale);
}
// return the real or imaginaryinary part
public double re() { return re; }
public double imag() { return imag; }
// return a / b
public Complex divides(Complex b) {
Complex a = this;
return a.timages(b.reciprocal());
}
// sample client for testing
public static void main(String[] args) {
Complex a = new Complex(9.5, 7.7);
Complex b = new Complex(1.2, 3.1);
System.out.printf("a = %s\n", a);
System.out.println("b = " + b);
System.out.println("a + b = " + a.plus(b));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.timages(b));
System.out.println("a / b = " + a.divides(b));
}
}
Compiler/Syntax errors:
ComplexTest.java:15: error: constructor Complex in class Complex cannot be applied to given types;
Complex Comp = new Complex(); // Invokes Complex constructor (pg 315)
^
required: double,double
found: no arguments
reason: actual and formal argument lists differ in length
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompA
location: class ComplexTest
ComplexTest.java:16: error: cannot find symbol
Comp.JunctionBox(CompA, CompB);
^
symbol: variable CompB
location: class ComplexTest
3 errors
EDIT1: Fixed the junk class, updated the errors code block. I knew that the junk class was a problem.
EDIT2: I need more help, I am making more errors trying to fix the ones I already have.
You have a "junk" class declaration messing the file up.
public class JunctionBox() {
}
...is not a valid class declaration to begin with (the brackets should not be there), and you should only have a single public class declaration - with the class named as the file - in each Java file.
Removing that class declaration would make the file compile correctly.
The problem is due to having the concatenation of double and string you have to convert them first to string in every way possible
for example in the following way
change
if (imag == 0) return "<" + re + ">"
to
if (imag == 0) return "<" + String.valueOf(re) + ">"
Some errors are:
1) in the main method of ComplexTest you invoke the empty Constructor Complex() that you have not defined yet.
2) in the next line of code you use CompA and CompB object, but you have not defined them.
3) In the second file you first declare JunctionBox Class, but you put a main method method in a secodary class of the same file.
Correct first the above errors and the update you question.
I read in a book that when you change the value of a method parameter that's a boolean or other basic datatype within the method it only is changed within the method and remains the same outside. I want to know if there is some way for me to actually change it within the method. For example:
public class Change {
void convert(boolean x, boolean y, boolean z) { //i want to set x,y, and z to false in this
x = false;
y = false;
z = false;
}
}
//Now in my main class:
public static void main(String[] args) {
boolean part1 = true;
boolean part2 = true;
boolean part3 = true;
System.out.println(part1 + " " + part2 + " " + part3);
Change myChange = new Change();
myChange.convert(part1,part2,part3);
System.out.println(part1 + " " + part2 + " " + part3);
}
EDIT1: These answers were good but not quite what i want to acheive. I want to put in part1, part2, and part3 when i call the method than i want them to be set to false within the method. The specific reason i asked this question was because im trying to code a battleship and i have a subroutine class with a method that when its called it checks if a ship has been sunk. If the there was a sink than the method will set a ton of boolean variables to false.
EDIT2: Just to clarify, I want something like this:
void convert(thing1,thing2,thing3,thing4) {
//some code here that sets thing1,thing2,thing3, and thing4 to false
}
// than in main:
boolean test1 = true;
boolean test2 = true;
boolean test3 = true;
boolean test4 = true;
convert(test1,test2,test3,test4);
System.out.println(test1 + " " + test2 + "....");
//and that should print out false, false, false, false
You can do it with this methodology
// these are called instance variables
private boolean x = false;
private boolean y = false;
private boolean z = false;
// this is a setter
public static void changeBool(boolean x, boolean y, boolean z) {
this.x = x;
this.y = y;
this.z = z;
}
Call the method like this
changeBool(true, true, false);
The values for x, y, and z are now changed.
This is a common problem in Java - pass-by-value vs. pass-by-reference. Java is always pass-by-value, where you're thinking of it as pass-by-reference.
Like #Rafael has said, you need to use instance variables to do what you want. I've gone a bit further and edited your source code to do what you want:
public class Change {
boolean part1;
boolean part2;
boolean part3;
Change(boolean x, boolean y, boolean z) {
part1 = x;
part2 = y;
part3 = z;
}
void convert(boolean x, boolean y, boolean z) { //this now sets the class variables to whatever you pass into the method
part1 = x;
part2 = y;
part3 = z;
}
// Now in my main class:
public static void main(String[] args) {
Change myChange = new Change(true, true, true);
System.out.println(myChange.part1 + " " + myChange.part2 + " "
+ myChange.part3);
myChange.convert(false, false, false);
System.out.println(myChange.part1 + " " + myChange.part2 + " "
+ myChange.part3);
}
}
The null-pointer exception error occurs in my toString method. I'm at a loss as to why. The error can occur through multiple ways. Most commonly, an object's reference is declared but the object itself remains uncreated. I've declared and created (is initialized the right word?) Mycircle circle1 = new Mycircle (); and Mypoint center = new Mypoint ();
I suspected that I hadn't initialized any of my fields when I invoked my getter methods, but that's not true. The setter methods work cleanly -- I've been successful in inputting values. Doesn't that imply that my getter methods can access some non-null value.
import java.util.Scanner;
public class MyCircle {
private Mypoint center;
private double radius;
Scanner input = new Scanner (System.in);
public MyCircle() {
radius = 0.0;
Mypoint center = new Mypoint ();
System.out.println("Enter coordinates here");
center.setCoordinateX(input.nextDouble());
center.setCoordinateY(input.nextDouble());
}
public String toString(MyCircle object) {
return "Circle # " + "(" + object.center.getCoordinateX() + "," +
object.center.getCoordinateY() + ")" + ". Its radius is " +
object.radius;
}
public double calcArea(MyCircle object) {
double area = Math.pow(object.radius, 2) * Math.PI;
return area;
}
public static void main (String[]args) {
MyCircle circle1 = new MyCircle ();
circle1.radius = 3.0;
System.out.println(circle1.calcArea(circle1));
System.out.println(circle1.toString(circle1));
}
}
class Mypoint {
private double posX;
private double posY;
public void setCoordinateX(double x) {
posX = x;
}
public void setCoordinateY(double y) {
posY = y;
}
public double getCoordinateX() {
return posX;
}
public double getCoordinateY() {
return posY;
}
}
In your MyCircle constructor, you're creating a local variable called center here:
Mypoint center = new Mypoint ();
What you probably want is to initialize the instance member:
center = new Mypoint ();
Your code doesn't make much sense, presumably this
public String toString(MyCircle object)
{
return "Circle # " + "(" + object.center.getCoordinateX() + ","
+ object.center.getCoordinateY() + ")"
+ ". Its radius is " + object.radius;
}
Should be
#Override
public String toString() {
return "Circle # (" + center.getCoordinateX() + ","
+ center.getCoordinateY()
+ "). Its radius is " + radius;
}
Then let's fix your constructor, use this so you know you're updating the instance field (instead of shadowing it) -
public MyCircle() {
this.radius = 0.0;
this.center = new Mypoint(); // <-- this.center
System.out.println("Enter coordinates here");
this.center.setCoordinateX(input.nextDouble());
this.center.setCoordinateY(input.nextDouble());
}
First of all, your toString() method is untypical. You want a Circle#toString() with no argument and produce a print representaton of yourself (i.e. this instead of object=).
Your toString method can fail if:
object is null
object.center is null
object.center.getCoordinateX() or getCoordinateY() itself throws exception.
The later case would be visible in the stacktrace, the other two cases all show the same line number as the cause.
It looks like the second case is your problem (as you fill a local variable and not the field of MyCircle).
BTW: using a input scanner in a constructor is a horrible layering violation. You should seperate input/output/user interaction logic from the geometric (circly, point) classes.
There are many things wrong in your code beside what #Mike pointed out.
Getting input from the user within the constructor is bad. You should get the input in the main method, and pass it to the constructor. In addition, you don't initialize the radius. It remains 0.0.
The constructor call should look like this (the MyPoint object passed to the constructor should be initialized prior to calling the constructor) :
MyCircle circle1 = new MyCircle (center, radius);
The constructor should look like this :
public MyCircle(MyPoint center, double radius)
{
this.radius = 0.0;
this.center = center;
}
The toString and calcArea methods don't need a parameter. They should operate on the current object (this) :
public String toString()
{
return "Circle # " + "(" + this.center.getCoordinateX() + "," + this.center.getCoordinateY() + ")" + ". Its radius is " + this.radius;
}
I'm fairly new at this programming, so please do bear with me.
In teaching myself I'm attempting to write a Batteleships game; not OO at the moment, but rather procedural - little steps at a time.
I have a method to read the coordinates to fire at, these coordinates I want to then validate to make sure that, well, they're valid.
There is one method that checks that they are numbers and within the correct range, the other method is 'supposed' to check through what has already been entered.
The issue I'm finding is that I'm not breaking out of the do while loop to progress, the while bit is using logical OR on the two aforementioned methods. In writing these methods, they both do what they're supposed to do, well I'm not entirely sure about the method that checks whether a coordinate has already been fired at.
Some pointers would be really appreciated (on any aspect of it), thanks!
Code:
public static String inputCoords(List<String> coordsFired){
Scanner sc = new Scanner(System.in);
//Console c = System.console();
String coordsEntered;
do {
System.out.println("in do\\while");
System.out.println("Enter coordinates as 'x, y': ");
coordsEntered = sc.nextLine();
System.out.println("end of do\\while loop");
} while(!validateCoords(coordsEntered)
|| !coordsFiredAt(coordsEntered, coordsFired));
coordsFired.add(coordsEntered);
System.out.println("contents of List<String> coordsFired" + coordsFired);
return coordsEntered;
}
public static boolean validateCoords(String coordsEntered){
boolean results;
int x, y;
String strx = splitCoordsString(coordsEntered, 'x');
String stry = splitCoordsString(coordsEntered, 'y');
if (numericCheckCoordsFire(strx) && numericCheckCoordsFire(stry)) {
x = Integer.parseInt(strx);
y = Integer.parseInt(stry);
if (x > 25 || y > 25) {
results = false;
System.out.println("The dimensions of the board are 25 x 25, 'x,y' entered must be less than this. You entered '" + strx + "' for x and '" + stry + "' for y.");
} else {
results = true;
}
} else {
results = false;
System.out.println("Coords are supposed to be numbers... You entered '" + strx + "' for x and '" + stry + "' for y.");
}
System.out.println(results);
return results;
}
public static boolean coordsFiredAt(String coordsEntered, List<String> coordsFired) {
boolean results = false;
// go through each item in the list and compare against coordsEntered
for (String s : coordsFired) {
System.out.println("in for loop, printing iterated var" + s);
if (s.equals(coordsEntered)) {
// put these matched coordsFire into listHit
results = false;
} else {
System.out.println("already fired at " + coordsEntered);
results = true;
}
}
return results;
}
I propose you add OOP a little and create a class for Coords:
public class Coords {
private final int x;
private final int y;
public Coords(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
/**
* This method is used for Coords comparison
*/
#Override
public boolean equals(Object o) {
Coords coords = (Coords) o;
return y == coords.y && coords.x ==x;
}
/**
* This method is used to output coords.
*/
#Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
So you code will look somethink like this:
public static Coords inputCoords(List<Coords> coordsFired) {
Scanner sc = new Scanner(System.in);
//Console c = System.console();
Coords coords;
do {
System.out.println("in do\\while");
System.out.println("Enter coordinates as 'x, y': ");
String coordsEntered = sc.nextLine();
coords = parseCoords(coordsEntered);
System.out.println("end of do\\while loop");
} while (coords == null || !areCoordsValid(coords) || !areCoordsNotFired(coords, coordsFired));
coordsFired.add(coords);
System.out.println("contents of List<String> coordsFired" + coordsFired);
return coords;
}
public static boolean areCoordsValid(Coords coords) {
boolean result = true;
if (coords.getX() > 25 || coords.getY() > 25) { // I think you also need to validate that it is possible values
result = false;
System.out.println("The dimensions of the board are 25 x 25, 'x,y' entered must be less than this. " +
"You entered '" + coords.getX() + "' for x and '" + coords.getY() + "' for y.");
}
return result;
}
public static boolean areCoordsNotFired(Coords coords, List<Coords> firedCoards) {
boolean result = true;
if (firedCoards.contains(coords)) {
result = false;
System.out.println("You already fired at " + coords.getX() + "," + coords.getY());
}
return result;
}
public static Coords parseCoords(String coordsEntered) {
Coords coords = null;
try {
String[] splittedCoords = coordsEntered.split(","); // Method splits values by comma. It should return an array of Strings with x value at the first element and y at the second one;
if (splittedCoords.length == 2) {
String x = splittedCoords[0].trim(); // Method removes all spaces at the beginning and ending of a passed String
String y = splittedCoords[1].trim();
coords = new Coords(Integer.parseInt(x), Integer.parseInt(y)); //Creates new instance of Coords class. x and y are passed as constructor params.
} else {
System.out.println("Format for coords is wrong. You entered '" + coordsEntered + "'.");
}
} catch (NumberFormatException e) { // Integer.parseInt throws an exception if the string does not contain parsable integer.
// We catch an exception and handle it by writing a message
System.out.println("Coords are supposed to be numbers... You entered '" + coordsEntered + "'.");
}
return coords;
}
Also Set is more applicable in this case. Set contains no duplicate elements and Set.contains() method works faster then List.contains(). But if you want to use Set you should implement both equals() and hashCode() methods.
You want to loop if the coords are invalid or already fired.
So shouldn't the while condition be:
while(!validateCoords(coordsEntered)
|| coordsFiredAt(coordsEntered, coordsFired))