Reputation: 39
I did a post yesterday about this problem but was completely wrong with how i wrote so ive spent the night and this morning trying again and still cant get my head around how to do it.
What i'm trying to do is test my IsInRoom
method by getting the user to input two coordinates i cant figure out how to get the user input read by the IF statement and everytime is use r.IsInRoom
it comes up with an error saying "the method IsInRoom(Point)
in the type room is not applicable for the arguments double.
I have tried so many different ways of doing this and everytime i keep ending up back in the same place. if any one has any ideas that would be great im really new to java so would be great if you could give me some pointers on how to use these functions properly?
Ive made the changes to the If else statement but in getting this error
Exception in thread "main" java.lang.NullPointerException
at CurtisBaldwin.buildingconsole.Room.isInRoom(Room.java:85)
at aCurtisBaldwin.buildingconsole.Room.main(Room.java:115)
i think this is because ive not assigned the user input to the if statement? but im not sure how you do that
import java.awt.Point;
import java.io.Reader;
import javax.swing.JOptionPane;
import java.util.Random;
public class Room {
private static int X;
private static int Y;
private static Point P;
private int[] coords;
Random ranGen;
public Room(String rstr) {
StringSplitter S = new StringSplitter(rstr, " ");
coords = S.getIntegers();
}
public Point getRandomPoint (Random ranGen) {
return new Point(coords[0] +1 + ranGen.nextInt(coords[2] - coords[0] -2), (coords[1] +1 + ranGen.nextInt(coords[3] - coords[1] -2)));
}
public String toString() {
return "Room " + coords[0] + ", " + coords[1] + ", " + coords[2] + ", " + coords[3] + " Door " + coords[4] + ", " + coords[5];
}
public boolean IsInRoom(Point xy) {
P = new Point (X,Y);
return (xy.getX() > coords[0] && xy.getX() < coords[2] && xy.getY() > coords[1] && xy.getY() < coords[3]);
}
public static void main(String[] args) {
Room r = new Room("0 0 5 5 0 2"); // create room
System.out.println(r.toString()); // and print it
String userIn = JOptionPane.showInputDialog
(null, "please enter two coordinates one X and one Y separated by a space");
JOptionPane.showMessageDialog(null, r.IsInRoom(P) + userIn);
if (r.IsInRoom(P.getX() ) ) {
System.out.println( (int) P.getX() + "," + (int) P.getY() + "Is in room");
}
else if (r.IsInRoom(P.getY() ) ){
System.out.println((int) P.getX() + "," + (int) P.getY() + "Is in room");
else (r.IsInRoom(P.getXY() ) ) {
System.out.println ((int) P.getX() + "," + (int) P.getY() + "Is not in room");
}
}
}
Upvotes: 1
Views: 399
Reputation:
You did this mistake
P.getX()
and P.getY()
return double
but in IsInRoom
argument you are expecting Point
:)
another point is that till java 11 we didn't support argument in else block like you did eg:
else (r.IsInRoom(P.getXY() ) )
Upvotes: 1
Reputation: 9326
The error states what's wrong. Your isInRoom
method expects a Point
parameter, but in your if(r.isInRoom(P.getX()))
or if(r.isInRoom(P.getY()))
the getX()
and getY()
are double
types, which is why your get the error..
Simply changing your if-else construction to this:
if(r.isInRoom(P)){
System.out.println((int)P.getX() + "," + (int)P.getY() + "Is in room");
} else{
System.out.println((int)P.getX() + "," + (int)P.getY() + "Is not in room");
}
Is enough to fix it.
PS: Methods and variables are usually in camelCase, so IsInRoom
should be isInRoom
. It's not a requirement, but it's best practice for Java programming.
Upvotes: 1