{"id":190493,"date":"2025-02-12T10:33:52","date_gmt":"2025-02-12T10:33:52","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=190493"},"modified":"2025-02-12T10:33:55","modified_gmt":"2025-02-12T10:33:55","slug":"exception-in-thread-main-java-lang-error-unresolved-compilation-problem","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/12\/exception-in-thread-main-java-lang-error-unresolved-compilation-problem\/","title":{"rendered":"Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem"},"content":{"rendered":"\n<p>Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem:<\/p>\n\n\n\n<p>Duplicate local variable sc<\/p>\n\n\n\n<p>at ZellerCongruence.main(ZellerCongruence.java:13)<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>The error you&#8217;re encountering, <code>Duplicate local variable sc<\/code>, is caused by declaring the same local variable <code>sc<\/code> more than once within the same scope in your Java program. To resolve this error, you need to ensure that you only declare a variable once within the scope in which it is being used. In this case, it looks like <code>sc<\/code> is being redeclared in the same method or block.<\/p>\n\n\n\n<p>Here&#8217;s how you can fix the issue:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Fix:<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Scanner;\n\npublic class ZellerCongruence {\n    public static void main(String&#91;] args) {\n        \/\/ Create a Scanner object to read input\n        Scanner sc = new Scanner(System.in);\n\n        \/\/ Take year, month, and day as input from the user\n        System.out.print(\"Enter year: \");\n        int year = sc.nextInt();\n\n        System.out.print(\"Enter month: \");\n        int month = sc.nextInt();\n\n        System.out.print(\"Enter day: \");\n        int day = sc.nextInt();\n\n        \/\/ Adjust month and year for January and February\n        if (month &lt;= 2) {\n            month += 12;\n            year--;\n        }\n\n        \/\/ Zeller's congruence formula\n        int k = year % 100;\n        int j = year \/ 100;\n\n        int h = (day + (13 * (month + 1)) \/ 5 + k + k \/ 4 + j \/ 4 + 5 * j) % 7;\n\n        \/\/ Output the result\n        System.out.println(\"The day of the week is: \" + dayOfWeek(h));\n    }\n\n    \/\/ Method to convert Zeller's output to day of the week\n    public static String dayOfWeek(int h) {\n        switch (h) {\n            case 0: return \"Saturday\";\n            case 1: return \"Sunday\";\n            case 2: return \"Monday\";\n            case 3: return \"Tuesday\";\n            case 4: return \"Wednesday\";\n            case 5: return \"Thursday\";\n            case 6: return \"Friday\";\n            default: return \"\";\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Scanner object initialization<\/strong>: The <code>Scanner sc = new Scanner(System.in);<\/code> initializes the scanner to read input from the user.<\/li>\n\n\n\n<li><strong>Input values<\/strong>: The user is prompted to input a year, month, and day.<\/li>\n\n\n\n<li><strong>Zeller\u2019s Congruence Formula<\/strong>: The formula calculates the day of the week for any given date. This algorithm requires modifications to the month and year if the month is January or February, as those months are treated as the 13th and 14th months of the previous year.<\/li>\n\n\n\n<li><strong>Modulo operation<\/strong>: The formula calculates the value <code>h<\/code>, which corresponds to the day of the week, ranging from 0 (Saturday) to 6 (Friday).<\/li>\n\n\n\n<li><strong>Day of the Week<\/strong>: A switch statement is used to print the corresponding day of the week based on the value of <code>h<\/code>.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Resolving the error:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Duplicate Declaration<\/strong>: The error occurs if the <code>sc<\/code> variable is declared multiple times in the same scope. Make sure that you declare it only once and reuse it for the input process.<\/li>\n<\/ul>\n\n\n\n<p>I\u2019ll generate a visual representation of Zeller&#8217;s Congruence formula for you. Let me get that ready!<\/p>\n\n\n\n<p>Here\u2019s the diagram that illustrates Zeller\u2019s Congruence formula for calculating the day of the week. It shows the components of the formula, such as year, month, day, and adjustments for January and February, as well as the calculation of &#8216;h&#8217;. This visual should help you understand how the algorithm works in a step-by-step manner.<\/p>\n\n\n\n<p>Let me know if you need further clarification!<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/02\/image-402.png\" alt=\"\" class=\"wp-image-190494\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Exception in thread &#8220;main&#8221; java.lang.Error: Unresolved compilation problem: Duplicate local variable sc at ZellerCongruence.main(ZellerCongruence.java:13) The Correct Answer and Explanation is : The error you&#8217;re encountering, Duplicate local variable sc, is caused by declaring the same local variable sc more than once within the same scope in your Java program. To resolve this error, you need [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-190493","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/190493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=190493"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/190493\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=190493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=190493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=190493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}