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, 2019 in Java by Dhanush
1,443 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, 2019 by sampriti
• 1,100 points

edited Aug 8, 2019 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,090 points
253 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,890 points
1,254 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 Parth
• 4,630 points
140 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21 in Java by kartik
• 20,680 points
154 views
+11 votes
12 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
244,048 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, 2019 in Java by sampriti
• 1,100 points
1,614 views
0 votes
1 answer

What is $stateProvider in AngularJs?

Routing is just another way of fixing some content ...READ MORE

answered Feb 11 in Angular by Niroj
• 44,220 points
601 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,890 points
3,712 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, 2019 in Java by sampriti
• 1,100 points
58 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, 2019 in Java by Sirajul
• 55,530 points
498 views