Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] strange if statement problem  (Read 951 times)
0 Members and 1 Guest are viewing this topic.
Offline alaslipknot
« Posted 2013-05-06 23:50:33 »

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) :
1  
System.out.println(stringWorld[0][0]);

the program print : o
now if i do this :
1  
2  
3  
4  
5  
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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
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 )
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
/**
 * 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 :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
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

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline Sammidysam
« Reply #1 - Posted 2013-05-06 23:52:49 »

For string comparison, you need to use .equals() instead of ==.
Offline Agro
« Reply #2 - Posted 2013-05-06 23:53:18 »

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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2013-05-06 23:56:36 »

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

Syntax Error!
Syntax Error!

Offline alaslipknot
« Reply #4 - Posted 2013-05-06 23:57:55 »



thank you

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline SimonH
« Reply #5 - Posted 2013-05-07 00:35:30 »

It's a gotcha for java.
This prints 'yes' so you kinda expect it to work everywhere;
1  
2  
3  
4  
5  
6  
7  
8  
9  
String s="x";
if (s=="x")
{
   System.out.println("yes");
}
else
{
   System.out.println("no");
}

It's caught me before!

People make games and games make people
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2013-05-07 00:38:17 »

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

Offline SimonH
« Reply #7 - Posted 2013-05-07 00:40:53 »

An excellent question! Er... Anyone?  persecutioncomplex

People make games and games make people
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #8 - Posted 2013-05-07 00:43:26 »

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.

Offline SimonH
« Reply #9 - Posted 2013-05-07 00:47:12 »

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

People make games and games make people
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 253
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #10 - Posted 2013-05-07 00:50:04 »

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)

Offline theagentd
« Reply #11 - Posted 2013-05-07 17:37:00 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
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:

1  
2  
3  
4  
5  
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 ==.

1  
2  
3  
4  
5  
6  
    @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.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (20 views)
2014-09-21 02:42:18

BurntPizza (14 views)
2014-09-21 01:30:30

moogie (14 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (40 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (96 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!