ok this is what I did so far but it doesnt work anyone know why
//< = < >=> cos102 slocum h shah
import java.io.*;
import java.util.*;
public class SyntaxHighlighter {
File javaFile;
File htmlFile;
public SyntaxHighlighter(){
javaFile = new File("file.java");
htmlFile = new File("file.html");
}
public SyntaxHighlighter(File javaFile, File htmlFile){
javaFile = this.javaFile;
htmlFile = this.htmlFile;
}
public void ScanJavaFile(){
try {
PrintWriter writer = new PrintWriter(htmlFile);
Scanner input = new Scanner(javaFile);
writer.println("<html>");
writer.println("<head></head>");
writer.println("<body>");
while (input.hasNextLine()){
String line = input.nextLine();
Scanner lineScanner = new Scanner(line);
writer.println(line + "</br>");
while(lineScanner.hasNext()){
String word = lineScanner.next();
if(isPreserved(word)){
}
else if(isANumber(word)) {
}
else if(isAString(word)) {
}
}
/*
*
* This is the meat of the program
* 1) read a line
* 2) break up that line into "tokens"
* 3) scan each token to see if keyword, string or number
* 4) if keyword use keyword html
* 5) else if number use number html
* 6) d
*/
}
writer.println("</body>");
writer.println("</html>");
}catch (IOException ioe){
ioe.printStackTrace();
}
}
public boolean isPreserved(String a) throws IOException{
PrintWriter writer = new PrintWriter(htmlFile);
String[] preserved = {"abstract", "assert", "boolean", "break",
"byte", "case", "catch", "char", "class", "const*", "continue"
, "default", "default", "double", "do", "else", "enum", "extends"
, "false", "final", "finally", "float", "for", "goto*", "if",
"implements", "import", "instanceof", "int", "interface", "long"
, "native", "new", "null", "package", "private", "protected"
, "public", "return", "short", "static", "strictfp", "super",
"switch", "synchronized", "this", "throw", "throws", "transient"
, "true", "try", "void", "volatile", "while"};
for(int i = 0; i <preserved.length; i++){
if(a.equals(preserved[i])){
writer.println("<strong>" + preserved[i] + "</strong>");
return true;
}
}
return false;
}
public boolean isANumber(String word) throws IOException{
PrintWriter writer = new PrintWriter(htmlFile);
/*
* numbers can have 0 - 9
* A - F
* a - f
* and x or X
*/
for (int i = 0; i < word.length(); i++){
if (Character.isDigit(word.charAt(i))){
//This is a number
writer.println("<FONT COLOR=\"#cc6600\">" + i + "</FONT>");
return true;
}
else if (word.charAt(i) == 'x' || word.charAt(i) == 'X'){
writer.println("<FONT COLOR=\"#cc6600\">" + i + "</FONT>");
return true;
}
}
return false;
}
public boolean isAString(String word) throws IOException{
PrintWriter writer = new PrintWriter(htmlFile);
// use the '"' or "\""
if(word.equals("\"") ){
writer.println("<FONT COLOR=\"#33FF33\">" + word + "</FONT>");
return true;
}
return false;
}
}
and this is the tester
import java.io.*;
import java.util.*;
public class SHTester {
public static void main(String args[]) throws IOException{
File javaFile = new File("java.java");
File htmlFile = new File("html.html");
SyntaxHighlighter SH = new SyntaxHighlighter(javaFile, htmlFile);
SH.ScanJavaFile();
}
}
i made a random java file so you can create the same