{"id":21195,"date":"2016-09-28T09:50:58","date_gmt":"2016-09-28T09:50:58","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=21195"},"modified":"2024-12-14T04:03:29","modified_gmt":"2024-12-14T04:03:29","slug":"compare-two-strings-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/compare-two-strings-java-program\/","title":{"rendered":"Compare Two Strings Java Program"},"content":{"rendered":"<h1>Compare Two Strings Java Program<\/h1>\n<p>In this post, we will learn how to compare two Strings using a simple Java program. We can use the <strong>String<\/strong> class in-built method <strong>compareTo()<\/strong> method to compare strings. The method returns zero if the strings are equal.<\/p>\n<p>The method signature is as follows:<\/p>\n<p>public int compareTo(String anotherString)<\/p>\n<h2>Java program<\/h2>\n<pre>package com.testingdocs.demo;\r\n\r\nimport java.util.Scanner;\r\n\r\npublic class SampleProgram {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\/\/declare Two String variables\r\n\tString str,anotherStr;\r\n\tScanner in = new Scanner(System.in);\r\n\r\n\t\/\/prompt for input strings\r\n\tSystem.out.print(\"Enter String 1: \");\r\n\tstr = in.nextLine();\r\n\tSystem.out.print(\"Enter String 2: \");\r\n\tanotherStr = in.nextLine();\r\n\tSystem.out.println(\"-----Program Output----\");\r\n\t\/\/compare the strings \r\n\tif(str.compareTo(anotherStr) == 0)\r\n\t\tSystem.out.println(\"The Two Strings are Equal.\");\r\n\telse\r\n\t\tSystem.out.println(\"The Two Strings are NOT Equal.\");\r\n\t}\r\n}\r\n\r\n<\/pre>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-21198\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings.png\" alt=\"Compare Two Strings\" width=\"1918\" height=\"981\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings.png 1918w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings-300x153.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings-1024x524.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings-768x393.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Compare-Two-Strings-1536x786.png 1536w\" sizes=\"auto, (max-width: 1918px) 100vw, 1918px\" \/><\/p>\n<h2>Sample Output<\/h2>\n<p>Save the program in the IDE. Run the program to verify the program output.<\/p>\n<p>Right click and choose <strong>Run As &gt;&gt; Java Application<\/strong><\/p>\n<h3>TestCase #1<\/h3>\n<p>Enter String 1: TestingDocs<br \/>\nEnter String 2: TestingDoc<br \/>\n&#8212;&#8211;Program Output&#8212;-<br \/>\nThe Two Strings are NOT Equal.<\/p>\n<h3>TestCase #2<\/h3>\n<p>Enter String 1: TestingDocs.com<br \/>\nEnter String 2: TestingDocs.com<br \/>\n&#8212;&#8211;Program Output&#8212;-<br \/>\nThe Two Strings are Equal.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compare Two Strings Java Program In this post, we will learn how to compare two Strings using a simple Java program. We can use the String class in-built method compareTo() method to compare strings. The method returns zero if the strings are equal. The method signature is as follows: public int compareTo(String anotherString) Java program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-21195","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=21195"}],"version-history":[{"count":13,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21195\/revisions"}],"predecessor-version":[{"id":26365,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/21195\/revisions\/26365"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=21195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=21195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=21195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}