0

How would you code direction to bearing in Java? For example: Enter compass direction: N Enter compass angle: 30 Enter compass direction: W The code should output 330. My code so far isn't outputting anything (I have not completely finished because I would like to find out what I am doing wrong first)

{System.out.println("Do you have either - "
            + "1: A bearing OR 2: A direction? (Enter number 1 or 2)");}
    int number;
    Unit2LastAssignment In1 = null;
    number = In1.getInt();
    if (number == 2)
    {System.out.println("Enter Compass Direction: ");}
    String direction1;
    Unit2LastAssignment In2 = null;
    direction1 = In2.getString();
    {System.out.println("Enter Compass Angle: ");}
    int angle;
    Unit2LastAssignment In3 = null;
    angle = In3.getInt();
    {System.out.println("Enter Compass Direction: ");}
    String direction2;
    Unit2LastAssignment In4 = null;
    direction2 = In4.getString();

    {if (direction1 == "N")
    {System.out.println("360");}
    else if (direction1 == "E")
    {System.out.println("90");}
    else if (direction1 == "S")
    {System.out.println("180");}
    else if (direction1 == "W")
    {System.out.println("270");}
    else if (direction1 == "NE")
    {System.out.println("45");}
    else if (direction1 == "SE")
    {System.out.println("135");}
    else if (direction1 == "SW")
    {System.out.println("225");}
    else if (direction1 == "NW")
    {System.out.println("315");}

        else if (direction1 == "N" && direction2 == "E" && angle > 0 && angle <     45)
        {System.out.println(angle);}
    else if (direction1 == "E" && direction2 == "N" && angle > 0 && angle <     45)
    {int angle1;
    angle1 = 180 - angle;
    {System.out.println(angle1);}}
SBSB
  • 1
  • 1
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 18 '15 at 01:01
  • So instead of `if (direction1 == "N")`, do `if ("N".equals(direction`))`. Also work on your code formatting since as written your code is very hard for us to read, making it hard for you and us to debug. – Hovercraft Full Of Eels Nov 18 '15 at 01:01
  • Thank you very much! Sorry about that, this is my first question here and also my first time coding. Also, do you know how I should go about coding it when the user inputs everything on the same line? Eg. N30W (all on the same line) - like how would I separate that? @HovercraftFullOfEels – SBSB Nov 18 '15 at 01:07

0 Answers0