Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java equals() not working?
New Posts  All Forums:Forum Nav:

Java equals() not working?

post #1 of 15
Thread Starter 
I am trying to get data from a file(named settings.dat). The format is as follows:
"Name of Setting" : "Value" (Without the quotes)

I have wrote a program to get the data for each line and see if the setting name matches a setting name saved in a string array. The program works correctly until I try to compare the saved names to the ones in the file.

Code:
Code:
public class GameHelper {
static int numOfSettings = 6;
static String[] currentSetting = new String[]{"EnemyAttackDelay","MaxEnemyWaitTime","EnemyPlayer","NumOfEnemyShips","NumOfPlayerShips","ShipSizeList"};

public static void loadGameData(){
try{
DataInputStream in = new DataInputStream(GameHelper.class.getResourceAsStream("/settings.dat"));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String[] str = new String[numOfSettings+1];

//Assigns each line to an array
for(int x = 0; (str[x] = br.readLine()) != null; x++);

for(int n = 0; n < numOfSettings; n++){
System.out.println(str[n]);
char[] command = new char[str[n].indexOf(':')];
str[n].getChars(0,str[n].indexOf(':'),command,0);
if(command.equals(currentSetting[n])){
System.out.println("Yay!");
}else{
System.out.println(command);
System.out.println(currentSetting[n]);
System.out.println();
}
}
in.close();

}catch(Exception e){
e.printStackTrace();
}
}
}
File:
Code:
EnemyAttackDelay: true
MaxEnemyWaitTime: 2000
EnemyPlayer: true
NumOfEnemyShips: 5
NumOfPlayerShips: 5
ShipSizeList: 2,3,3,4,5

When the program finally prints the values of the two strings to be compared, they are visually identical.
Its probablly really obvious. Thanks

EDIT: If it helps, here is what was printed in the console:
Code:

EnemyAttackDelay: true
EnemyAttackDelay
EnemyAttackDelay

MaxEnemyWaitTime: 2000
MaxEnemyWaitTime
MaxEnemyWaitTime

EnemyPlayer: true
EnemyPlayer
EnemyPlayer

NumOfEnemyShips: 5
NumOfEnemyShips
NumOfEnemyShips

NumOfPlayerShips: 5
NumOfPlayerShips
NumOfPlayerShips

ShipSizeList: 2,3,3,4,5
ShipSizeList
ShipSizeList
EDIT2: If I print the length of the strings, they are also identical.
Edited by Jtvd78 - 5/17/11 at 5:34pm
post #2 of 15
Just as a precaution its usually a good idea to strip any whitespace characters from Strings:

Quote:
String strippedString = original.replaceAll("[\\\
\\\
]", "");
Edit: And I mean the non-deliberate whitetspaces
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #3 of 15
Thread Starter 
Ahhhhh... I see my problem. I am comparing an array of chars to a string. So how do I convert a char array into a string? When I use methods from google......

String strCommand = new String(command);

...it creates a bunch or little boxes when printed, instead of a string

EDIT: Same thing with this:

String strCommand = String.valueOf(command);
Edited by Jtvd78 - 5/17/11 at 5:53pm
post #4 of 15
Sounds like you're having character encoding issues..

new String(chars); usually works fine, but I believe that StringBuilder is now the canonical way of doing this..

Code:
StringBuilder builder = new StringBuilder();
builder.append(command);
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #5 of 15
Thread Starter 
I'm confused. builder.append(command); returns a builder object.
post #6 of 15
Yeah it returns you a reference to itself for chaining purposes..

You're going to want to use the toString() method (I know, I know.. using the built-in toString seems weird!)
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #7 of 15
Thread Starter 
Even when I use that method, I get the same error.
post #8 of 15
Try this

Code:
StringBuffer sb = new StringBuffer();

sb.append(your chars here);

sb.toString();   //converts the StringBuffer object to a string
once you convert your char array to a string using StringBuffer...you now can use the .equal method built in the string class to compare the 2 strings.
Edited by surfbumb - 5/17/11 at 6:20pm
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
Black Silence
(15 items)
 
  
CPUMotherboardRAMHard Drive
i5 3570k @ 4.5 GHz Asus P8Z77-M Pro Kingston HyperX Genesis 8 GB - 1600 MHz Seagate Barracuda 250 GB 
Optical DriveCoolingOSMonitor
Samsung WriteMaster Noctua NH-D14 Windows 10 ASUS VS24AH-P 
KeyboardPowerCaseMouse
Logitech Navigator Enermax Infiniti 650W Fractal R3 Black Pearl Razer Death Adder 
Mouse PadAudio
SteelSeries QcK Mass Altec Lansing FX4021 
  hide details  
Reply
post #9 of 15
Thread Starter 
Nope, Same Thing
post #10 of 15
I just got it to work, but did some reworking of your code. Used more of the things that make Java so popular

Code:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class GameHelper {
static int numOfSettings = 6;
static String[] currentSetting = new String[]{"EnemyAttackDelay","MaxEnemyWaitTime","EnemyPlayer","NumOfEnemyShips","NumOfPlayerShips","ShipSizeList"};

public static void loadGameData(){
try{
DataInputStream in = new DataInputStream(new FileInputStream(new File("settings.dat")));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//String[] str = new String[numOfSettings+1];
ArrayList<String> commands = new ArrayList<String>();

//Assigns each line to an array
//for(int x = 0; (str[x] = br.readLine()) != null; x++);

while(br.ready()){
commands.add(br.readLine());
}

for(String setting : currentSetting){
for(String full : commands){
//System.out.println(full);
String[] splits = full.split(": ");
String command = splits[0];
if(!setting.equals(command)) continue;
String value = splits[1];
System.out.println(setting + " value is " + value);
}
}

in.close();

}catch(Exception e){
e.printStackTrace();
}
}

public static void main(String[] args){
GameHelper.loadGameData();
}
}
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java equals() not working?