Write program that prints numbers from 1 to X

Write program that prints numbers from 1 to X

Assignment question: Write a program that prints the numbers from 1 to X. But for multiples of 2 print “Fiddle” instead of the number and for multiples of 5 print “Faddle” instead of the number. For numbers which are multiples of both 2 and 5 print “Fiddle Faddle” instead of the number.

  • The program should be constructed in Java 1.7 or below.
  • The program should prompt for and accept user input for X from the command line.
  • The program should do basic input validation on what the user enters (e.g. -1 and X are invalid input)
  • The program should exit when the user enters ‘quit’

Your deliverable: Please provide the source code as a well formed maven project including any tests as well as instructions for execution, if necessary.

Example program execution:

Enter an integer > 0: X
Invalid input ‘X’
Enter an integer > 0: -1
Invalid input ‘-1’
Enter an integer > 0: 0
Invalid input ‘0’
Enter an integer > 0: 1
1
Enter an integer > 0: 10
1
fiddle
3
fiddle
faddle
fiddle
7
fiddle
9
fiddle faddle
Enter an integer > 0: quit

Solution:

  • Project structure:

Write program that prints numbers from 1 to X

  • Create maven project name: PrintNumbers in eclispe
  • Create PrintNumbers.java class inside com.fiddle.printnumbers package and copy paste below code:
/*
 * PrintNumbers.java
 * Version 1.0 
 */
package com.fiddle.printnumbers;

import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * This program prints the numbers from 1 to X. But for multiples of 2 print
 * "Fiddle" instead of the number and for multiples of 5 print "Faddle" instead
 * of the number. For numbers which are multiples of both 2 and 5 print
 * "Fiddle Faddle" instead of the number.
 * 
 * The program has been constructed in jdk1.7.0_60 The program should prompt for
 * and accept user input for X from the commandline The program should do basic
 * input validation on what the user enters (e.g. -1 and X are invalid input)
 * The program should exit when the user enters 'quit'
 * 
 * 
 * 
 */
public class PrintNumbers {

    private static final String INVALID_INPUT = "Invalid input '";
    private static final String ENTER_AN_INTEGER_0 = "Enter an integer > 0:";
    private static final String FIDDLE_FADDLE = "Fiddle Faddle";
    private static final String FADDLE = "Faddle";
    private static final String FIDDLE = "Fiddle";
    private static Scanner scanner = new Scanner(new InputStreamReader(System.in));
    private static String input = null;

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {

            while (true) {

                System.out.print(ENTER_AN_INTEGER_0);
                input = scanner.nextLine();

                if (input.equalsIgnoreCase("quit")) {
                    Runtime.getRuntime().exit(0);
                }

                int inputValue = 0;

                if (isInteger(input)) {
                    inputValue = Integer.parseInt(input);
                } else {
                    System.out.println(INVALID_INPUT + input + "'");
                    continue;
                }

                if (inputValue < 1) {
                    System.out.print(INVALID_INPUT + inputValue + "'\n");
                    continue;
                }

                int[] array = new int[inputValue];

                for (int i = 0; i < array.length; i++) {
                    array[i] = i + 1;

                }

                printOutput(array);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param array
     */
    private static void printOutput(int[] array) {
        for (int i : array) {

            if (i == 1) {
                System.out.println(i);
            } else if (i % 2 == 0 && i % 5 == 0) {
                System.out.println(FIDDLE_FADDLE);
            } else if (i % 2 == 0) {
                System.out.println(FIDDLE);
            } else if (i % 5 == 0) {
                System.out.println(FADDLE);
            } else {
                System.out.println(i);
            }

        }
    }

    /**
     * @param input
     * @return
     */
    public static boolean isInteger(String input) {
        try {
            Integer.parseInt(input);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

}

Output:
Write program that prints numbers from 1 to X

 

Note: To run this program from command line please use PrintNumber.bat file which is included in project for download. Below is content of bat file:

CD /d C:\temp\PrintNumbers\src\main\java
start cmd /k echo To run program please enter: java com/fiddle/printnumbers/PrintNumbers and press enter

download2 Download project:  PrintNumbers

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *