Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


How to add a check if entered number is even


hi! I need some help. I need user to enter the size of the figure to be printed out. It has to ba an even number (otherwise it will print out with error). I need to add a check if entered size is an even number, otherwise display an error message and prompt user for entering the size again. If it is an even number the program should proceed and print out a figure. Any suggestions for the piece of code I need to add? Many thanks in advance!  import java.util.*;   public class Question2 {       public int Size;       public static void main(String[] args) {           Scanner keyboard = new Scanner(System.in);          System.out.print("Please enter the size of a triangle you want to print out (it has to be an even number): ");          int Size = keyboard.nextInt();{         if (Size%2==0){          }      }           line(Size);          drawtop(Size);          middleline(Size);          drawbtm(Size);          line(Size);       }       public static void line(int Size) {           System.out.print("|");          for (int i = 1; i <= Size / 2; i++) {              System.out.print("-");          }          System.out.print("*");          for (int i = 1; i <= Size / 2; i++) {              System.out.print("-");          }          System.out.print("|");          System.out.println(" ");      }       public static void drawtop(int Size) {          for (int line = 1; line <= Size / 2; line++) {              System.out.print("|");               for (int space = 1; space <= Size / 2 - line; space++) {                  System.out.print(" ");              }               for (int star = 1; star <= (line * 2 + 1); star++) {                  System.out.print("*");              }               for (int space = 1; space <= Size / 2 - line; space++) {                  System.out.print(" ");              }               System.out.println("|");           }      }       public static void middleline(int Size) {          System.out.print("*");          for (int i = 1; i <= Size / 2; i++) {              System.out.print("**");          }          System.out.println("**");      }       public static void drawbtm(int Size) {          for (int line = 1; line <= Size / 2; line++) {              System.out.print("|");               for (int space = 1; space <= line - 1; space++) {                  System.out.print(" ");              }               for (int star = -1; star <= (2 * Size / 2 - 2 * line) + 1; star++) {                  System.out.print("*");              }               for (int space = 1; space <= line - 1; space++) {                  System.out.print(" ");              }               System.out.println("|");           }      }  }

Its really unbelievable that if you have written the code of printing triangles by yourself, how you got stuck at a trivial problem like this. Please just dont copy and paste the code for completing your assignments.  Anyway the solution is trivial and I am giving you a work-through-:  Think what the  if condition is doing in the below code : if (Size%2==0){  }   It does nothing.  You need a condition in which to print a message to ask user to input an even number and ask again if the number is not even. Doesnt this look like that u need a loop, And you dont know the how many times to loop but you do know when to end the loop. SO you need a WHILE loop with appropriate condition checked of even number.  Now I hope you can complete the program.

{quote:title=J2EE_Life wrote:} And you dont know the how many times to loop but you do know when to end the loop. SO you need a WHILE loop   Well, any loop will work, but a while or do/while is most common for this situation.

If your query is solved, Mark this thread as ANSWERED so that others visiting this forum with similar problems could be benefited. Otherwise let us know what other help you need in order to solve it...


Related Links

Extract parameters from a WAV file?
LinkedHashMap problem (Same key but different value)
A simple question
Python vs. Jython
Mortgage Calculator Help
Call method every 5 minutes
Quantum Processor (8 GflopS) trade release announced
i want a Java program that confirms the first three letters are alpha
Quick query about loop statements
Using methods and classes...
Can someone show me what i'm wrong?
Java Utility- Populate Access Database using several .txt files
Error 2753 regutils.dll
Can't convert int into String
How much should coding conventons be applied?
Reading From File And Sum of Last 2 Integers