/** This program applies random html formatting to each line of a text file. */ import java.io.*; import java.util.*; public class html { private static Random rand = new Random(); 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(new FileWriter(name + ".html")); String line; while (infile.hasNext()) { line = infile.nextLine(); outfile.println(format(line)); } outfile.close(); } /** Create an html 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; for (int i = 1; i <= 3; i++) { c = codes.charAt(rand.nextInt(6)); 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(6) + 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 } }