Help! Beginner at java coding! :O

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
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? :)
 

Issac

Iᔕᔕᗩᑕ
Supervisor
Joined
Apr 10, 2004
Messages
7,025
Trophies
3
Location
Sweden
XP
7,345
Country
Sweden
Ahh! Okay, thanks! :D So "==" is basically only for numbers? :)
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
 

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
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)
 

Jamstruth

Secondary Feline Anthropomorph
Member
Joined
Apr 23, 2009
Messages
3,462
Trophies
0
Age
31
Location
North East Scotland
XP
710
Country
"==" 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:
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)...
Some misinformation here. The .equals() method will work for any object. Regardless if it is made by you or any other person.
 
  • Like
Reactions: Delta517

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
Country
Norway
"==" 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.
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 :)
 

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
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:
 

chyyran

somehow a weeb now.
Developer
Joined
Dec 10, 2009
Messages
2,845
Trophies
1
Location
here
Website
ronnchyran.com
XP
1,076
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.
 

Delta517

Its okay...Im a ninja ;)
OP
Member
Joined
Nov 25, 2008
Messages
1,329
Trophies
0
Age
29
Website
Visit site
XP
1,180
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:
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    S @ salazarcosplay: how are you doing @K3Nv2