Syntax Error in Java print statement [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
System.out.println("if(line.contains(\"<string key=\"concept:name\" value=\"LCSP\"/>\"\))");
I am getting an error. I want to print above statement as a string.
Can someone help me.

Remove the last backslash \ there is no need to escape )
System.out.println("if(line.contains(\"<string key=\"concept:name\" value=\"LCSP\"/>\"))");
DEMO1
Solution to the problem given in comment
String v11 = "John";
System.out.println("if(line.contains(\"<string key=\\\"concept:name\\\" value=\\\""+v11+"\\\"/>\"))");
OUTPUT
if(line.contains("<string key=\"concept:name\" value=\"John\"/>"))
DEMO2

Original
System.out.println("if(line.contains(\"<string key=\"concept:name\" value=\"LCSP\"/>\"**\**))");
**** is not required I have removed from Original
Changed:
System.out.println("if(line.contains(\"<string key=\"concept:name\" value=\"LCSP\"/>\"))");

String v11 = "John1";
System.out.println("if(line.contains(\"<string key=\\\"concept:name\\\" value=\\\""+v11+"\\\"/>\"))");
remove the last backlash and update the "LCSP" to v11 variable.
output:
if(line.contains("<string key=\"concept:name\" value=\"John1\"/>"))

no need to escape ')' at end of the line.
the changed statement is:
System.out.println("if(line.contains(\"<string key=\"concept:name\" value=\"LCSP\"/>\"))");

Related

Parse a CSV using Regular Expression [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 days ago.
Improve this question
I have a CSV that I am trying to parse in a java program. Each value has double quotes around it. They are separated by comma(,). Value within quotes could have commas inside them- that needs to be ignored/escaped.
I need to split this string and parse out:
"Hello","Test","Hi, how are you?","State","Sorry,ABC!Team"
Result I want after split is array with following values. Use the right comma as separator.
Result:
Hello
Test
Hi, how are you?
State
Sorry,ABC!Team

Defining String with the char '{' in JAVA [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to define a Constant with some pattern identifier to be able to replace it in large String constructs later, for example in a complex Querie.
private static String SUBSTITUTE_1 = "${substitute1}";
But when I do that, compiler complains about expecting a number next to the '{' char.
My question is: Is there a way to escape the special characters in the string?
private static String SUBSTITUTE_1 = "\$\{substitute1\}";
This does not work.
This is strange because if I define a String builder like so
private static String SUBSTITUTE_1 = new StringBuilder("${substitute1}").toString();
there no problem with the special characters.
Unless there is something really obvious that i'm missing this does not make sense.
Thanks in advance.
Apparently, Intellij's compiler detects that somewhere in the code I'm using the defined constant in a String.replaceAll() instruction and complains about its definition as "${x}" is not a valid Regex Expression.
Solution is change to String.replace().

Filtering a string went wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Consider we have the following set of String:
String text="Geeraertsb,∗,1";
String text2="bla bla";
String text3="in terms of &#1";
I would like to get those strings that do not contain either of & and/or "*". so that means only `text2" is of interest.
however if I use:
if(!text.contains("*"))
{
//do something
}
if(!text3.contains("&"))
{
//do something
}
it does enter the if statement while it should not. Any idea why?
You're using different characters
∗ is not equal to *
You may want to check that you're using the same set of characters for you String objects and your conditions.
You can try like this:
Pattern mypat = Pattern.compile("[^a-z ]", Pattern.CASE_INSENSITIVE);
Matcher mat = mypat.matcher("Geeraertsb,∗,1");
boolean found = mat.find();
if (found)
{
//code
}
The above code uses the regex approach in which you try to check if your string contains anything apart from the alphabets.

Java Regex not finding all results [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Using Java 8, I have the following snippets of code:
Here:
private ArrayList<MatchResult> allMatches(final Pattern p, final CharSequence input){
ArrayList<MatchResult> results = new ArrayList<MatchResult>();
Matcher matcher = p.matcher(input);
while(matcher.find()){
results.add(matcher.toMatchResult());
}
return results;
}
And the main method here:
List<MatchResult> matches = allMatches(Pattern.compile(searchString), searchable.getText());
System.out.println(matches.size());
The searchString variable holds the following String:
(?im)Exception
The searchable's text is this:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create PoolableConnectionFactory (ORA-28040: No matching authentication protocol
As you can see, there are 3 times in the searchable's text where we see the word "exception". However, Java is only finding 2 of them.
The output of the code is this:
2
One more piece of information about this, is that the searchString variable comes from the user.
My question is regarding the number of matches. Java seems to be missing some matches when using regex. The text I provided comes from a bigger file, and this isn't the first one it's missed, but it is the one I'm focusing on to hopefully have some hint of where to look. What is going on here? Why is Java missing some matches? Is there a more accurate regular expression search that I should use?
To hopefully help those that aren't familiar with the "(?im)" in the searchString variable, I've located a resource here.
Found the problem, it was not in the code I posted, as that is proprietary code and cannot be put out in public domain. But basically the searchString was not being sent to the Pattern compiler correctly and the "(?im)" was stripped off before arrival. Thank you all for your help.

Regex to match a string with a slash [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want to match a string containing slashes in Java. I have the following code:
String exclude = "some/class/in/package/*.class";
String className = "some/class/in/package/TheClass.class";
boolean value = className.matches(exclude);
System.out.println(value);
>false
Can anyone help me to fix this?
Valid class names must start with a letter so it could reasonably be
"some/class/in/package/[A-Za-z].*\\.class"
But then you probably ought to put in valid characters for the rest
"some/class/in/package/[A-Za-z][A-Za-z0-9_]*\\.class"
Nevermind, I had forgotten the '.' before '*'
String exclude = "some/class/in/package/.*.class";

Categories