Answer to Question #15307 in C++ for dfarajzade

Question #15307
[code]Write a program that inputs a Java source code file and outputs a copy of that file with Javakeywoards surrounded with HTML tags for bold type. For example this input:
public class JavaSource
{
public static void main ( String[] args )
{
if ( args.length == 3 )
new BigObject();
else
System.out.println("Too few arguments.");
}
}

In a browser the code will look like this:

[b]public[/b] [b]class[/b] JavaSource{
[b]public[/b] [b]static[/b] [b]void[/b] main ( String[] args ){
[b]if[/b] ( args.length == 3 )[b]new[/b] BigObject();[b]else[/b]System.out.println("Too few arguments.");
}
}[/code]
1
Expert's answer
2012-09-25T10:53:06-0400
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;


public class Main {

public static void main(String... args) throws IOException{
& Set<String> set = new HashSet<String>();
& set.add("byte");set.add("short");set.add("int");set.add("long");
& set.add("float");set.add("double");set.add("char");set.add("boolean");
& set.add("do");set.add("for");set.add("break");set.add("if");
& set.add("continue");set.add("else");set.add("switch");set.add("case");
& set.add("default");set.add("break");set.add("private");set.add("public");
& set.add("protected");set.add("final");set.add("static");set.add("abstract");
& set.add("synchronized");set.add("volatile");set.add("strictfp");set.add("false");
& set.add("true");set.add("null");set.add("return");set.add("void");
& set.add("package");set.add("import");set.add("try");set.add("catch");
& set.add("finally");set.add("throw");set.add("throws");set.add("new");
& set.add("extends");set.add("implements");set.add("class");set.add("instanceof");
& set.add("this");set.add("super");
&
& String name;
& Scanner conSc = new Scanner(System.in);
& System.out.print("Full path to the input file: ");
& name = conSc.nextLine();
& Scanner fileSc = new Scanner(new File(name));
& (new File((new File(name)).getParent()+ File.separator+ "copy"+(new File(name)).getName())).createNewFile();
& FileWriter write = new FileWriter(new File((new File(name)).getParent()+ File.separator+ (new File(name)).getName().substring(0, (new File(name)).getName().lastIndexOf('.'))+".html"));
& String line = "";
& line = fileSc.nextLine();
&
& while(line != null){
& String output = "";
& String lexems[] = line.split(" ");
& for(int i=0; i<lexems.length;i++){
& if(set.contains(lexems[i])){
& output+=("<b>"+lexems[i]+"</b> ");
& }else{
& output+=(lexems[i]+" ");
& }
& }
& write.write(output+"\r\n");
& try{line = fileSc.nextLine();}catch(Exception e){line=null;};
& }
& write.close();
& conSc.close();
& fileSc.close();
&
}

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS