Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 11 12 [13] 14 15 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 820645 times)

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #180 on: January 08, 2012, 07:20:36 pm »

Has Java changed since I last brushed up on it?
Seriously, I remember testing such a thing and it didn't work in the past... All this new shit it does apparently.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #181 on: January 08, 2012, 07:25:12 pm »

Short answer: it doesn't work because System.in.readln() does not exist.

Longer answer: You mean let someone enter a string into the console a la C++ cin, right?  I was going to say I've never used System.in.readln(), but it appears there's no such thing in Java...  So are you asking something else or are you using the wrong command?  I'll look into entering and reading from the console, but I've honestly never done it before in Java.  I was originally taught that if I want user input I should either pass it as a command line argument, read it from a file, or read it from a JOptionPane or other GUI element.

Beyond that, I'm not up to methods.  The only methods I'm using so far are main() and System.out.println(), both of which count as magic until I get around to the methods lesson.

What? Fuck hold on, BRB. FUCKING JAVA! Why you no consistent? This is why I always rage at Java, you need to spend 5 hours reading over the docs.
No, what I mean is that it will always return false because it is comparing the locations of the strings, rather than the content of the strings.

That's because, despite all the magic and hand-waving, Strings are still objects, and Java tests equality of objects by seeing if they exist at the same place in memory (like C/C++ pointers). Stargrasper, unless that was changed in Java 6, Max is correct.

Also, Max:

Code: [Select]
Scanner in = new Scanner(System.in);
String input = in.readLine();

Yay Java!

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #182 on: January 08, 2012, 07:42:51 pm »

Short answer: it doesn't work because System.in.readln() does not exist.

Longer answer: You mean let someone enter a string into the console a la C++ cin, right?  I was going to say I've never used System.in.readln(), but it appears there's no such thing in Java...  So are you asking something else or are you using the wrong command?  I'll look into entering and reading from the console, but I've honestly never done it before in Java.  I was originally taught that if I want user input I should either pass it as a command line argument, read it from a file, or read it from a JOptionPane or other GUI element.

Beyond that, I'm not up to methods.  The only methods I'm using so far are main() and System.out.println(), both of which count as magic until I get around to the methods lesson.

What? Fuck hold on, BRB. FUCKING JAVA! Why you no consistent? This is why I always rage at Java, you need to spend 5 hours reading over the docs.
No, what I mean is that it will always return false because it is comparing the locations of the strings, rather than the content of the strings.

Wow, that's even more stupid.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #183 on: January 08, 2012, 07:45:00 pm »

What is more stupid than what?
Not bothering to remember every detail of a class, or knowing the difference between a class and a primitive.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #184 on: January 08, 2012, 07:49:05 pm »

Look, guys, I'm telling you the string comparison thing works.  I don't know when it changed, but I know it works in Java 6 and Java 7.  Java checks all objects for equality by calling the equals method or checking the memory location if equals wasn't overridden except for String.  It treats String as a primitive for this purpose.
Code: [Select]
package test;

public class StringCompare {

public static void main(String[] args) {
String s1 = "foo";
String s2 = "foo";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

String s = "foo";
if (s == "foo") {
System.out.println("It Works");
} else {
System.out.println("It Doesn't Work");
}
}

}
outputs
Code: [Select]
true
true
It Works

