PDA

View Full Version : I don't get scanner



All that is man
11-16-2009, 04:22 AM
import java.util.Scanner;

public class teh1337hax {
public static void main(String[] args){
/**Scanner s = new Scanner(System.in);
System.out.println("Please enter command: ");
String command = s.nextLine();
if(command == "login"){**/
if(login("Brian", "lolgf")){
System.out.println("W00tW00t u r t3h 1337 l0l");
}
//}
}

public static boolean login(String userName, String passWord){
Scanner s = new Scanner(System.in);
System.out.print("Please enter username: ");
String username = s.nextLine();
System.out.print("Please enter password: ");
String password = s.nextLine();
boolean tempbool = false;
if(username == userName){
if(password == passWord){
System.out.println("Login succesful");
tempbool = true;
}else{
System.out.println("Invalid password!");
tempbool = false;
}
}else{
System.out.println("Invalid username!");
}
return tempbool;
}
}


I cannot get scanner to work no matter what i type in, I was just messing around with java :p going to bed now will check in later

Method
11-16-2009, 05:48 AM
You shouldn't compare Strings using ==. Instead, you should use the equals method of the String class.

For example, instead of

if(command == "login")

you would use

if(command.equals("login"))

The comparisons for username and password should follow that syntax also.

arash
11-17-2009, 02:44 AM
Actually technically you can compare Strings using ==, but that compares the JVM heap identification of the String objects as opposed to their actual content. Otherwise you are correct in your corrections.


You can't compare Strings use ==. Instead, you need to use the equals method of the String class.

For example, instead of

if(command == "login")

you would use

if(command.equals("login"))

The comparisons for username and password should follow that syntax also.

All that is man
11-17-2009, 02:51 AM
Thank you very much, got it working!!

noidea
11-17-2009, 02:52 AM
However, with chars, you use the ==
You will get an error if you use .equals()

Method
11-17-2009, 03:00 AM
Actually technically you can compare Strings using ==, but that compares the JVM heap identification of the String objects as opposed to their actual content. Otherwise you are correct in your corrections.

Yeah, I've fixed that in my post.

arash
11-17-2009, 03:03 AM
Much better :)

super_
11-23-2009, 03:49 PM
Actually technically you can compare Strings using ==, but that compares the JVM heap identification of the String objects as opposed to their actual content. Otherwise you are correct in your corrections.

how can you compare 'jvm heap identification'? == compares the references themselves, while equals() should be used for content. doesn't this seem a bit dumb? yeah but java doesnt have operator overloading wah wah too hard for noobs wah. anyway, a scanner is what it says it is... a scanner/lexer which converts a character stream into tokens.


However, with chars, you use the ==
You will get an error if you use .equals()
this is because char is a primitive type, not a reference type, and as so, does not have any methods or fields.

and lastly, if the two strings are both intern'd (constant strings are automatically) into the string pool, then the following will succeed because they are the same reference.


String s1 = "hello";
String s2 = "hello";
assert (s1 == s2);

arash
11-24-2009, 02:33 AM
The "JVM heap identification" is the address of the object on the heap, AKA the memory address of the object stored on the heap. You would use this in complex data structures that use hash tables or any other form of object organization. For you it may "seem dumb" because you do not have the intelligence to understand it.


how can you compare 'jvm heap identification'? == compares the references themselves, while equals() should be used for content. doesn't this seem a bit dumb? yeah but java doesnt have operator overloading wah wah too hard for noobs wah. anyway, a scanner is what it says it is... a scanner/lexer which converts a character stream into tokens.

super_
11-25-2009, 05:10 PM
actually, you ignorant piece of shit, there is no such thing as "heap identification".
let's take a look at this stuff...

class Z extends java.lang.Object{
Z();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: aconst_null
1: astore_1
2: aload_0
3: aload_1
4: if_acmpne 15
7: getstatic #2; //Field java/lang/System.err:Ljava/io/PrintStream;
10: ldc #3; //String arash is a fucking idiot
12: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
15: return

}
if_acmpne and if_acmpeq are the branch instructions emitted for comparing two objects with ==.
openjdk, bytecodeInterpreter.cpp:


#define COMPARISON_OP2(name, comparison) \
COMPARISON_OP(name, comparison) \
CASE(_if_acmp##name): { \
int skip = (STACK_OBJECT(-2) comparison STACK_OBJECT(-1)) \
? (int16_t)Bytes::get_Java_u2(pc + 1) : 3; \
address branch_pc = pc; \
UPDATE_PC_AND_TOS(skip, -2); \
DO_BACKEDGE_CHECKS(skip, branch_pc); \
CONTINUE; \
}

COMPARISON_OP2(eq, ==); /* include ref comparison */
COMPARISON_OP2(ne, !=); /* include ref comparison */
as you can see, it compares the top stack words. but what do they look like for objects?


CASE(_aload):
SET_STACK_OBJECT(LOCALS_OBJECT(pc[1]), 0);
UPDATE_PC_AND_TOS_AND_CONTINUE(2, 1);

#define LOCALS_OBJECT(offset) ((oop)locals[-(offset)])

#define STACK_OBJECT(offset) (*((oop *) &topOfStack [-(offset)]))
so it stores objects as oops. so whats an oop?
i'll let you figure out the rest.
the term for an object's address on the heap is not 'heap identification', you moron. and yes, i am fully aware of what you can do with an address of an object, likely more-so than yourself. i called java's lack of operator overloading dumb, and i most definitely have enough intelligence to comprehend it. it's a shame, though, that you do not. next time before you start making up your own shit vocabulary and calling people idiots, oh, and, just generally being an idiot, take notice to who you're in the presence of.

arash
11-25-2009, 11:09 PM
So your basically proving and agreeing with my previous statements but you take issue with the term "heap identification".

Congratulations that was a pointless flame post on programming concepts. However I am amazed by your knowledge of basic programming concepts, and your ability to copy and paste from openjdk.



the term for an object's address on the heap is not 'heap identification', you moron.

super_
11-26-2009, 01:04 AM
So your basically proving and agreeing with my previous statements but you take issue with the term "heap identification".

Congratulations that was a pointless flame post on programming concepts. However I am amazed by your knowledge of basic programming concepts, and your ability to copy and paste from openjdk.

the whole argument was over your poor choice of vocabulary. ps i can assure you that my skills are far beyond basics, as well as yours.

Nava2
11-26-2009, 01:15 AM
Lets stop the flaming, k? This is all offtopic, and completely unnecessary.

Stop.

R0b0t1
11-27-2009, 08:03 AM
What the...?

This is honestly a pretty stupid argument.


EDIT: And I've got to say, ==, when used with objects, does compare instances.

arash
11-28-2009, 03:53 AM
Not instances, references or as I like to call it "JVM Heap Identification". References and instances are not interchangeable in programming vocabulary.



EDIT: And I've got to say, ==, when used with objects, does compare instances.