Tutorial [Noobie-Info] Easier ways to do stuff.

Discussion in 'Resources' started by stoneminer02, Dec 12, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    stoneminer02

    So, I have seen much of
    Code:
    if (someboolean)
    {
        awesomeString = "True";
    } else {
        awesomeString = "False";
    }
    1. You don't need the brackets/braces(whatever the name is) when its 1 line of code in that statement.
    2. You don't even need if statements. It can be like this:
    Code:
    awesomeString = (someboolean ? "True" : "False");
    3. As I have seen someone state in a reply, you can use Boolean.toString(someBoolean);
    Please note the big B in Boolean.

    And I have seen stuff like this:
    Code:
    if(x == "59124fhaw")
    ... please! Use x.equals("some awesome stuff"); as it is more accurate and does less memory leaks as far as I know. I haven't gotten any memory leaks with this, but I have gotten once with two equal symbols.

    Also, when doing while loops. Do not place a while true and never use a break if its doing some Thread.sleep.
    This is a nice one. Could also be done as a for loop thats a bit further down.
    Code:
    while true { if(someint.equals(130)) break; someint++; log.log(Level.INFO, "hai"); }
    
    for (int x = 0; if(x <= 130); x++)
        log.log(Level.INFO, "hai");
     
    Last edited: Dec 19, 2014
  2. @stoneminer02
    Java tutorials (and tutorials in general) don't belong here. Anyways:

    1. True, but it doesn't hurt to use them. It's a preference.
    2. Or you could use String.valueOf(object). And if you're telling someone to do something as oppose to something else, you should explain why
    3. Your for loop is not even proper. Maybe you should look up the usage yourself.
     
    Last edited: Dec 12, 2014
    teej107 likes this.
  3. Moved and put a nice prefix on :)
     
    teej107 likes this.
  4. Offline

    MisterErwin

    @stoneminer02 Please - DON'T just say use equals!
    Say why and when to not use it!
     
    ChipDev likes this.
  5. Offline

    jthort

    In my opinion you should always use braces to keep your code organized.
     
  6. Offline

    HeadGam3z

    Integer "x" is null. You cannot increase a null value, nor put something along the lines of if (x == 130) in a for loop.
     
    ChipDev likes this.
  7. Offline

    ChipDev

    Oh no!
    You listed:
    "And I have seen stuff like this:"
    Code:
    if(x == "59124fhaw")
    "
    THEN did this:
    "... please! Use x.equals("some awesome stuff");
    Also, when doing while loops. Do not place a while true and never use a break if its doing some Thread.sleep.
    This is a nice one. Could also be done as a for loop thats a bit further down.
    Code:
    [LIST=1]
    [*]while true { if(someint.equals(130)) break; someint++; log.log(Level.INFO, "hai"); }
    [*]
    
    [*]for (int x; if(x == 130); x++)
    [*]    log.log(Level.INFO, "hai");
    [/LIST]
    
    "
    You seriously just used if(x == 130) in that loop, why not .equals WHICH YOU STATED THE PARAGRAPH above?
    :p
     
  8. Offline

    Deleted user

    awesomeString = Boolean.toString(someboolean);

    rekt

    Also, it's

    condition ? true : false

    So your example is kinda backwards
     
  9. Offline

    teej107

    The output of Boolean.toString(boolean) won't have the first letter capitalized! That last statement was sarcastic so don't respond to it. What gets me is why would you want to have a string named true or false when you have a perfectly capable primitive to handle the same thing? Though I would say the OPs example was a bad one. Those strings could say anything, not just "True" or "False".
     
  10. Offline

    Avygeil

    Code:
    boolean ʼ = false, ri = false, v = false, m = false;
    System.out.println(Boolean.toString(
        ! (ri &v&ri ^ʼ)|m
    ));
    @HeadGam3z Actually, local variables don't have a default value (not even null) and won't compile as such.
     
  11. Offline

    teej107

    Code:
    boolean ʼ = false, ri = false, v = false, m = false;
            System.out.println(! (ri &v&ri ^ʼ)|m);
     
    Avygeil likes this.
  12. Offline

    Avygeil

    @teej107 It was just meant to be read "i cri everi tim", printing true, using Boolean.toString as a joke for your statement. :( Yeah, nerd joke is nerd
     
    Last edited: Dec 13, 2014
    ChipDev, teej107 and (deleted member) like this.
  13. Offline

    Cirno

    That's not how primitives work...
     
  14. Offline

    Mr360zack

    ...
    I think you mean
    Code:
    for(int i = 0; i <=130; i++){
    getLogger().info("Hai");
    //Will log "hai" 130 times
    }
     
  15. @Mr360zack Correction, will log it *131 times since it's less than or equal to 130.
     
    Last edited: Dec 14, 2014
  16. Offline

    Mr360zack

    ** "//Will log "hai" 130 times" **
     
  17. Offline

    Aqua

    131, it will include 0.
    so;
    0, 1, 2, 3, 4, 5.... 130
     
    WarmakerT, mythbusterma and DJSkepter like this.
  18. Offline

    stoneminer02

    I was so sleepy when I wrote this, so I have fixed a lot of typos and fails in the code.
    Including the one when I used == in a for loop, instead of .equals, as I wasn't gonna use that either.
    As it is right now, its <=. For nubs, it will loop 131 times.
     
    ChipDev likes this.
  19. Offline

    teej107

    @stoneminer02 Have you even tried to write this in an IDE? There are still so many syntax errors in your basic Java code.
     
  20. Offline

    xTrollxDudex

    @teej107
    Hurray for hardcoded strings!

    Both the T/F letters in True and False are consistently uppercase!
     
  21. Offline

    Deleted user

    Wait what?

    Memory leaks from a basic comparison operator? Call the Java Cops! Something MUST BE DONE!
     
  22. Offline

    Skionz

    Personally I overuse my brackets as a personal preference. For me it is easier to read. I also prefer if-else statements to the ? : operator.
     
    Last edited: Dec 21, 2014
  23. Offline

    thomasb454

    If you want the name to the "? : operator" -> ternary operator.
     
    Skionz likes this.
  24. Offline

    stoneminer02

    No, as of this isn't going to be 100% right cause there is people who just copy-pasta the code and thats it to save some writing.
     
  25. Offline

    teej107

    Yeah, as if any Bukkit developer would even copy your code to begin with. This is basic Java and has nothing to do with Bukkit. Even posting a link to an actual Java tutorial would be more helpful than this.
     
  26. Offline

    stoneminer02

    1. People may not understand the java tutorials.
    2. I am awful at explaining java.
    3. I have feelings too.
     
  27. Offline

    teej107

    1. Programming with Bukkit requires a basic knowledge of Java.
    2. Then what makes you think that people would to be able to understand your resource? It's not just the explaining that needs work. It's your code.
    3. I'm sorry that may have put you down. Many people on this thread posted how to fix your code and you still haven't done that.
     
  28. Offline

    Shawckz

    where to begin...
     
    Avygeil likes this.
  29. Offline

    stoneminer02

    I don't get any good feedback so I'm done.
    - REQUEST: Lock
     
  30. Offline

    mrCookieSlime

    Locked on request.
     
Thread Status:
Not open for further replies.

Share This Page