PDA

View Full Version : Java String problems



Faisal
16-02-11, 14:33
Well I was just testing some Java programs and I am running into this problem which I really can't understand why it's happening. If I was to make the script in another programming language it would work a treat but in Java it's not having any of it :S.

Basically the test program I was making was simple to make the user enter "help" and then it would add a "/" to the beginning of help, making it "/help". Once that's done, I would bring up an if statement to see if it recognizes the string to be "/help" and if it does it is to output a message saying it recognizes it. Fairly basic stuff right? But the only problem is it's not recognizing it, I even make it output the string variable before and it reads "/help" and then when I try to check that it is "/help" in an if statement, according to the if statement it is not equal to "/help". I'm probably making a very stupid mistake but can anyone help me here? I just want to know the solutions to all the problems I encounter when programming in Java rather than finding a get around.

Here's the source code:


import java.util.Scanner;

public class tests
{
public static void main ( String[] args )
{
String storeCMD;
Scanner input = new Scanner (System.in);

System.out.print("Enter a command: ");
storeCMD = input.nextLine();

storeCMD = "/"+storeCMD;
System.out.println("\nStore CMD is now equal to: "+storeCMD);

if (storeCMD == "/help")
System.out.println("The help CMD was also recognised");
else
System.out.println("The help CMD was not recognised");

}

}

alexnifty
16-02-11, 14:47
I think your syntax could be wrong on the if statement?



if (storeCMD == "/help")
{
System.out.println("The help CMD was also recognised");
}

else
{
System.out.println("The help CMD was not recognised");
}

andyn
16-02-11, 14:52
Nah the brackets are implied, you are allowed to do that with single-line if statements in most c-like languages. Dunno the answer to the OP sorry, don't actually do java. Looks syntactically valid though. What exactly DOES it output?

Faisal
16-02-11, 14:55
Nothing really, the whole point of the program is to see how I can experiment with strings and to see how Java reacts to my experimenting. I was trying to modify the string which I succeeded in and Java outputed the modified string but when I tried to use the modified string in a if statement it doesn't recognize it/work.

As for the curly brackets, yes I'm aware of that but since this isn't a complicated program I couldn't be bothered and I was pretty sure it's not required.

EDIT: I know the syntax is correct otherwise it wouldn't compile (in most cases), I also know that the logic of the program is perfectly well but there is something about those if statements that Java simply doesn't like :S.

andyn
16-02-11, 15:00
I think the thing you might be missing is that you're treating this like a C program, rather than a Java program.

What I mean is that what you have there is a class. It won't actually DO anything by and of itself, a class needs methods to be useful, and it needs some piece of code to actually instantiate it and call a method before it will actually do anything.

I don't think that just by defining a method called 'main' it will be called like it would with a C program.

Unless you actually have a separate (unshown) bit of code which actually calls the class and the main method? In which case you probably knew all this already :P.

Faisal
16-02-11, 15:10
I think the thing you might be missing is that you're treating this like a C program, rather than a Java program.

What I mean is that what you have there is a class. It won't actually DO anything by and of itself, a class needs methods to be useful, and it needs some piece of code to actually instantiate it and call a method before it will actually do anything.

I don't think that just by defining a method called 'main' it will be called like it would with a C program.

Unless you actually have a separate (unshown) bit of code which actually calls the class and the main method? In which case you probably knew all this already :P.

Actually I was only using one method and one class, but I actually never thought about that it's probably the source of my problem. I'm still not too good at using void and static in methods so they may also have something to do with it?

Dukey
16-02-11, 15:28
Common issue with Java, strings don't like being compared in this way


I think you need to use

storeCMD.equals("/help");

Strings in Java are actually Objects, not primitive types such as Integers or Booleans, The String class has many methods for comparing and altering strings that can be called in the same way :)

alexnifty
16-02-11, 15:31
Yeah I was about to suggest using an int instead to test. I seem to remember strings are a bit weird.

Faisal
16-02-11, 15:34
Common issue with Java, strings don't like being compared in this way


I think you need to use

storeCMD.equals("/help");

Strings in Java are actually Objects, not primitive types such as Integers or Booleans, The String class has many methods for comparing and altering strings that can be called in the same way :)

THANKS! Now it's fixed.

Thank'd + Rep'd

Dukey
16-02-11, 15:40
THANKS! Now it's fixed.

Thank'd + Rep'd

Fantastic :) any more Java issues hit me up! Had it all drilled into me at Uni :D

Bombalur
16-02-11, 16:16
Common issue with Java, strings don't like being compared in this way


I think you need to use

storeCMD.equals("/help");

Strings in Java are actually Objects, not primitive types such as Integers or Booleans, The String class has many methods for comparing and altering strings that can be called in the same way :)

Yeah, got to use storeCMD.equals("/help"); for the reasons dukey said.

It checks the value of the object, not the string, if you use == it treats it like an array and will return an integer iirc. and Its just plain incompatible with that logical operator

alexnifty
16-02-11, 17:22
Yeah, got to use storeCMD.equals("/help"); for the reasons dukey said.

It checks the value of the object, not the string, if you use == it treats it like an array and will return an integer iirc. and Its just plain incompatible with that logical operator

That's a UNI practical coming screaming back to me in this post :lol:

Dukey
16-02-11, 17:27
Java 101 :D