Click here to Skip to main content
15,446,232 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is my program -

Java
import java. util. Scanner; 
public class Rudra  {
public static void main  (String  [] args) {



Scanner someObj = new Scanner  (System.in); 

System. out. println  (".............. "); 
System. out. println  ("Are you new to here? "); 
String answer = someObj.nextLine (); 

System. out. println  ("Your name: "); 
String subAnswer = someObj. nextLine (); 

System.out. println  ("Your age:"); 
int lastAnswer = someObj.nextInt  (); 

if ( subAnswer == "Shruti" && lastAnswer == 23) {
            System.out.println  (subAnswer + "your cuteness is 82 percent "); 
} else {
        System. out. println  ("Invalid "); 
}


}
}





This is my program and l want that it will ask name and age and tell the cuteness level. But again there is a problem that it asks are you new to here and then l enter yes and then it asks name and age and when l enter age 23 it says invalid. Why?
I am very close to my project please help me. I will not repost these type of questions after this please help me...

What I have tried:

.............................................................
Posted
Updated 19-Jul-21 21:49pm
v2

How many times are you going to ask the same question, without listening to what we tell you?

Debugging your apop is part of your task: writing the code is the easy bit, making sure it works and fixing it when it doesn't is the longer part.

So grab your debugger, and start looking at exactly what is going on.
Hint: What does the documentation tell you about the "==" operator?
 
Share this answer
 
Comments
[no name] 20-Jul-21 3:43am    
It tells that if this variable is = something, it will give true
OriginalGriff 20-Jul-21 4:06am     CRLF
Does it? Are you sure? What - exactly - does it compare? Trust me, this is important that you learn!
OriginalGriff 20-Jul-21 4:09am     CRLF
I have a pocket in my jeans: it contains one item: a crisp, white handkerchief. You also have a pocket in your jeans: it also contains one item: a crisp, white handkerchief. Does that mean that the two pockets are in the same pair of jeans?
[no name] 20-Jul-21 3:48am     CRLF
Sorry sir please help me last time once my project is complete, l will never ask these type of questions or repost. Just you tell me why it is coming so and l also tried this--- if else ( subAnswer ! = "Shruti" && lastAnswer != 23) { System. out. println ("invalid ") } Again l tried if else statement instead of else but again not working. It asks till age but after that it doesn't tells cuteness.
[no name] 20-Jul-21 4:14am    
No
Your problem is on string comparison. See, for instance (https://www.baeldung.com/java-compare-strings[^]). Try
Java
import java. util. Scanner;
public class Rudra
{
  public static void main  (String  [] args)
  {
    Scanner scanner = new Scanner  (System.in);

    System. out. println  (".............. ");
    System. out. println  ("Are you new to here? ");
    String answer = scanner.nextLine ();

    System. out. println  ("Your name: ");
    String subAnswer = scanner. nextLine ();

    System.out. println  ("Your age:");
    int lastAnswer = scanner.nextInt  ();

    if ( subAnswer.equals("Shruti") && lastAnswer == 23)
    {
      System.out.println  (subAnswer + " your cuteness is 82 percent ");
    }
    else
    {
      System. out. println  ("Invalid ");
    }
  }
}
 
Share this answer
 
Comments
[no name] 20-Jul-21 4:13am    
You mean to say that l need to put scanner object?
CPallini 20-Jul-21 4:26am     CRLF
I mean you should use the equals() method, instead of the == operator, to compare the strings. Renaming 'someObj' to 'scanner' is just a matter of style.
[no name] 20-Jul-21 5:01am    
Oh, you mean to say instead of this - subAnswer == "Shruti", l need to write equals in String? == operator will not work in String?
[no name] 20-Jul-21 5:02am    
If that's true then excellent, you got me out of my problem. You 't amazing
[no name] 20-Jul-21 4:13am    
Then it will work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900