Orcworm's SMP Server

We have moved to http://www.orcworm.co.uk

Join the forum, it's quick and easy

Orcworm's SMP Server

We have moved to http://www.orcworm.co.uk

Orcworm's SMP Server

Would you like to react to this message? Create an account in a few clicks or log in to continue.

Forum dedicated to Orcworm's Minecraft SMP Server, located at: orcworm.co.uk


+6
PureCraft
Lizardman
Ciphon
Onlyme
Albuca
patrickfreed
10 posters

    Coding Help

    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Coding Help

    Post by patrickfreed 2011-05-03, 12:51

    I hate to make two topics in one day, but what the hell; I'll do it anyways.

    Bare with me, as I've only watched a couple 10 minute tutorials in java.

    I'm trying to write a basic translator of a single latin word to english. Here's my code
    Code:
    import java.util.Scanner;
    public class  Main {
       public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          
          System.out.println("Enter a noun or verb.");
          String Word = scanner.next(); 
             if (Word == "neco"){
                System.out.println("Kill"); }
                else System.out.println("I suck at coding.");
             }
             
    }

    I type in neco, but all that comes up is the else. What am I doing wrong?
    Albuca
    Albuca
    In-Game Staff
    In-Game Staff


    Posts : 1205
    Join date : 2011-02-18
    Age : 31
    Location : 192.168.1.109

    Coding Help Empty Re: Coding Help

    Post by Albuca 2011-05-03, 13:16

    You cant use "==" for string comparisons.
    Use x.compareTo(y);

    Cheers!
    -A
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 13:19

    Could you edit the code to fit what you just posted?
    Onlyme
    Onlyme


    Posts : 357
    Join date : 2010-12-26
    Age : 33
    Location : On the Server

    Coding Help Empty Re: Coding Help

    Post by Onlyme 2011-05-03, 13:24

    patrickfreed wrote:I hate to make two topics in one day, but what the hell; I'll do it anyways.

    Bare with me, as I've only watched a couple 10 minute tutorials in java.

    I'm trying to write a basic translator of a single latin word to english. Here's my code
    Code:
    import java.util.Scanner;
    public class  Main {
       public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          
          System.out.println("Enter a noun or verb.");
          String Word = scanner.next(); 
             if (Word == "neco"){
                System.out.println("Kill"); }
                else System.out.println("I suck at coding.");
             }
             
    }

    I type in neco, but all that comes up is the else. What am I doing wrong?

    When you're comparing strings and objects with ==, it checks if it is the same object, or "alias". The static "neco" with a user inputted "neco" are different objects in the heap. Therefore, they are not equal.
    Albuca has it right with x.compareTo(y). That method checks if the objects are the same in content, and not memory location.




    Edit: Just ran the code in eclipse.

    Code:
    import java.util.*;
    import java.io.*;


    public class Main
    {

      public static void main(String args[])
      {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter a noun or verb.");
        String Word = scanner.next();

        if (Word.compareTo("neco") == 0)
        {
          System.out.println("Kill");
        }
        else
          System.out.println("I suck at coding.");
      }

    }

    The compareTo returns an int value based on if it's similar. 0 if it is, and 1 if it isn't. Since the if statement needs a boolean value, you need to turn it into a true or false by adding the "== 0" to it.


    Last edited by onlyme on 2011-05-03, 13:36; edited 2 times in total
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 13:28

    so it would look like:
    Code:
    import java.util.Scanner;
    public class  Main {
       public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          
          System.out.println("Enter a noun or verb.");
          String Word = scanner.next(); 
             if (Word.compareTo("neco"))
                System.out.println("Kill");
             
       }
             }
    This is giving me a ton of errors. I'm sorry, but I fail at this.
    Onlyme
    Onlyme


    Posts : 357
    Join date : 2010-12-26
    Age : 33
    Location : On the Server

    Coding Help Empty Re: Coding Help

    Post by Onlyme 2011-05-03, 13:38

    patrickfreed wrote:so it would look like:
    Code:
    import java.util.Scanner;
    public class  Main {
       public static void main(String args[]){
          Scanner scanner = new Scanner(System.in);
          
          System.out.println("Enter a noun or verb.");
          String Word = scanner.next(); 
             if (Word.compareTo("neco"))
                System.out.println("Kill");
             
       }
             }
    This is giving me a ton of errors. I'm sorry, but I fail at this.

    Look at my code.
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 13:44

    Alright that makes sense. thanks.
    Also, I'm going to add one of those if statements for each word I add to the dictionary, is there a way I can add a "master" else so that if the word doesn't match any of the if statements, it prints "That word is not in the dictionary"?
    Onlyme
    Onlyme


    Posts : 357
    Join date : 2010-12-26
    Age : 33
    Location : On the Server

    Coding Help Empty Re: Coding Help

    Post by Onlyme 2011-05-03, 13:47

    patrickfreed wrote:Alright that makes sense. thanks.
    Also, I'm going to add one of those if statements for each word I add to the dictionary, is there a way I can add a "master" else so that if the word doesn't match any of the if statements, it prints "That word is not in the dictionary"?

    Do you have skype? Onlyme395
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 13:47

    I do but it's almost 11 pm and I have an exam tomorrow. Can I talk to you tomorrow or sometime this week?
    btw it's patrickfreed1
    Albuca
    Albuca
    In-Game Staff
    In-Game Staff


    Posts : 1205
    Join date : 2011-02-18
    Age : 31
    Location : 192.168.1.109

    Coding Help Empty Re: Coding Help

    Post by Albuca 2011-05-03, 14:00

    if {}
    else if{}
    else if{}
    else if{}
    else {}

    Etc etc etc.
    Pm me your skype.

    -A
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 14:03

    is albuca your skype?
    Ciphon
    Ciphon


    Posts : 1637
    Join date : 2011-03-06
    Age : 27
    Location : 156,68,1261

    Coding Help Empty Re: Coding Help

    Post by Ciphon 2011-05-03, 14:35

    Holy crap I cannot understand a word you guys are saying!
    <-not a computer/coding geek
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 14:36

    Watch 30 minutes of java tutorials and you'll understand most of this ;p
    Ciphon
    Ciphon


    Posts : 1637
    Join date : 2011-03-06
    Age : 27
    Location : 156,68,1261

    Coding Help Empty Re: Coding Help

    Post by Ciphon 2011-05-03, 14:40

    Anything with dots returns strange symbols and strange words just makes my eyes dizzy
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 14:49

    I'd love to see Liz try to make sense of this :3
    Lizardman
    Lizardman


    Posts : 2846
    Join date : 2011-03-05
    Age : 32
    Location : Happy Place

    Coding Help Empty Re: Coding Help

    Post by Lizardman 2011-05-03, 16:11

    My sense of it:

    Get a fucking Latin dictionary. :3
    PureCraft
    PureCraft


    Posts : 3781
    Join date : 2010-12-12
    Age : 29
    Location : Germany

    Coding Help Empty Re: Coding Help

    Post by PureCraft 2011-05-03, 21:38

    No, get google translator <_>
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-03, 21:53

    Albuca wrote:if {}
    else if{}
    else if{}
    else if{}
    else {}

    Etc etc etc.
    Pm me your skype.

    -A

    No. Just no. If you want to add more words then rewrite the code to use, for example, a hashtable.
    Anyways if you STILL do not have this running... check you }'s... I can see at least 2 errors with them in the pasted code.

    Disregard that, it is just your horrible copy-paste work.

    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 22:16

    Ok, I'll look into hashtables, it just seems too complicated for an hour's worth of learning java. (I just went over while loops, scanners, and if statements)
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-03, 22:18

    Better to do it "complicated" first than to start off on the wrong foot and teach yourself some SERIOUSLY bad coding practices.
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-03, 22:20

    Yeah that's why I posted it here, because even I could tell that hundreds of if statements would be a horrible technique.
    Albuca
    Albuca
    In-Game Staff
    In-Game Staff


    Posts : 1205
    Join date : 2011-02-18
    Age : 31
    Location : 192.168.1.109

    Coding Help Empty Re: Coding Help

    Post by Albuca 2011-05-03, 22:32

    FillerB wrote:
    Albuca wrote:if {}
    else if{}
    else if{}
    else if{}
    else {}

    Etc etc etc.
    Pm me your skype.

    -A

    No. Just no. If you want to add more words then rewrite the code to use, for example, a hashtable.
    Anyways if you STILL do not have this running... check you }'s... I can see at least 2 errors with them in the pasted code.

    Disregard that, it is just your horrible copy-paste work.


    Hey now, I gave him the nitty gritty, super basic run down of how to do if-else's.
    Its not pretty, and its not supposed to be XP.
    If he wanted to get more complicated he could read/write the words to a file, and have 4 options (lookup, add, delete, exit).

    -A
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-03, 22:35

    And as I said: it's seriously bad practice. While legal you shouldn't do it if it isn't absolutely necessary.
    Ciphon
    Ciphon


    Posts : 1637
    Join date : 2011-03-06
    Age : 27
    Location : 156,68,1261

    Coding Help Empty Re: Coding Help

    Post by Ciphon 2011-05-04, 00:15

    Lizardman wrote:My sense of it:

    Get a fucking Latin dictionary. :3

    Lol Ikr I just can't make any sense out of this whatsoever
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-04, 00:18

    If you got nothing to add then why bother posting at all?
    Lizardman
    Lizardman


    Posts : 2846
    Join date : 2011-03-05
    Age : 32
    Location : Happy Place

    Coding Help Empty Re: Coding Help

    Post by Lizardman 2011-05-04, 01:54

    [quote=&quot;patrickfreed&quot;]I'd love to see Liz try to make sense of this :3[/quote]

    Dat's why I posted. :3

    Edit: why is the quote function fucked up? >.>
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-04, 13:58

    Is there such thing as a Class file executer GUI?
    I want to sell this to the freshman to help them out with latin, but I feel like navigating through the command line isn't too user friendly.


    Last edited by patrickfreed on 2011-05-04, 21:40; edited 1 time in total
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-04, 19:25

    Go look into Spring Framework.
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-05, 07:10

    What is it exactly?
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-05, 07:10

    It's a framework to make GUIs in Java. Not exactly newb friendly but it is the simplest way to make a GUI under Java that I know of. Then again I know very little of Java (more into the C's/Python)
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-05, 07:18

    Class Description wrote:VISUAL BASIC PROGRAMMING I & II(950, 952)/one semester each
    Students will learn to problem solve and think critically in the Visual Basic.Net graphical user interface (GUI) environment. No computer programming experience is required. The curriculum includes an introduction to Visual Basic .Net, Variables and Constants, Control Statements, Decisions Structures, Looping Structures, Procedures, Mathematical and Business Functions, Arrays and Structures, Color and Graphics, Creating Classes, Using Files, Sorting and Searching, and Advanced Topics if time permits. Advanced assignments include building user interactive games. Open to all students.

    I'm taking this next year, so I wanted to get some knowledge of coding, and since I play Minecraft, why not do Java?

    Anyways, to solve the GUI problem, I'll just have people plop the class into their C:\Users\Name directory, so all they have to do is type "Java Latin Dictionary"

    I'm following [You must be registered and logged in to see this link.] as a start, I don't know where I'll go from there.
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-05, 07:22

    Meh, I dislike the VB(A/.NET) syntax. While the basic ideas behind it are also in just about every modern language... dat syntax.
    sieve
    sieve


    Posts : 1371
    Join date : 2010-12-29
    Age : 29
    Location : Atlantis

    Coding Help Empty Re: Coding Help

    Post by sieve 2011-05-07, 10:18

    Im gonna try to take java classes over the summer, until then that may as well BE in latin
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-09, 13:04

    I learned about Arrays and decided to put them to a use. (This isn't really functional, it was just practice for me)
    It's a grade book average thingy. You start off with one grade of 100%, and then you can either add 1 more grade or 2 more.

    Let me know if there's any bugs if you do try it.
    [You must be registered and logged in to see this link.]
    Pete
    Pete


    Posts : 3194
    Join date : 2010-12-22
    Age : 29
    Location : Santiago, Chile

    Coding Help Empty Re: Coding Help

    Post by Pete 2011-05-09, 13:19

    I fail at coding... But the farthest I've got is with Python... ^_^
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-05-09, 15:38

    If you know one of them, you could easily pick up another though.


    I just learned Array Tables, so I coded a weight loss chart that spans 4 weeks.
    Again it's not very useful, but it is practice and might be helpful to people learning java if they read the source.

    [You must be registered and logged in to see this link.]
    embair
    embair


    Posts : 33
    Join date : 2011-04-09
    Age : 37
    Location : Brno, Czech Republic

    Coding Help Empty Re: Coding Help

    Post by embair 2011-05-09, 17:05

    I'm way late here, but can't help myself Smile
    Code:

    Word.equals("neco")
    returns boolean, no need to bother with compareTo.

    Also, hashtables and collections in general may seem complicated at first glance, but they are in fact plain simple to use
    Code:

    Map<String,String> dictionary = new HashMap<String,String>(); //create a dictionary (also called associative array, or map) that maps one string to another

    dictionary.put("Neco","Kill"); //stuff shit in

    System.out.println("Translation of Neco is " + dictionary.get("Neco")); //profit
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-05-09, 18:54

    That could use some error-handling Embair.

    patrickfreed wrote:If you know one of them, you could easily pick up another though.
    That's not completely true. Knowledge of ASM, SQL or Haskell (to name a few) is worth fuckall if your trying to learn C(++/#.NET) or Java.
    embair
    embair


    Posts : 33
    Join date : 2011-04-09
    Age : 37
    Location : Brno, Czech Republic

    Coding Help Empty Re: Coding Help

    Post by embair 2011-05-09, 19:07

    That it sure could Filler Smile, was just trying to show the basics as simply as possible. I probably should have shown the usage of directory.containsKey() right away though, you got point.

    Code:

    if (!directory.containsKey("Neco")) System.out.println("dunno how to translate Neco");
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-06-09, 15:37

    Exactly a one month necro...I feel dirty
    Anyways, can someone tell me what's going on in these videos?
    vids:
    From his faulty description of "this", I don't really understand what's going on in those.
    this description:
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-06-09, 19:57

    Not gonna watch the videos but judging from the titles it's about scope resolution.

    pseudocode ahoy
    Code:

    Class Dog{
      private String breed
      private Int age

      public Dog(string breed, int age)
          this.breed = breed
          this.age = age
    ]

    The this-keyword in the constructor is necessary for the compiler to differentiate between the parameter-breed and the instance variable-breed. So "this.breed = breed" mean set my instance variable "breed" to the given parameter called "breed". You could also do something like this and then the this-keyword wouldn't be necessary:
    Code:

    Class Dog
      private string iBreed
      private int iAge

      public Dog(string breed, int age)
          iBreed = breed
          iAge = age
    ]

    Getters and setters are just functions to manipulate the private instance variabels of a class. In the examples I used you would have for example getBreed, getAge() and setAge(int). setBreed(string) is possible but rather useless since the breed of a dog doesn't change.


    Last edited by FillerB on 2011-06-09, 23:36; edited 1 time in total (Reason for editing : spell/error check. Global -> Instance. Blame C.)
    embair
    embair


    Posts : 33
    Join date : 2011-04-09
    Age : 37
    Location : Brno, Czech Republic

    Coding Help Empty Re: Coding Help

    Post by embair 2011-06-09, 21:32

    I skimmed through the videos and gotta say that guy seems pretty clueless. Even if I overlook the fact that he is oblivous to the meaning of the this keyword. No decent java programmer would start a class name with lower case for instance. I guess the tutorial is somewhat usable, but be prepared that you might learn a bunch of bad coding practices in the process. But enough rumbling.

    For the general meaning of the this keyword
    1. Completely and thoroughly forget what the guy in the video says.
    2. Refer to Fillers post.

    As for the multiple constructors example in the video. It shows a special usage of this keyword, which is allowed only at the beginning of constructors. You got four constructors defined, but only one of them directly initializes the fields (the one that takes three parameters). Let's call him the smart guy. The other three constructors just know how to call the smart guy with some parameters. So, when you call

    tuna tunaObect = new tuna();

    the following happens:
    1. the constructor tuna() is called
    2. but he's like "meh, screw this, let the smart guy do it"
    3. thus, the constructor tuna(0,0,0) is called

    The second line illustrates the meaning of the this(0,0,0); statement.
    FillerB
    FillerB
    So long and thanks for all the fish!


    Posts : 4066
    Join date : 2011-01-30
    Age : 35
    Location : Netherlands

    Coding Help Empty Re: Coding Help

    Post by FillerB 2011-06-09, 23:14

    What I'm also surprised by is that he is using inline-ifs in a tutorial that is only now covering setters/getters, his references to "military" time instead of "normal" time and NOT using or showing Eclipse's generate getter/setter function.
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-06-10, 03:39

    embair wrote:tuna tunaObect = new tuna();

    the following happens:
    1. the constructor tuna() is called
    2. but he's like "meh, screw this, let the smart guy do it"
    3. thus, the constructor tuna(0,0,0) is called

    The second line illustrates the meaning of the this(0,0,0); statement.

    So if I were to put
    tuna tunaObject = new Tuna(5)
    the constructor this(h,0,0) would be called?
    embair
    embair


    Posts : 33
    Join date : 2011-04-09
    Age : 37
    Location : Brno, Czech Republic

    Coding Help Empty Re: Coding Help

    Post by embair 2011-06-10, 03:44

    Yep
    patrickfreed
    patrickfreed
    Forum Staff
    Forum Staff


    Posts : 2508
    Join date : 2011-01-13
    Age : 27
    Location : Washington DC, United States

    Coding Help Empty Re: Coding Help

    Post by patrickfreed 2011-06-10, 03:49

    Thanks guys.

    Sponsored content


    Coding Help Empty Re: Coding Help

    Post by Sponsored content


      Current date/time is 2024-05-20, 12:00