Problem Statement:
We need to highlight words in a sentence that matches a given list of keywords. Matched words should be surrounded by "[blue]".
Sample Input:
If my code contains many for loops or while loops then my program may run slow.
Keywords List:
if, else, then, until, while, do , for
Sample Output:
[blue]If[blue] my code contains many [blue]for[blue] loops or [blue]while[blue] loops [blue]then[blue] my program may run slow.
Solution:
package com.hellojavatech.syntaxhighlight;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Sagar
* Class to highlight words that matches keyword list
*
*/
public class SyntaxHighlighter {
public static void main(String[] args) {
UIForInputAndDisplay uiForInputAndDisplay = new UIForInputAndDisplay();
String userInput = uiForInputAndDisplay.getInputText();
KeywordsList keywordsList = new KeywordsList();
ArrayList keywords = keywordsList.keywordsToHighlight();
ProcessText processText = new ProcessText();
String highlightedString = processText.processInput(keywords, userInput);
uiForInputAndDisplay.displayHighlightedText(highlightedString);
}
}
package com.hellojavatech.syntaxhighlight;
import java.util.Scanner;
public class UIForInputAndDisplay {
/**
* method to get Input from user
* @return String userInput
*/
public String getInputText() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence: ");
String userInput = sc.nextLine();
return userInput;
}
/**
* method to display highlighted String
* @param highlightedString
*/
public void displayHighlightedText(String highlightedString) {
System.out.println("Text after Syntax Highlighting is: ");
System.out.println(highlightedString);
}
}
Please feel free to comment.
We need to highlight words in a sentence that matches a given list of keywords. Matched words should be surrounded by "[blue]".
Sample Input:
If my code contains many for loops or while loops then my program may run slow.
Keywords List:
if, else, then, until, while, do , for
Sample Output:
[blue]If[blue] my code contains many [blue]for[blue] loops or [blue]while[blue] loops [blue]then[blue] my program may run slow.
Solution:
- SyntaxHighlighter.java
package com.hellojavatech.syntaxhighlight;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Sagar
* Class to highlight words that matches keyword list
*
*/
public class SyntaxHighlighter {
public static void main(String[] args) {
UIForInputAndDisplay uiForInputAndDisplay = new UIForInputAndDisplay();
String userInput = uiForInputAndDisplay.getInputText();
KeywordsList keywordsList = new KeywordsList();
ArrayList keywords = keywordsList.keywordsToHighlight();
ProcessText processText = new ProcessText();
String highlightedString = processText.processInput(keywords, userInput);
uiForInputAndDisplay.displayHighlightedText(highlightedString);
}
}
- UIForInputAndDisplay.java
package com.hellojavatech.syntaxhighlight;
import java.util.Scanner;
public class UIForInputAndDisplay {
/**
* method to get Input from user
* @return String userInput
*/
public String getInputText() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Sentence: ");
String userInput = sc.nextLine();
return userInput;
}
/**
* method to display highlighted String
* @param highlightedString
*/
public void displayHighlightedText(String highlightedString) {
System.out.println("Text after Syntax Highlighting is: ");
System.out.println(highlightedString);
}
}
- keywordsToHighlight.java
package com.hellojavatech.syntaxhighlight;
import java.util.ArrayList;
public class KeywordsList {
/**
* method to create list of keywords
* @return ArrayList list of keywords
*/
public ArrayList keywordsToHighlight() {
ArrayList keywords = new ArrayList();
keywords.add("if");
keywords.add("else");
keywords.add("then");
keywords.add("until");
keywords.add("while");
keywords.add("do");
keywords.add("for");
return keywords;
}
}
- processInput.java
package com.hellojavatech.syntaxhighlight;
import java.util.ArrayList;
import java.util.Scanner;
public class ProcessText {
/**
* method to process the user input to highlight keywords
* @param keywords
* @param userInput
* @return String processed text
*/
public String processInput(ArrayList keywords, String userInput) {
StringBuilder tempString = new StringBuilder();
String[] splitInput = userInput.split(" ");
for (String word : splitInput) {
if(keywords.contains(word)){
tempString.append("[blue]"+word+"[blue]");
}
else{
tempString.append(word);
}
tempString.append(" ");
}
return tempString.toString().trim();
}
}
Please feel free to comment.
No comments:
Post a Comment