0

Hi guys just wondering if you could help me out, could you tell me what i am doing wrong here, what i am trying to do is add 1 if r is typed and subtract 1 if L is typed for both the lower and upper cases. but the position keeps returning the original. please help!!

    int position = 0;
    System.out.print("move: ");
    String car = Global.keyboard.nextLine();
    if (car == "r/")
        position =  + 1;
    if (car == "R")
        position = +1;
    if (car == "l")
        position =  -1;
    if (car == "L")
        position = -1;
        System.out.print(position);
JustDanyul
  • 13,813
  • 7
  • 53
  • 71
  • 6
    Use `String.equals` to compare Strings – Reimeus Aug 27 '13 at 15:07
  • 1
    http://stackoverflow.com/questions/767372/java-string-equals-versus – kiheru Aug 27 '13 at 15:07
  • 1
    Most of the answers mention that `position = +1;` should be `position += 1;`, yet no-one mentioned the preferred way of `position++;`. – Bernhard Barker Aug 27 '13 at 15:17
  • Also, what happens when you don't press `r`, `R`, `l` or `L`? Did you notice that you execute all of the `if`s regardless of which key was pressed? I've taken that into account, should you need another point of view. – Fritz Aug 27 '13 at 15:28

5 Answers5

2

use:

int position = 0;
System.out.print("move: ");
String car = Global.keyboard.nextLine();
if (car.equals("r"))
    position += 1;
if (car.equals("R"))
    position += 1;
if (car.equals("l"))
    position -= 1;
if (car.equals("L"))
    position -= 1;
    System.out.print(position);
codeMan
  • 5,730
  • 3
  • 27
  • 51
0

If you want a 1 liner, use this:

position += car.equalsIgnoreCase("r") ? 1 : car.equalsIgnoreCase("l") ? -1 : 0;
Josh M
  • 11,611
  • 7
  • 39
  • 49
0

use one of these styles :

position += 1
position -= 1

or

position = position + 1
position = position - 1

to add or subtract 1 from a value. Currently you're just assigning it the value + / - 1.

Graham Griffiths
  • 2,196
  • 1
  • 12
  • 15
  • while in this case using `+=1` and `-=1` is better (because then changing the amount is easier), you really ought to at least mention that they could use `++` and `--` to accomplish this. – AJMansfield Aug 27 '13 at 15:22
0

Replace every car == "something" with car.equals("something")

Like this:

String car = Global.keyboard.nextLine();
if (car.equals("r"))
    position =  + 1;
if (car.equals("R"))
    position = +1;
if (car.equals("l"))
    position =  -1;
if (car.equals("L"))
    position = -1;
    System.out.print(position);
Jakob
  • 751
  • 4
  • 17
0

Instead of using multiple ifs (that should be if - else statements, so you don't cheack each one in every case) and Strings, remember that you can switch a char:

int position = 0;
//int dx = 1; For this case, the explanation is in a comment below.
System.out.print("move: ");
char car = Global.keyboard.nextChar();
switch(car) {
    case 'r':
    case 'R':
        position += 1; //Or position++ if you prefer, but I'd use this approach
                       //just in case you want to do position += 5 in the future
                       //or better yet, position += dx, being dx an int that
                       //defines the range of a single movement.
        break;
    case 'l':
    case 'L':
        position -= 1; //Same as with right, but position -= dx;
        break;
    default:
        System.out.println("Please, use r - R for (R)ight or l - L for (L)eft");
        break;
}
System.out.print(position);

Also, please note I changed your position updating. To adress your current question, == shouldn't be used to compare Strings since it compares references. Use equals instead.

Fritz
  • 9,987
  • 4
  • 30
  • 49