My First program, I need help.
I'm trying to make it so you type in you username, then you type it in later
and if the two match then it prints logged in succesfully, but it's basically saying the two don't match..?
import java.util.Scanner;
class Der{
public static void main(String[]arguments)
{
Scanner key = new Scanner(System.in);
System.out.println("Set your Username: ");
String UserName = key.nextLine();
Scanner key2 = new Scanner(System.in);
System.out.println("Enter your UserName");
String EnteredUserName = key2.nextLine();
if (EnteredUserName == UserName)
{
System.out.print("Logged in Successfully");
}
}
}
Re: My First program, I need help.
Hello Tyridge77!
You should use the String's equals() method instead of the == operator when comparing Strings.
And for your next posts be sure you use the code tags to get highlighting in your code.
Re: My First program, I need help.
Quote:
Originally Posted by
andreas90
Hello Tyridge77!
You should use the
String's equals() method instead of the
== operator when comparing
Strings.
And for your next posts be sure you use the
code tags to get highlighting in your code.
I tried the equals mehod, and it still isn't working..
------
Code :
import java.util.Scanner;
class Der{
public static void main(String[]arguments)
{
Scanner key = new Scanner(System.in);
System.out.println("Set your Username: ");
String UserName = key.nextLine();
Scanner key2 = new Scanner(System.in);
System.out.println("Enter your UserName");
String EnteredUserName = key2.nextLine();
String a = key.nextLine();
String b = key2.nextLine();
if(a.equals(b)){
System.out.print("Logged in Successfully");
}
}
}
Re: My First program, I need help.
Try debugging the code by adding a println statement that prints out the values of a and b so you can see what the computer sees:
System.out.println("a="+a + "<\nb=" + b +"<");
Add this just before the if statement.
Re: My First program, I need help.
Quote:
Originally Posted by
Tyridge77
I tried the equals mehod, and it still isn't working..
Your code is working for me as expected. You are probably entering different Strings for a and b (a space after one of the inputs might be the problem). Try printing them as Norm suggested and you 'll find out what the problem is.
Re: My First program, I need help.
//You do not need to create key and key2. One will work.
import java.util.*;
class Application{
public static void main(String[]arguments)
{
Scanner key = new Scanner(System.in);
System.out.print("Set your Username: ");
String UserName = key.nextLine();
System.out.print("Enter your UserName");
String EnteredUserName = key.nextLine();
if(UserName.equals(EnteredUserName)){
System.out.print("Logged in Successfully");
}
else
System.out.println("Unsucessful"); just to check whether it works or not you can delete it later
}
}
Re: My First program, I need help.
@kindk12
Please Edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Also use code that follows the java programming standards.