How to allow Special Character '&' in java - java

I have the following code to allow the special character :
public static String replacer1(StringBuffer outBuffer) {
String data = "";
StringBuffer tempBuffer = null;
try {
if (outBuffer != null) {
data = outBuffer.toString();
tempBuffer = new StringBuffer();
int incrementor = 0;
int dataLength = data.length();
while (incrementor < dataLength) {
char charecterAt = data.charAt(incrementor);
if (charecterAt == '%') {
tempBuffer.append("<percentage>");
} else if (charecterAt == '+') {
tempBuffer.append("<plus>");
}else if (charecterAt == '-') {
tempBuffer.append("<hyphen>");
} else if (charecterAt == '&') {
tempBuffer.append("<AMPERSAND>");
} else if (charecterAt == '_') {
tempBuffer.append("<UNDERSCORE>");
}
else {
tempBuffer.append(charecterAt);
}
incrementor++;
}
data = tempBuffer.toString();
data = URLDecoder.decode(data, "utf-8");
data = data.replaceAll("<percentage>", "%");
data = data.replaceAll("<plus>", "+");
data = data.replaceAll("<hyphen>", "-");
data = data.replaceAll("<AMPERSAND>", "&");
data = data.replaceAll("<UNDERSCORE>", "_");
//data = data.replaceAll("\"", "");
data = data.replaceAll("'", "");
data = data.trim();
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
all the special charcters are comming except '&'.
IS there any other way?
Please help

Related

Remove Blank Lines While Printing An Output

I have this Java lexical analyzer algorithm that prints out each assigned token to every symbol. The output should print out on each and every individual line without any space in between. But mine does have spaces. I have tried to use trim() method, even replaceAll() method, nothing seems to work. Here is my Java code:
import org.w3c.dom.ls.LSOutput;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Lexer {
public static void Tokenize(String fileName) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = reader.readLine()) != null) {
line = removeComments(line);
String[] tokens = tokenizeLine(line);
for (String token : tokens) {
System.out.println(token);
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
System.err.println("Error closing file: " + e.getMessage());
}
}
System.out.println("SYNTAX ERROR: INVALID IDENTIFIER NAME");
}
private static String removeComments(String line) {
// Remove inline comments
line = line.replaceAll("//.*", "");
// Remove block comments
Pattern pattern = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);
return matcher.replaceAll("");
}
private static String[] tokenizeLine(String line) {
String[] tokens = line.split("\\s+ |(?=[\\[\\](){}<>=,;+-/*%|&!])|(?<=[\\[\\](){}<>=,;+-/*%|&!])");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i].replaceAll("\\s+", "");
if (token.matches("procedure")) {
tokens[i] = "PROC";
} else if (token.matches("int")) {
tokens[i] = "INT";
} else if (token.matches("[0-9]+")) {
// Integer constant
tokens[i] = "INT_CONST";
} else if (token.matches("[(]")) {
tokens[i] = "LP";
} else if (token.matches("[)]")) {
tokens[i] = "RP";
} else if (token.matches("\".*\"")) {
// String constant
tokens[i] = "STR_CONST";
} else if (token.matches("String") || token.matches("string")) {
// String keyword
tokens[i] = "STR";
} else if (token.matches("if")) {
tokens[i] = "IF";
} else if (token.matches("for")) {
tokens[i] = "FOR";
} else if (token.matches("while")) {
tokens[i] = "WHILE";
} else if (token.matches("return")) {
tokens[i] = "RETURN";
} else if (token.matches("[;]")) {
tokens[i] = "SEMI";
} else if (token.matches("do")) {
tokens[i] = "DO";
} else if (token.matches("break")) {
tokens[i] = "BREAK";
} else if (token.matches("end")) {
tokens[i] = "END";
} else if (token.matches("[a-zA-Z][a-zA-Z0-9]*")) {
// Identifier
tokens[i] = "IDENT";
} else if (token.matches("[=]")) {
tokens[i] = "ASSIGN";
} else if (token.matches("[<]")) {
tokens[i] = "LT";
} else if (token.matches("[>]")) {
tokens[i] = "RT";
} else if (token.matches("[++]")) {
tokens[i] = "INC";
} else if (token.matches("[{]")) {
tokens[i] = "RB";
} else if (token.matches("[}]")) {
tokens[i] = "LB";
} else if (token.matches("[*]")) {
tokens[i] = "MUL_OP";
} else if (token.matches("[/]")) {
tokens[i] = "DIV_OP";
} else if (token.matches("[>=]")) {
tokens[i] = "GE";
}
}
return tokens;
}
}
and here is my output.
LP
IDENT
RP
FOR
LP
IDENT
ASSIGN
INT_CONST
SEMI
IDENT
LT
IDENT
SEMI
IDENT
ASSIGN
IDENT
INC
INC
RP
RB
IDENT
ASSIGN
IDENT
MUL_OP
LP
IDENT
DIV_OP
INT_CONST
RP
SEMI
IF
LP
IDENT
RT
ASSIGN
INT_CONST
RP
BREAK
SEMI
LB
IDENT
SEMI
IDENT
IDENT
ASSIGN
STR_CONST
SEMI
SYNTAX ERROR: INVALID IDENTIFIER NAME``
Either remove the empty strings in your tokenizeLine from the returned array before returning, for example by changing the return statement from:
return tokens;
to
return Arrays.stream(tokens).filter(Predicate.not(String::isBlank)).toArray(String[]::new);
or just print the non empty strings in your loop by checking before printing
for (String token : tokens) {
if(!token.isBlank()) {
System.out.println(token);
}
}

