Archive for the ‘Facebook Puzzles’ Category

Hoppity Hop! Facebook Puzzle in Java

This is a very simple facebook puzzle.I just started to solve the facebook puzzles.So started with simple problems..

Write a program that takes as input a single argument on the command line. This argument must be a file name, which contains a single positive integer. The program should read this file and obtain the integer within, and then output a sequence of strings based upon the number (details below).

Input specifications
The input file will contain a single positive integer (in base 10) expressed as a string using standard ASCII text (e.g. for example, the number “15” but without the double quotes). This number may or may not be padded on either side with white space. There will be no commas, periods, or any other non-numeric characters present within the number. The file may or may not terminate in a single new line character (“\n”).
An example input file is below:
15

Output specifications
The program should iterate over all integers (inclusive) from 1 to the number expressed by the input file. For example, if the file contained the number 10, the submission should iterate over 1 through 10. At each integer value in this range, the program may possibly (based upon the following rules) output a single string terminating with a newline.

* For integers that are evenly divisible by three, output the exact string Hoppity, followed by a newline.
* For integers that are evenly divisible by five, output the exact string Hophop, followed by a newline.
* For integers that are evenly divisible by both three and five, do not do any of the above, but instead output the exact string Hop, followed by a newline.

Example output (newline at end of every line):
Hoppity
Hophop
Hoppity
Hoppity
Hophop
Hoppity
Hop


import java.io.File;
import java.util.Scanner;

public class HoppityHop
{
 public static void main(String args[])throws Exception
 {
 String fileName = args[0]; // input file
 int line;
 Scanner fileScan, lineScan;
fileScan = new Scanner (new File(fileName));

 StringBuffer stringBuffer = new StringBuffer();
 while (fileScan.hasNext())
 {
 line = Integer.parseInt(fileScan.nextLine());
 hoppityHop(line);
 }
 }

 public static void hoppityHop(int number)
 {
 for(int i=1;i<=number;i++)
 {
 evenlyDivision(i);
 }
 }

 public static void evenlyDivision(int number)
 {
 int[] divident = {3,5};
 if(number % divident[0] == 0)
 {
 if(number%divident[1] == 0)
 {
 System.out.println("Hop");
 }
 else
 {
 System.out.println("Hoppity");
 }
 }
 else if(number%divident[1] == 0)
 {
 System.out.println("Hophop");
 }
 }
}
Advertisements