I was taught at university that this works (and was promptly told I shouldn't do it) and I've tested it.  I'm willing to believe it works.  Either run my code and believe me or show me an example where it doesn't work.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #185 on: January 08, 2012, 07:51:43 pm »

Wow, that's even more stupid.

That's because, despite all the magic and hand-waving, Strings are still objects, and Java tests equality of objects by seeing if they exist at the same place in memory (like C/C++ pointers). Stargrasper, unless that was changed in Java 6, Max is correct.

No, actually it makes perfect sense. Java is consistent in its ways of comparing equality of objects. It could be argued that the problem comes from the hand-waving used to make Strings seem like primitives, but that's a different discussion.

The alternative to this system would be to compare all objects and primitives based on value, and incorporate pointers into the language. As a C/C++ programmer, I know exactly how badly pointers can be used. Plus, I don't think there's a viable way to even add that functionality to Java without re-writing 70% of the language. The way things work now is the lesser of the two evils.

@Stargrasper: From what you've said, it sounds like that was a change made in Java 6. Try it in Java 5. I'll put a screenshot of my program up in a second. Also, try comparing two String objects.

EDIT: I remembered now that it was comparing two String objects that doesn't work. I've never tried comparing a String object to a literal because of this.

EDIT 2: Why is it working in this class but not in the other class?

Code: [Select]
// A program from my compsci class
import java.util.Scanner;

public class P4_bad {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        System.out.println("Do you want to continue?");
        String response = input.nextLine();
        if(response == "y" || response == "yes" || response == "ok" || response == "sure" || response == "why not?") {
            System.out.println("OK");
        } else if(response == "n" || response == "no") {
            System.out.println("Terminating");
        } else {
            System.out.println("Bad input: " + response);
        }
    }
}

Code: (Terminal Window) [Select]
Do you want to continue?
n
Bad input: n
« Last Edit: January 08, 2012, 07:59:33 pm by Mego »
Logged

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #186 on: January 08, 2012, 07:53:31 pm »

Wow, that's even more stupid.

That's because, despite all the magic and hand-waving, Strings are still objects, and Java tests equality of objects by seeing if they exist at the same place in memory (like C/C++ pointers). Stargrasper, unless that was changed in Java 6, Max is correct.

No, actually it makes perfect sense. Java is consistent in its ways of comparing equality of objects. It could be argued that the problem comes from the hand-waving used to make Strings seem like primitives, but that's a different discussion.

Can't you overload the == operator or something?
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #187 on: January 08, 2012, 07:53:59 pm »

This is not C++. There is not operator overloading. Only method overloading. And even then, to use that, you'd need to extend the String class in a new class and change the equals method there. So, it's not possible to overload it in the sense that you're thinking of.

MaximumZero

  • Bay Watcher
  • Stare into the abyss.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #188 on: January 08, 2012, 07:55:19 pm »

*sigh* My next semester (C++2, VB2, and Java) is going to be !!FUN!!, isn't it?
Logged
  
Holy crap, why did I not start watching One Punch Man earlier? This is the best thing.
probably figured an autobiography wouldn't be interesting

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #189 on: January 08, 2012, 07:56:25 pm »

Can't you overload the == operator or something?
C++ yes.
C# yes.
Java narp.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #190 on: January 08, 2012, 07:59:10 pm »

@Stargrasper: From what you've said, it sounds like that was a change made in Java 6. Try it in Java 5. I'll put a screenshot of my program up in a second. Also, try comparing two String objects.

EDIT: I remembered now that it was comparing two String objects that doesn't work. I've never tried comparing a String object to a literal because of this.

Um...I did...let me remind you...third time I've posted this excerpt here, btw.
Code: [Select]
String s1 = "foo";
String s2 = "foo";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

Both return TRUE.  Are you trying to tell me that isn't comparing two String objects?

I'll look into how Java 5 functioned later.  I've only ever used Java 6 and 7.

Can't you overload the == operator or something?

As stated, no, you can't do that.  Java does that internally somewhere (probably in the compiler) specifically and only for String.
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #191 on: January 08, 2012, 08:01:17 pm »

I am discovering (remembering?) that my memory is bad. The program I was thinking of was comparing Strings to literals. I edited that post with the code.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #192 on: January 08, 2012, 08:01:49 pm »

Star, relax, nobody is disagreeing with you, we are just stating our surprise that Java does this now, as it did not in the past.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #193 on: January 08, 2012, 08:02:48 pm »

Actually I'm stating my surprise that it works for one program but not for another, for no clear reason.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #194 on: January 08, 2012, 08:04:57 pm »

I'm sorry.  I interpreted it as you refusing to believe me that it works like I described.  Stupid misunderstanding. :(
Logged
Pages: 1 ... 11 12 [13] 14 15 ... 795