Identify whether my string contains any hex code or not in java

I want to identify whether my string contains any hex code or not.
Use cases
String input1 = "hello check this input ";
String input2 = "hello check 0x740x680x690x73 input";
String input3 = "0x680x650x6c0x6c0x6f0x200x630x680x650x630x6b0x200x740x680x690x730x200x690x6e0x700x750x74";
isContainHex(input1) should return false
isContainHex(input2) should return true
isContainHex(input3) should return true
I have tried
String input2 = "hello check 0x740x680x690x73 input";
if(input2.contains("0x") || input2.contains("\\x"))
{
System.out.println("string contains hex");
}
and I am able to find hex but,
If My input contains hex like
String input4 = "h68h65h6ch6ch6f check this input ";
Here I cant check input4.contains("h")
Any one have solution for this?
is there any standard library by which I can achive same?
Update
I have wrote following code, and its working well but taking time.
Now can it be optimize
try
{
if (input != null && input.trim().length() > 0)
{
String originalHex = null;
StringBuilder output = new StringBuilder();
String inputArray[] = null;
if (StringUtils.countMatches(input, "\\x") > 3)
{
originalHex = input.substring(input.indexOf("\\x"), input.lastIndexOf("\\x", input.length()) + 4);
inputArray = input.split("\\Q\\x\\E");
}
else if (StringUtils.countMatches(input, "0x") > 3)
{
originalHex = input.substring(input.indexOf("0x"), input.lastIndexOf("0x", input.length()) + 4);
inputArray = input.split("0x");
}
if (inputArray != null && inputArray.length > 0)
{
for (String str: inputArray)
{
int strLength = str.trim().length();
if (strLength == 2)
{
output.append((char)Integer.parseInt(str, 16));
}
else if (strLength > 2)
{
if (strLength % 2 != 0)
{
strLength = strLength - 1;
}
for (int i = 0; i < strLength; i += 2)
{
String val = str.substring(i, i+2);
if (val.matches("\\d+"))
{
output.append((char)Integer.parseInt(val, 16));
}
}
}
}
input = input.replaceAll("\\Q" + originalHex + "\\E", output.toString());
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
syso(input);

How to read multiple sheets with Event model using Apache POI?

I can successfully read Excel file (.xls) using Event model POI. I am not using the usermodel (org.apache.poi.ss.usermodel) but an Event API to process xls and xlsx files (to address the memory footprint issue). However, when reading an .xls file with multiple sheets, only first sheet is read. Other sheets are ignored.
I am implementing HSSFListener and overriding its processRecord(Record record) method for xls files.
Here is my part of the code:
/**
* Main HSSFListener method for xls. It processes events and creates a RuntimeRecord to put into the DataPool.
*/
public void processRecord(Record record) {
int thisRow = -1;
String thisStr = null;
switch (record.getSid()) {
case BoundSheetRecord.sid:
boundSheetRecords.add(record);
break;
case BOFRecord.sid:
BOFRecord br = (BOFRecord)record;
if(br.getType() == BOFRecord.TYPE_WORKSHEET) {
// Works by ordering the BSRs by the location of their BOFRecords, and then knowing that we
// process BOFRecords in byte offset order
if(orderedBSRs == null) {
orderedBSRs = BoundSheetRecord.orderByBofPosition(boundSheetRecords);
}
// Check the existence of sheets
if(sheetIndex == 0) {
for(int i=0;i<excelSheetList.length;i++) {
boolean found = false;
if(this.getExcelSheetSpecification().equals(MSExcelAdapter.USE_WORKSHEET_NAME)) {
for(BoundSheetRecord rec : orderedBSRs) {
int len = rec.getSheetname().length();
if(len > 31)
this.warning("processRecord()","The length of sheet: " + rec.getSheetname() + " has more than 31 characters. Please change the name of the sheet, save the file and try again.");
}
}
if(this.getExcelSheetSpecification().equals(MSExcelAdapter.USE_WORKSHEET_NUMBER)) {
int sheetNo = Integer.parseInt(excelSheetList[i]);
if(sheetNo > 0 && sheetNo <= orderedBSRs.length) {
found = true;
}
} else {
for(int j=0;j<orderedBSRs.length;j++) {
if(this.getExcelSheetSpecification().equals(MSExcelAdapter.USE_WORKSHEET_NAME)) {
String sheetName = ((BoundSheetRecord) boundSheetRecords.get(j)).getSheetname();
if(excelSheetList[i].equals(sheetName)) {
found = true;
break;
}
}
}
}
if(!found)
this.error("processRecord()","Sheet: " + excelSheetList[i] + " does not exist.");
}
}
readCurrentSheet = true;
sheetIndex++;
System.out.println(sheetIndex);
if(this.getExcelSheetSpecification().equals(MSExcelAdapter.USE_WORKSHEET_NAME)) {
String sheetName = ((BoundSheetRecord) boundSheetRecords.get(sheetIndex-1)).getSheetname();
if(!canRead(sheetName)) {
readCurrentSheet = false;
}
} else {
if(!canRead(sheetIndex + "")) {
readCurrentSheet = false;
}
}
}
break;
case SSTRecord.sid:
sstRecord = (SSTRecord) record;
break;
case BlankRecord.sid:
BlankRecord brec = (BlankRecord) record;
thisRow = brec.getRow();
thisStr = null;
values.add(thisStr);
columnCount++;
break;
case FormulaRecord.sid:
FormulaRecord frec = (FormulaRecord) record;
thisRow = frec.getRow();
if(Double.isNaN( frec.getValue() )) {
// Formula result is a string
// This is stored in the next record
outputNextStringRecord = true;
nextRow = frec.getRow();
} else {
thisStr = formatListener.formatNumberDateCell(frec);
}
break;
case StringRecord.sid:
if(outputNextStringRecord) {
// String for formula
StringRecord srec = (StringRecord)record;
thisStr = srec.getString();
thisRow = nextRow;
outputNextStringRecord = false;
}
break;
case LabelSSTRecord.sid:
if(readCurrentSheet) {
LabelSSTRecord lsrec = (LabelSSTRecord) record;
thisRow = lsrec.getRow() + 1;
if(rowNumberList.contains(thisRow + "") ||
(rowNumberList.contains(END_OF_ROWS) && thisRow >= secondLastRow)) {
if(sstRecord == null) {
thisStr = "(No SST Record, can't identify string)";
} else {
thisStr = sstRecord.getString(lsrec.getSSTIndex()).toString();
}
}
}
break;
case NumberRecord.sid:
if(readCurrentSheet) {
NumberRecord numrec = (NumberRecord) record;
thisRow = numrec.getRow() + 1;
if(rowNumberList.contains(thisRow + "") ||
(rowNumberList.contains(END_OF_ROWS) && thisRow >= secondLastRow)) {
// No need to format.
// thisStr = formatListener.formatNumberDateCell(numrec); // Format
thisStr = String.valueOf(numrec.getValue());
}
}
break;
default:
break;
}
// Handle missing column
if(record instanceof MissingCellDummyRecord) {
thisStr = "";
}
// If we got something to print out, do so
if(thisStr != null) {
values.add(thisStr);
columnCount++;
}
// Handle end of row
if(record instanceof LastCellOfRowDummyRecord) {
if(readCurrentSheet) {
int currentRow = ((LastCellOfRowDummyRecord) record).getRow() + 1;
if(rowsReadSet.add(String.valueOf(currentRow))) {
if(processTestData) {
try {
int dpSize = this.getOutputDP().getSize();
if(dpSize < 1000 &&
(rowNumberList.contains(currentRow + "") ||
(rowNumberList.contains(END_OF_ROWS) && currentRow >= secondLastRow))) {
for(int i=columnCount; i<this.getConfigRecord().size(); i++) {
values.add(null); // Add empty string for missing columns
}
RuntimeRecord resultRecord = this.createRuntimeRecord(values);
this.fireRecordCreatedEvent(resultRecord);
this.putNextRecord(resultRecord);
}
} catch (Exception e) {
this.error("processRecord()",e.getMessage());
this.fireRecordCreationFailedEvent(e.getMessage(), e.getMessage());
}
} else {
try {
if(rowNumberList.contains(currentRow + "") || (rowNumberList.contains(END_OF_ROWS) && currentRow >= secondLastRow)) {
for(int i=columnCount; i<this.getConfigRecord().size(); i++) {
values.add(null); // Add empty string for missing columns
}
RuntimeRecord resultRecord = this.createRuntimeRecord(values);
this.fireRecordCreatedEvent(resultRecord);
this.putNextRecord(resultRecord);
}
} catch (Exception e) {
this.error("processRecord()",e.getMessage());
this.fireRecordCreationFailedEvent(e.getMessage(), e.getMessage());
}
}
}
values.removeAllElements();
columnCount = 0;
}
}
}
Here is the method that registers the listener:
private void readxls() throws FileNotFoundException, IOException {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(this.getFileName()));
MissingRecordAwareHSSFListener listener = new MissingRecordAwareHSSFListener(this);
formatListener = new FormatTrackingHSSFListener(listener);
HSSFEventFactory factory = new HSSFEventFactory();
HSSFRequest request = new HSSFRequest();
request.addListenerForAllRecords(formatListener);
rowsReadSet.clear();
factory.processWorkbookEvents(request, fs);
}
When I debugged by adding some breakpoints, I noticed that it actually adds the column values to my values Vector. However, following condition is never matched for second sheet.
if(record instanceof LastCellOfRowDummyRecord) {
...
}
How to get rid off this problem? I want to read all the sheets in Excel file (.xls). I am using Apache POI 3.11-20141221 with JDK7.

Null pointer exception when comparing two Strings

I keep getting a Null Pointer Esception when comparing two Strings. I know both of the Strings aren't null so I am not sure what is going on.
public void search() {
while (!openList.isEmpty()) {
currState = openList.removeFirst();
if (currState.equals(goal)) { //this line produces NullPointerException
solution = true;
printSolution(currState);
break;
Goal is a String that I read in from a file.
Openlist is a linked list.
the string start is: 120345678
and goal is: 012345678
public class BFS {
public String start;
public String goal;
public String startFinal;
LinkedList<String> openList;
Map<String, Integer> levelDepth;
Map<String, String> stateHistory;
int nodes = 0;
int limit = 100;
int unique = -1;
int newValue;
int a;
public String currState;
boolean solution = false;
public BFS() {
openList = new LinkedList<String>();
levelDepth = new HashMap<String, Integer>();
stateHistory = new HashMap<String, String>();
this.start = start;
this.goal = goal;
addToOpenList(start, null);// add root
}
public void loadStartState(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
StringBuilder currentLine = new StringBuilder();
while (line != null) {
currentLine.delete(0, currentLine.capacity());
currentLine.append(line);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
sb.append("\n");
line = reader.readLine();
}
start = sb.toString();
System.out.println(start);
} finally {
reader.close();
}
}
public void loadGoalState(String filename)throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(filename));
try {
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
StringBuilder currentLine = new StringBuilder();
while (line != null) {
currentLine.delete(0, currentLine.capacity());
currentLine.append(line);
currentLine.deleteCharAt(1);
currentLine.deleteCharAt(2);
sb.append(currentLine.toString());
sb.append("\n");
line = reader.readLine();
}
goal = sb.toString();
System.out.println(goal);
} finally {
reader.close();
}
}
public void search() {
while (!openList.isEmpty()) {
currState = openList.removeFirst();
if (currState != null && currState.equals(goal)) {
solution = true;
printSolution(currState);
break;
} else {
a = currState.indexOf("0");
// left
while (a != 0 && a != 3 && a != 6) {
String nextState = currState.substring(0, a - 1) + "0"
+ currState.charAt(a - 1)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// up
while (a != 0 && a != 1 && a != 2) {
String nextState = currState.substring(0, a - 3) + "0"
+ currState.substring(a - 2, a)
+ currState.charAt(a - 3)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// right
while (a != 2 && a != 5 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.charAt(a + 1) + "0"
+ currState.substring(a + 2)
+ currState.substring(a + 1);
addToOpenList(nextState, currState);
nodes++;
break;
}
// down
while (a != 6 && a != 7 && a != 8) {
String nextState = currState.substring(0, a)
+ currState.substring(a + 3, a + 4)
+ currState.substring(a + 1, a + 3) + "0"
+ currState.substring(a + 4);
addToOpenList(nextState, currState);
nodes++;
break;
}
}
}
}
private void addToOpenList(String newState, String oldState) {
if (!levelDepth.containsKey(newState)) {
newValue = oldState == null ? 0 : levelDepth.get(oldState) + 1;
unique++;
levelDepth.put(newState, newValue);
openList.add(newState);
stateHistory.put(newState, oldState);
}
}
Solution
Try this, remove invocation of addToOpenList(start, null) before loading value of goal and start.
Old stuff
Here is null
addToOpenList(start, null);
String currState = openList.removeFirst();
currState == null
Additional information
public BFS() {
this.start = start; //  Variable 'start' is assigned to itself
this.goal = goal; //  Variable 'goal' is assigned to itself
addToOpenList(start, null); // start is null
}
Even my IntelliJ see this :)
Method invocation currState.indexOf("0") at line 115 may produce
java.lang.NullPointerException
115: a = currState.indexOf("0");
Perhaps you could try something like:
public void search(){
currState = openList.removeFirst();
while(currState != null){
if(currState.equals(goal)){
solution = true;
printSolution(currState);
break;
}
currState = openList.removeFirst();
}
}

Conversion from prefix to infix: Null Pointer exception

I am trying to convert an expression from infix to postfix. I've tried debugging the code but I keep getting null pointer exception when the variable is popped. The input for which I get the error is :
(=> (NOT (award)) (badgrade))
The output after I get the error is (NOT award)
Please tell me if need to edit the Q to post lesser code/ add more comments/ provide any more information. Thanks!
public class toInfix1 {
Stack<String> symbol = new Stack<String>();
Stack<String> variable = new Stack<String>();
Stack<String> operator = new Stack<String>();
static String inputfile = "kb2.txt";
ArrayList<String> infix = new ArrayList<String>();
public void toPrefix1() {
try {
File f = new File(inputfile);
FileReader fr = new FileReader(f);
BufferedReader bf = new BufferedReader(fr);
String str;
String kb = "";
while ((str = bf.readLine()) != null) {
Pattern pattern = Pattern.compile("[(]|[)]|<=>|=>|\\w+|^\\s+");
Matcher m = pattern.matcher(str);
//int a = 0;
System.out.println("KB" + kb);
while (m.find()) {
String node1 = m.group();
System.out.println("Node1" + node1);
//If (
if (node1.equals("(")) {
symbol.push(node1);
} else if (node1.equals("OR") || node1.equals("AND")
|| node1.equals("NOT") || node1.equals("=>")
|| node1.equals("<=>")) {
operator.push(node1);
//If )
} else if (node1.equals(")")) {
//Pop symbol (
if(!variable.empty()&& !operator.empty() && !symbol.empty()){
String symbol1 = "", op = "";
if (symbol.peek() != null && !symbol.empty()) {
symbol1 = symbol.pop();
}
//Pop if operator AND OR => <=> (Binary)
if (operator.peek() != null && !operator.empty()) {
op = operator.pop();
if (op.equals("AND") || op.equals("OR")
|| op.equals("=>") || op.equals("<=>")) {
String var2 = "";
String var1 = "";
if (variable.peek() != null && !variable.empty()) {
var1 = variable.pop();
}//Error occurs in the following if condition
if (variable.peek() != null && !variable.empty()) {
var2 = variable.pop();
}
kb = "(" + var1 + op + var2 + ")";
variable.push(kb);
//Pop if operator NOT (Unary)
} else if (op.equals("NOT")) {
String var1 = "";
if (variable.peek() != null && !variable.empty()) {
var1 = variable.pop();
}
kb = "(" + op + var1 + ")";
variable.push(kb);
//No operator after popping )
}
}
}
//If there are no operators
} else {
variable.push(node1);
}
}
}
fr.close();
} catch (Exception e) {
System.err.println("Error thrown" + e.getMessage());
}
}
public static void main(String[] args) {
System.out.println("In new file");
toInfix1 inf1 = new toInfix1();
inf1.toPrefix1();
System.out.println("Completed");
}
}
I was trying to access an element which was null at variable.peek(). removing all peek conditions, will make the program work.

Categories

Resources