• Post Reply Bookmark Topic Watch Topic
  • New Topic

If statement help - checking/ comparing two strings from user input  RSS feed

 
faiz jethwa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone. I have just registered on this forum and I need to pick your brains please. I am a newbie java programmer. I have only been using java for a few days. I have a small problem which I have tried hard to solve but after many hours of changing and chopping the code I don't seem to be getting any closer to the solution.

All I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.

Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement. Please can someone help. Most of you will be expert programmers on here. If you are able to provide the solution, please can you also write me a few lines of explanation to go with the code so I can learn from this exercise. Thank you very much to you all.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiz,
Welcome to CodeRanch! I added [ code ] tags to make the code easier to read.

I think the problem is here:


In Java, we are suppose to use a method to compare Strings:


The difference is that == compares whether the same memory is used but equals() compares the actual values. == works fine for primitives like int. You know it is a primitive because the type name begins with a lowercase.
 
faiz jethwa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:Faiz,
Welcome to CodeRanch! I added [ code ] tags to make the code easier to read.

I think the problem is here:


In Java, we are suppose to use a method to compare Strings:


The difference is that == compares whether the same memory is used but equals() compares the actual values. == works fine for primitives like int. You know it is a primitive because the type name begins with a lowercase.


Hi, Thanks for your reply. I have amended the code but I am still getting the same error. Please can you advise further. Once again thanks for all the help in advance

Here is the revised code
import java.io.*;

public class Hellohuman
{

public static void main (String[] args)
{
String mystringone="alice";
String mystringtwo="bob";

try {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader bfr = new BufferedReader(isr);
System.out.println ("what is your name");
String sInput=bfr.readLine();
System.out.println("your name is " +sInput);

}


catch(Exception e)
{
System.out.println ("error has occurred");
}



if (mystringone.equals(sInput))


{
System.out.println("welcome to the system alice");
}

}


}
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faiz jethwa wrote:If you are able to provide the solution, please can you also write me a few lines of explanation to go with the code so I can learn from this exercise.

That's not how this site works. We will give you tips and point you to reference material that you can read to get more understanding but you have to do the work.

Also, please UseCodeTags (←click on that link to learn how) when you post code so that it's easier for other people to read your code and help you.

And please be more specific than just "it doesn't work" or "I get the same error." Copy/paste the exact error message you get when you compile or run your program. If you're getting an exception at run time, tell us the exact input that you gave to the program to produce the error.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faiz Jethwa welcome to the Ranch again.

Tthere is an explanation about the == operator here. To put it simply, == doesn't work on reference types.
Please take note of what you were told about the code button. Such code with the formatting hidden is difficult to read.

Lakshya Gupta welcoke to the Ranch
Please read this which appears on this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Simply posting code will deprive the OP of the chance to learn fpr themselves. Don't be annoyed with me, but I have pulled rank and deleted your post. FJ will learn better by working out the answer.
 
faiz jethwa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






Thanks all for your kind support and guidance. I have now managed to solve the problem.











Campbell Ritchie wrote:Faiz Jethwa welcome to the Ranch again.

Tthere is an explanation about the == operator here. To put it simply, == doesn't work on reference types.
Please take note of what you were told about the code button. Such code with the formatting hidden is difficult to read.

Lakshya Gupta welcoke to the Ranch
Please read this which appears on this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Simply posting code will deprive the OP of the chance to learn fpr themselves. Don't be annoyed with me, but I have pulled rank and deleted your post. FJ will learn better by working out the answer.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
Please show us the solution.
 
faiz jethwa
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faiz jethwa wrote: . . .
Here is the final solution. . . .
It still need editing, I am afraid. You need to remove the unnecessary comments, and also get the indentation right. Lost all those multiple blank lines which don't add to legibility.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!