CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Bryce Lindley
Level 9
Olympia
  • 21.02.2019
  • 1514views
  • 8comments

Getting Errors in this code. Not sure why but the ELSE-IF statements are throwing errors.

Question about the task Minimum of two numbers
Java Syntax,  Level 4,  Lesson 6
Resolved


Use the keyboard to enter two integers, and display the minimum. If the two numbers are equal, display either of them.

Requirements:
  • The program should read the numbers from the keyboard.
  • The program must display a number on the screen.
  • The program should display the minimum of two integers.
  • If the two numbers are equal, display either of them.
package com.codegym.task.task04.task0418; /* Minimum of two numbers */ import java.io.*; public class Solution { public static void main(String[] args) throws Exception { //write your code here BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String a = reader.readLine(); String b = reader.readLine(); int number1 = Integer.parseInt(a); int number2 = Integer.parseInt(b); if (number1 < number2) { System.out.println(number1); if (number2 < number1) { System.out.println(number2); if (number1 == number2) { System.out.println(number2); } } } } }
+2
Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
21 February 2019, 14:41
Delete line 20-28 and copy and paste this into their place:
if (number1 < number2)
   System.out.println(number1);
else if (number2 < number1)
   System.out.println(number2);
else
   System.out.println(number2);
When an if statement has exatly one line that it executes, you do not need to enclose it in curly brackets. I think the big problem that you were having is that you were enclosing too much in the curly brackets. For example, not anything to do with your code, this code does not need the brackets:
if (number1 < number2)
   System.out.println(number1); // only one line
While this code you will need them:
if (number1 < number2){
   number1++;
   System.out.println(number1);
   break;
   // this if block executes 3 lines of code, you need them enclosed.
}
+2
Bryce Lindley
Level 9 , Olympia, United States
22 February 2019, 03:05
I was also putting an arguments for the ELSE statement so that was giving me grief. The IF's don't end the method so I finally realized the importance of Else-IF and then finally ELSE. Curly brackets are pretty hard to manage. Thanks!
+2
Abdur Rehman
Level 7 , Faisalabad, Pakistan
4 April 2019, 16:20
nothing of this working i even tried copying and pasting but i think this task is bugged
0
Abdur Rehman
Level 7 , Faisalabad, Pakistan
4 April 2019, 16:25
i get it now i was running but it was not running so decided to verify anyway and it worked i dont know y but it seems a bit strange can u explain this
0
Guadalupe Gagnon
Level 37 , Tampa, United States
4 April 2019, 17:04
download and use intelliJ
+1
Abdur Rehman
Level 7 , Faisalabad, Pakistan
5 April 2019, 09:30
i have downloaded it but when i run a code it says sdk not found
0
Guadalupe Gagnon
Level 37 , Tampa, United States
5 April 2019, 13:31
https://codegym.cc/quests/lectures/questsyntax.level03.lecture01
0
Thomas
Level 11 , Scottsdale, United States
26 May 2019, 17:26
Cleaning out all the unnecessary { } worked fine Thanks --- was losing it Sequence mattered too: if else if else
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.