Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with code

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with code

    I am trying to move the O throughout the game board, then change the previously occupied space to a X. For some reason it won't add or subtract from row or col when I type in U D L or R.

      public static void pickMove(char[][] array){
     
        int row = 0;
        int col = 0;
     
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Move: ");
        //String letter = sc.next();
        //String letter = sc.nextLine();
     
        for(row=0; row<array.length; row++){
          for(col=0; col<array.length; col++){
     
            if (array[row][col] == 'O'){
     
              char temp = array[row][col];
     
              temp = '.';
     
              if (sc.nextLine() == "U")
                row++;
              else if (letter == "D")
                row--;
              else if (letter == "L")
                col--;
              else if( letter == "R")
                col++;
     
              array[row][col] = 'X';
     
              System.out.printf("%d %d ", row, col);
              }
     
            }
     
          }
        printBoard(array);
        }


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: help with code

    if (sc.nextLine() == "U")
    Don't use == to compare the contents of strings, use equals() instead.

    if (sc.nextLine().equals("U"))

    The thing is that == compares the values of variables or other expressions. This is fine if those variables are primitive (char, int, etc) because the value (a character, integer etc) is probably what you want to compare. But it not so good if you are comparing variables or expressions that are a reference type like String. Those variables have as their value a reference (aka pointer), and you don't mean "do these expressions point at the same object?", rather you mean "do the strings these two expressions point to consist of same sequence of characters?"

    The String class - and all other classes - have an equals() method that expresses what it means for two strings to be considered "the same". String also has an equalsIgnoreCase() method that expresses an obvious variation on this theme.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: help with code

    == compares the references. If you want to compare the references use, == and if you want to compare the contents, use equals().

Similar Threads

  1. code to refresh and check the code of a webpage..
    By vaggelis in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 7th, 2012, 07:43 AM
  2. Help merging program code with GUI code
    By Wilha in forum AWT / Java Swing
    Replies: 2
    Last Post: January 25th, 2012, 07:03 PM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  5. describe this program code by code ....
    By izzahmed in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2011, 12:03 AM