3

Does anyone know why this doesn't compile?

public class ArrayCompare{

   public static void main (String []args){

      String words= "Hello this is a test";

      String delimiter=" ";
      String [] blocker=words.split(delimiter);

      String [][] grid= new String [100][100];

      grid[0]="Hello";

           if (grid[0].equals(blocker[0])){
                 System.out.print("Done!");
   }
        }
             }

I would like to perform this function of comparison using a 2 dimensional array. I am a newbie! Please help if you can. Thanks in advance!

3
  • 2
    grid[0] is array of string. It is impossible to assign string to array of string. Commented Mar 30, 2012 at 3:51
  • Do you want to compare each String in grid to blocker[i]? If so, you need a nested loop Commented Mar 30, 2012 at 3:51
  • I wanted to test each String in grid to blocker[i]. Commented Mar 30, 2012 at 4:03

6 Answers 6

1

Try this:

grid[0][0]="Hello";

grid is a two-dimensional array. For the same reason, you need to do this:

if (grid[0][0].equals(blocker[0]))
Sign up to request clarification or add additional context in comments.

Comments

0

grid[0] is a String[] type, not a String. So your code should be like this

grid[0] = new String[100];
grid[0][0] = "Hello";
if (grid[0][0].equals(bloker[0])) {
    //your logic...
}

2 Comments

"grid[0][0] == bloker[0]" No! Don't do this! This is comparing references, which will not be equal. You need to use grid[0][0].equals(blocker[0]). This is true in general in Java.
But that's extremely bad practice, it shouldn't be shown to a newbie especially, and not true in the general case.
0
String [][] grid= new String [100][100];

  grid[0]="Hello";

There's your problem. You are trying to assign a string to a string array. Think of a 2d array as an array of arrays.

You probably want something like

grid[0][0]="Hello!";

Comments

0

grid is 2d array. you can't do like d[0] = "Hello". So, if you want to assign value at 0 position

d[0][0] = "Hello";

 if (grid[0][0].equals(blocker[0])){
System.out.print("Done!");
 }

Comments

0

It won't compile because grid[0] is not String type. It is String[] (Array) type. Variable grid[0] is actually pointing to a String[100] array.

You are attempting to assign a string "Hello" to an array by

grid[0]="Hello"; statement.

If you want to assign a string to a location in grid, you must provide two index(s) - following is legal:

grid[0][0]="Hello";

May I suggest using eclipse or BlueJ to edit your Java code? so that such basic errors are shown on real-time and explained well?

1 Comment

@user1299661, if an answer was helpful, consider upvote (arrow on left) or accepting the answer - that will improve your chances of getting questions answered in SO in future.
0

First thing you can't assign value to the element of the multi dimensional array using single index

grid[0]="Hello";
you need to specify both the indices like grid[0][0] = "Hello" this will set the 0th element of 0th row to Hello

Likewise while compairing if (grid[0].equals(blocker[0])){ System.out.print("Done!"); you have to pass the same indices here (You cannot compare a String to a Array Object) if (grid[0][0].equals(blocker[0])){ System.out.print("Done!");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.