Help! Beginner at java coding! :O

Discussion in 'Computer Programming, Emulation, and Game Modding' started by Delta517, Apr 5, 2013.

Apr 5, 2013
  1. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    So I was just messing around Java today (since I found out that I learn stuff much easier by doing this) and when I tried to create a simple, and we are talking REALLY simple, password system it won't work! :nayps3:

    The problem is that when I type in the correct password it still says it's wrong :huh:

    Code:
    import java.util.Scanner;
     
    public class EksempelOppgave {
        public static void main (String[] args) {
            Scanner keyboardInput = new Scanner(System.in);
           
            String Attempt;
            String correctPassword = "Password";
           
            System.out.println("Enter correct password:");
            Attempt = keyboardInput.next();
           
            if (Attempt == correctPassword){
                System.out.println("Correct! You can enter!");
            }else{
                System.out.println("ERROR! Wrong password entered!");
            }
        }
    }
    
    If I change the line: " if (Attempt == correctPassword){" to have a number like "123" instead of correctPassword it works when entering "123". :unsure: I'm just a simple beginner, but I know that a lot of you guys are good at this, so please help? :)
     
  2. tj_cool

    Supervisor tj_cool Stuff

    Joined:
    Jan 7, 2009
    Messages:
    9,942
    Location:
    This planet
    Country:
    Belgium
    In java you need to compare strings with the equals method, not ==

    Code:
    if( Attempt.equals(correctPassword) ) {
       ...
    }
     
    Rydian likes this.
  3. purplesludge

    Member purplesludge anyone have any ideas for this space

    Joined:
    Mar 2, 2009
    Messages:
    1,047
    Location:
    wv
    Country:
    United States
    use the .equals method
     
  4. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    Ahh! Okay, thanks! :D So "==" is basically only for numbers? :)
     
  5. Issac

    Member Issac Mini-mod

    Joined:
    Apr 10, 2004
    Messages:
    4,454
    Location:
    Sweden
    Country:
    Sweden
    Yes. I think it'd work for single characters as well if I'm not mistaken. A string is an array of characters, and you can't use ">" "<" and "==" with arrays. the method "equals" compares two objects, whether these objects are arrays, numbers, strings, characters or self made classes (you'd have to overwrite the equals method for your own classes though)...

    Oh well :P
     
  6. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    Okay, thanks! :D

    I have alittle different problem now :P

    Code:
    import java.util.Scanner;
     
    public class main {
        public static void main(String[] args) {
            Scanner keyboardInput = new Scanner(System.in);
            int numberInput;
           
            System.out.println("Enter a number between 1-100...");
            numberInput = keyboardInput.nextInt();
           
            if(numberInput <= 50){
                System.out.println("The number is 50 or less");
            }else if(numberInput >= 51-100){
                System.out.println("The number is 51 or higher");
            }else if(numberInput >= 101){
                System.out.println("The number is higher than 100");
            }
        }
    }
    
    If I enter 101 or higher, it still only gives me "The number is higher than 51 or higher" :unsure: How do I make java search from 1 to 50 in one if statement :unsure:

    Something like this:

    if(numberInput = 1 to 50)
     
  7. Jamstruth

    Member Jamstruth Secondary Feline Anthropomorph

    Joined:
    Apr 23, 2009
    Messages:
    3,456
    Location:
    North East Scotland
    Country:
    United Kingdom
    "==" only works with "Primitive Types" these are the ones built into Java that don't need a capital letter at the start of their type announcement e.g. "int", "char", "boolean".
    Everything else needs ".equals(<Object>)" because there's a bunch more stuff to check other than the base value.

    Are you using Eclipse? Learn to use the breakpoints and debugger. They're lifesavers sometimes. The problem with that is the ordering and your if statement "else if(numberInput >= 51-100)" actually means "else if(numberInput >= -49" because it does the maths in the if statement. Since any number you put in is greater than that it will stop there.

    Since you can't check over a range to fix it you need to reverse the ordering. Check for highest first then move down if you're using ">" and check for lowest then move up if using "<" as well as changing your middle if statement to just be ">=50". If you did this it would remove the need for the top if statement like so.

    Code:
    if(numberInput > 100){
    System.out.println("The number is higher than 100");
           
            }
    else if(numberInput > 50){
                System.out.println("The number is 51 or higher");
            }
    else{
                System.out.println("The number is 50 or less");
            }
    The key to coding is thinking it through and knowing the logic of what you're doing. In Java it's more easily spelled out and read than in other languages (like C) but the principle is the same.

    Edit:
    Some misinformation here. The .equals() method will work for any object. Regardless if it is made by you or any other person.
     
    Delta517 likes this.
  8. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    Ahh! Ofcourse! :D I can't believe I was that stupid xD I always fail at the < and >, but thats why I'm practicing on it now, hehe :)

    Also to my defence the "else if(numberInput >= 51-100){" was just something I was messing around with and trying to get it to work :P Thanks for the answer :)
     
  9. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    Okay, so one more thing :) Im currently watching a guy's (Bucky) Youtube Java tutorials and I'm on part 15 which is about Methods with Parameters :) I can't really explain all what he's doing, since my english isn't that good, but here's the video:



    What I don't understand is the whole thing with Methods with Parameters, what they do and why they are needed? :unsure:
     
  10. Ron

    Member Ron somehow a weeb now.

    Joined:
    Dec 10, 2009
    Messages:
    2,837
    Location:
    here
    Country:
    Canada
    For example, if you want to pass on a value to a method, you have to add a parameter so the method knows about that value

    Like this
    Code:
     
    public void helloWorld(String arg0){
    System.out.println("Hello World" + arg0);
    }
     
    public static void main(String[] args){
    this.helloWorld("foobar");
    }
    
    By passing arg0 along, which is "foobar", helloWorld will also have knowledge of "foobar" and does stuff with it. In this case, it appends it to "Hello World".

    This might not be valid code, I forgot whether you can access this in a static method, probably not, but I don't have an IDE to help me, just typing from the message box. The principle is the same though, it's just the syntax that might be a little off.
     
  11. Delta517
    OP

    Member Delta517 Its okay...Im a ninja ;)

    Joined:
    Nov 25, 2008
    Messages:
    1,327
    Country:
    Norway
    Hmmm...I don't quite understand it, but do you mean that if I made two different classes and I would need to use an variable from the other class, I could use this? :unsure:
     
  12. JoostinOnline

    Member JoostinOnline Certified Crash Test Dummy

    Joined:
    Apr 2, 2011
    Messages:
    10,834
    Location:
    The Twilight Zone
    Country:
    United States
    I suggest you download Eclipse and optionally set it up with the Java API
     
  13. Jamstruth

    Member Jamstruth Secondary Feline Anthropomorph

    Joined:
    Apr 23, 2009
    Messages:
    3,456
    Location:
    North East Scotland
    Country:
    United Kingdom
    Yes. That's pretty much it.
     

Share This Page