[SOLVED] strange if statement problem

Hello,
i am trying to create a tiled map based on a file content, and till now things are working “fine” till i got this strange problem,
the thing is that i have a multi-Dimensional array, which i store in it all the content of the file (just to make it easier when i will draw the world)
now the problem is here :
if i do this (please follow me here) :

System.out.println(stringWorld[0][0]);

the program print : o
now if i do this :


if (stringWorld[0][0] == "o") {
			System.out.println("yes");
		} else {
			System.out.println("no");
		}

the program print : no

here is the classes that you might need :

this is the “world creator” class (the one that contain the error)


import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class World {

	private int width, height;
	private Block block;
	private String[][] stringWorld;
	private File file;
	private Load load;

	//constructor
	public World(int width, int height) {

		this.width = width;
		this.height = height;

		load = new Load();
		file = new File("res/beta.txt");

		load.loadLevel(file);
		stringWorld = load.getWorld();
		
	}

	//checking the grid case 
	public void setWorld() {
		System.out.println(stringWorld[0][0]);

		if (stringWorld[0][0] == "o") {
			System.out.println("yes");
		} else {
			System.out.println("no");
		}

	}

	// doesn't matter now 
	public void drawInWorld(int x, int y, int type) {
		Block block = new Block(0, 0, 32, type);
		block.setX(x * block.getSize());
		block.setY(y * block.getSize());
		block.draw();

	}
	
	//in case i want to check 
	public void printTheWorld() {
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				System.out.print(stringWorld[y][x]);
			}
			System.out.println("");
		}
	}

}


this is the " file loader" class

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Load {
	// the array that will hold information from the file
	private String[][] testWorld = new String[Board.height][Board.width];

	// reading from the file and writing into the array
	public void loadLevel(File path) {
		try {
			Scanner loadScanner = new Scanner(path);

			while (loadScanner.hasNext()) {

				for (int y = 0; y < Board.height; y++) {
					for (int x = 0; x < Board.width; x++) {
						testWorld[y][x] = loadScanner.next();
					}

				}
			}

		} catch (FileNotFoundException e) {
			System.err.println("Error: " + e.getMessage());
		}
	}

	// getting the array
	public String[][] getWorld() {

		return testWorld;

	}
	
	//in case i want to check 
	public void printing() {
		for (int y = 0; y < Board.height; y++) {
			for (int x = 0; x < Board.width; x++) {
				System.out.print(testWorld[y][x]);
			}
			System.out.println("");
		}
	}

}

this is the board class (normally you’re not gonna need it )


/**
 * This class will handle the game logic 
 * 
 */

import java.util.Arrays;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

public class Board {
	// varibable of other classes
	private Drawing draw = new Drawing(); // drawing class
	private Loop loop = new Loop(); // the loop class

	private double heroX, heroY, heroW, heroH, heroSpeed;
	private Player hero;

	private Block block;
	private World world;

	private GameTools tool = new GameTools();

	private boolean onGround, canJump;

	int delta;

	static int width = 26, height = 22;
	static String[][] myWorld;

	// initializing
	public void init() {

		loop.init();
		initHero();
		hero = new Player(heroX, heroY, heroW, heroH);
		onGround = false;
		canJump = false;
		world = new World(width, height);

		world.setWorld();

	}

	// the main game logic "caller"
	public void myBoardLoop() {
		delta = loop.getDelta();
		update(delta);
	}

	// the update method
	private void update(int delta) {
		painting();
		heroUpdate();
		loop.updateFPS();
	}

	private void painting() {
		// clear the screen and depth buffer
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		//draw the player 
		draw.coloring("white");
		draw.fillRect(hero.getX(), hero.getY(), hero.getWidth(),
				hero.getHeight());

	}

	// initializing the player
	private void initHero() {
		heroX = 3 * 32;
		heroY = Game.HEIGHT - (2 * 32);
		heroW = 32;
		heroH = 32;
		heroSpeed = 0.5;
	}

	// updateing the player
	private void heroUpdate() {
		control(delta);
		hero.setX(heroX);
		hero.setY(heroY);
		hero.setWidth(heroW);
		hero.setHeight(heroH);
		// heroCollision();

	}

	// controlling the player
	private void control(int delta) {
		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			heroX -= heroSpeed * delta;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			heroX += heroSpeed * delta;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			heroY += heroSpeed * delta;

		}
		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			heroY -= heroSpeed * delta;
		}
	}

	private void heroCollision() {

		if (!onGround) {
			heroY -= 0.9 * delta;
		}

		for (int y = 0; y < 10; y++) {
			for (int x = 0; x < 10; x++) {

				int tmpBlockX = x;
				int tmpBlockY = y;

				int blockGridX = tmpBlockX * 32;
				int blockGridY = tmpBlockY * 32;
				String block = myWorld[y][x];

				if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),
						hero.getHeight(), blockGridX, blockGridY, 32, 32)) {
					if (block == "o") {
						System.out.println("touch");
					}
					if (block == "g") {
						onGround = true;
						canJump = true;
					} else {
						onGround = false;
					}
				}
			}
		}
	}

}


and finaly this is the file :


o  A  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  o  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  g  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  
*  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  *  

thank you very much

For string comparison, you need to use .equals() instead of ==.

if(stringworld[z][y].equals("wahtever)

Syntax Error!
Syntax Error!

thank you

It’s a gotcha for java.
This prints ‘yes’ so you kinda expect it to work everywhere;


String s="x";
if (s=="x")
{
	System.out.println("yes");
}
else
{
	System.out.println("no");
}

It’s caught me before!

Why does that print yes while in the OP’s case it does not?

An excellent question! Er… Anyone? :persecutioncomplex:

In your case, you are comparing String constant to String constant, which IS the same object.

In the case of the OP, it isn’t a String constant being loaded, so is not the same object as what’s being compared.

I knew that! It’s still confusing for a beginner though… Maybe there ought to be a compiler warning or something?

Hehehe…

One minute after that post I made the same mistake in my text-based game.

facepalm

(although I did get it right the other 100 or so times)

There’s a difference between two objects being the same object and the objects being equal. A problem many people encounter is that they create for example a point class for a 2D point:


public class Point{

    public float x, y;

    public Point(float x, float y){
        this.x = x;
        this.y = y;
    }
}

Then they compare two points like this:


Point p1 = new Point(0, 0);
Point p2 = new Point(0, 0);
if(p1 == p2){
    //...
}

A programmer new to object orientation will believe that the two points are the same since they contain the same data but they’re actually two different objects containing the same data, so the if-statement will always evaluate to false. The correct way to compare two points is to implement an equals(Point p) function and use that instead of ==.


    @Override
    public boolean equals(Object other) {
    	//Assume that Points will only ever be compared to other Points.
    	Point otherPoint = (Point) other;
    	return this.x == otherPoint.x && this.y == otherPoint.y;
    }

This knowledge can be immediately applied to Strings too. A string is actually an array of chars. Just because the data of the String object (the chars in the array) is the same doesn’t necessarily mean that the String objects are the same objects. What confuses people is that sometimes the == comparison works since the JVM tries to reuse String instances to avoid creating multiple identical Strings, but it cannot always figure this out. The String.equals() function however compares the length of the strings and their chars and will always work as you expect.