how to compare two words in java?

0 votes

Hey, I was trying to compare two words in java but I am not able to do that , Continuously I am getting an error.

public class Compare {

    public static void main(String args[])

    {

        String string1 ="Garments" ;

        String string2 = "Dog";

        

        

        System.out.println("Comparing " + string1 + " and " + string2

                           + " : " + string1==string2));

  
Aug 7 in Java by Dhanush
367 views

1 answer to this question.

0 votes

hey,

 I think in your code the function you used to compare the words its not right,

So you can check your code with .equals() function

  public static void main(String args[])

    {

        String string1 ="Garments" ;

        String string2 = "Dog";

        

        

        System.out.println("Comparing " + string1 + " and " + string2

                           + " : " + string1.equalsIgnoreCase(string2));

Thank you

answered Aug 7 by sampriti
• 1,100 points

edited Aug 8 by Kalgi

Related Questions In Java

0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,050 points
135 views
0 votes
2 answers

How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE

answered Jul 19, 2018 in Java by Sushmita
• 6,880 points
620 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,640 points
66 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 10,197 views
+8 votes
11 answers

Default parameters of XMS and XMX in JVM

You use these Java command-line parameters to ...READ MORE

answered Nov 13, 2018 in Java by Murali
125,297 views
0 votes
1 answer

How to add items in list at a time?

Hi Priyanka , I think this code snippet ...READ MORE

answered Aug 9 in Java by sampriti
• 1,100 points
433 views
0 votes
3 answers

Does Java support Default Parameters?

You can try this with method overloading. void ...READ MORE

answered Sep 21, 2018 in Java by Sushmita
• 6,880 points
886 views
0 votes
1 answer

Is it possible to create a memory leak with Java

Here's a good way to create a memory ...READ MORE

answered May 30, 2018 in Java by parth295
• 4,640 points
520 views
0 votes
1 answer

How can I compare two integers properly?

Hi Divya, I hope this code snippet will ...READ MORE

answered May 31 in Java by sampriti
• 1,100 points
34 views
0 votes
2 answers

How do I get distinct element from list in java?

You should use a Set. it doesn't contain duplicates. If ...READ MORE

answered Aug 23 in Java by Sirajul
• 37,480 points
77 views