Click here to Skip to main content
15,041,266 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?
   
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
   
Does it? Are you sure?
What - exactly - does it compare?

Trust me, this is important that you learn!
OriginalGriff 20-Jul-21 4:09am
   
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
   
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
[no name] 20-Jul-21 4:15am
   
Sorry but what you are trying to say, l am not getting it. Can you please tell how to finish my successful program?
Richard MacCutchan 20-Jul-21 4:23am
   
Do what I told you a week ago, and actually learn Java.
OriginalGriff 20-Jul-21 4:33am
   
I could ... but that wouldn't help you because you wouldn't understand why - and that means that next week's homework (which assumes you know this stuff from this homework) is harder to do, and takes you even longer. And loads more help from us.

Seriously: what - exactly - does "==" compare?
If the "two pockets" aren't the same, would "==" tell you they were?
[no name] 20-Jul-21 5:05am
   
I got it you mean to say == will not work in String instead l need to use equals ()? Thanks
OriginalGriff 20-Jul-21 5:36am
   
Yes, but do you know why? That's really kinda important ...
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 ");
    }
  }
}
   
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
   
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
[no name] 20-Jul-21 5:14am
   
One last favour l want that please tell me that what l will do if in this same project if l'll enter no instead of yes then it will say sorry and also l want to keep multiple names and ages and then it will act according to that, please help me
OriginalGriff 20-Jul-21 5:16am
   
Google Translate ["Student" to "English"]: "please do my homework for me"
[no name] 20-Jul-21 5:25am
   
It simply means that if l enter different name and age then the cuteness level will be different. So how to do that?? Please help me
CPallini 20-Jul-21 5:31am
   
You simply need an if-chain, see, for instance:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
[no name] 20-Jul-21 6:04am
   
Thanks, l understood that in that case l need to use if else statement. Thanks once again
CPallini 20-Jul-21 6:14am
   
You are welcome.
Patrice T 22-Jul-21 3:56am
   
+5
CPallini 22-Jul-21 4:00am
   
Thank you.

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