import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner; import java.io.IOException; import java.util.Random; /** This program applies random html formatting to each line of a text file. */ public class Html { /** Random variable for generating data by all methods. */ private static Random rand = new Random(); /** Use as a limit in several methods. */ private static final int MAX = 6; /** The main method where execution begins. @param args not used. @throws IOException if there are file issues */ public static void main(String[] args) throws IOException { Scanner keyboard = new Scanner(System.in); System.out.print("enter name of input text file: "); String name = keyboard.nextLine(); Scanner infile = new Scanner(new FileReader(name)); PrintWriter outfile = new PrintWriter((name + ".html")); String line; while (infile.hasNext()) { line = infile.nextLine(); outfile.println(format(line)); } outfile.close(); } /** Create an html randomly formatted line of text. @param line the original line @return the formatted line */ public static String format(String line) { String result = line; result = color(result); result = heading(result); result = centering(result); return result; } /** Wrap a random html color code around a string. @param s the string to format @return the color formatted string */ public static String color(String s) { String result = ""; String codes = "0369CF"; char c; final int parts = 3; for (int i = 1; i <= parts; i++) { c = codes.charAt(rand.nextInt(MAX)); result = result + c + c; } return "" + s + ""; } /** Wrap a random html heading: h1 - h6 around a string. @param s the string to format @return the heading formatted string */ public static String heading(String s) { String hd = "h" + (rand.nextInt(MAX) + 1); return "<" + hd + ">" + s + ""; } /** Randomly decide whether to center a string in html. @param s the string @return the string, possibly with center tags */ public static String centering(String s) { if (rand.nextBoolean()) { return "
" + s + "
"; } return s; // no centering } }