The below code is meant to trim my string. Unfortunately it's removing more than the 'conf' value.
Did I do this correctly?
What this is doing is adding a double html response address, so i'm removing the first url which is 'conf'
String pageIdUrl = response.getRedirectUrl();
if(pageIdUrl.startsWith(conf.toString()));
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
It would appear that there is a misplaced semicolon that is stopping your if statement from working as you expect.
String pageIdUrl = response.getRedirectUrl();
if(pageIdUrl.startsWith(conf.toString()))
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
Console.WriteLine(conf.toString());
Console.WriteLine(pageIdUrl);
if(pageIdUrl.startsWith(conf.toString()));
^
Note the semicolon here. The then-action is empty: if the condition is met, nothing is done.
{
pageIdUrl = pageIdUrl.substring(conf.toString().length());
}
This part happens whether or not the condition is met, because it's just another follow-on statement. Yes, you're allowed to use braces like that. They create an extra scope for local variable names.
What's happening is that your string doesn't actually start with the conf string, and is getting chomped anyway.
You kept one semicolon after if statement.
String pageIdUrl = "conf:xxx";
String conf = "conf";
if (pageIdUrl.startsWith(conf.toString())){
pageIdUrl = pageIdUrl.substring(conf.toString().length());
System.out.println(pageIdUrl);
}
As others have pointed out, the problem is with the extra ";". Tip: If you're and Eclipse user, I'd recommend setting the Compile/Error Warnings for "Empty Statement" to Warning or Error. Eclipse will then flag this problem in your editor. Other IDE's may well offer something similar.
Related
I have hit a road block in a program I am writing in Java. The program basically copies folders and files from multiple locations into one folder to make backing up code from multiple locations on my computer easier. The problem I have is that I don't want to copy specific folders, in this case "workspace/.metadata". The start of the code I am having issues with is below:
public void copyFolder(File in, File out, String loc)
throws IOException{
String check = in.getName().substring(1);
System.out.println("loc:"+loc+"check: "+check);
if(loc.equals("java")){
if(check.equals("metadata")){
System.out.println("failboat");
//statusBox.append("failboat");
}
}
And this is the result I see:
loc:java
check: orkspace2
loc:java
check: metadata
loc:java
check: lock
I've had other's look at the code and they agree it should work. I've even created a replica of the code in a test file:
String test = "fmetadata";
String loc = "java";
String check = test.substring(1);
if(loc.equals("java")){
if(check.equals("metadata")){
System.out.print("failboat");
}else{
System.out.println("WTF");
System.out.print(test+ ": :"+check);
}
}
And the result?
failboat
There is a dent in my desk the size of my forehead from trying to figure this out.
If that output you posted is the actual output:
loc:java
check: orkspace2
loc:java
check: metadata
loc:java
check: lock
It does not match the code you've pasted, as you do not print a newline between the two items:
System.out.println("loc:"+loc+"check: "+check);
If this is truly what you are seeing with that code then I would say the problem is that loc has a stray newline at the end, and is actually "java\n" rather than "java" as you expect. So you should go back and examine how you generate the value you pass through loc to begin with before calling that function.
If this is the case, some suggestions for diagnostics improvements that could help you notice these kinds of issues sooner (in addition to stepping through with a debugger):
// quote your strings to spot whitespace, print length to spot unexpected
// unprintable characters.
System.out.println("loc: \""+loc+"\" "+loc.length());
System.out.println("check: \""+check+"\" "+check.length());
And:
if(loc.equals("java")){
// make sure you're getting to this point, don't assume that the first
// if condition was satisfied:
System.out.println("grumpycat"); // <----
if(check.equals("metadata")){
System.out.println("failboat");
}
}
Hi I am trying to load configuration from a String in Java as follows:
#Test
public void testIllegalCharacter(){
String input = "prop=\\asd";
Config conf = ConfigFactory.parseString(input);
}
The code above produces the following error:
com.typesafe.config.ConfigException$Parse: String: 1: Expecting a value but got wrong token: '\' (Reserved character '\' is not allowed outside quotes) (if you intended '\' (Reserved character '\' is not allowed outside quotes) to be part of a key or string value, try enclosing the key or value in double quotes, or you may be able to rename the file .properties rather than .conf)
I understand I have an illegal character in my String. Although how do I find the full set of illegal characters?
If I (for example) convert this String into a Properties object and then parse it with ConfigFactory.parseProperties I can see the value "\\asd" in resolved as "asd". So there must be some some sanitising going on in the typesafe library, I wish I could call that sanitisation myself, but I cannot see how. Parsing to Properties is not a viable solution as the configuration could be composed by Objects or Lists too.
Has anyone have any suggestion how to solve this issue?
Alternatively can someone point out all the reserved characters set?
Many thanks
If I understand the error message correctly, you should put quotes around you special characters, e.g. like this:
"prop=\"\\asd\"";
Not sure why you's want a property definition with a backslash a ('\a') in it, but I guess I don't need to know :-)
I think I might have found the answer. I need to set the ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES)
Which works for the test below:
#Test
public void test(){
String input = "prop=C:/MyDocuments/mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:/MyDocuments/mydir", conf.getAnyRef("prop"));
}
But will not work for the test with backslashes
#Test
public void test(){
String input = "prop=C:\\MyDocuments\\mydir";
Config conf = ConfigFactory.parseString(input, ConfigParseOptions.defaults().setSyntax(ConfigSyntax.PROPERTIES));
assertEquals("C:\\MyDocuments\\mydir", conf.getAnyRef("prop"));
}
Which fails:
org.junit.ComparisonFailure:
Expected :C:\MyDocuments\mydir
Actual :C:MyDocumentsmydir
So I am not sure this is the definitive answer...
I am trying to extract the pass number from strings of any of the following formats:
PassID_132
PassID_64
Pass_298
Pass_16
For this, I constructed the following regex:
Pass[I]?[D]?_([\d]{2,3})
-and tested it in Eclipse's search dialog. It worked fine.
However, when I use it in code, it doesn't match anything. Here's my code snippet:
String idString = filename.replaceAll("Pass[I]?[D]?_([\\d]{2,3})", "$1");
int result = Integer.parseInt(idString);
I also tried
java.util.regex.Pattern.compile("Pass[I]?[D]?_([\\d]{2,3})")
in the Expressions window while debugging, but that says "", whereas
java.util.regex.Pattern.compile("Pass[I]?[D]?_([0-9]{2,3})")
compiled, but didn't match anything. What could be the problem?
Instead of Pass[I]?[D]?_([\d]{2,3}) try this:
Pass(?:I)?(?:D)?_([\d]{2,3})
There's nothing invalid with your tegex, but it sucks. You don't need character classes around single character terms. Try this:
"Pass(?:ID)?_(\\d{2,3})"
I am writing an Android app and need some help.
I have a string that contains a URL. Sometimes I get extra text before the url and need to trim that off.
I get this "Some cool sitehttp://somecoolsite.com"
And want this "http://somecoolsite.com"
First, I need to detect if the string does not start with http:// and then if not, I need to trim everything in front of http://
Is there an easy way to do this?
I can do the first part.
if (url.startsWith("http://") == false) {
url.replace("", replacement)
}
Any help?
To check if the string starts with http:// you do
if (inputUrl.startsWith("http://")) {
...
}
To trim off the prefix up until the first occurrence of http:// you do
int index = inputUrl.indexOf("http://");
if (index != -1)
inputUrl = inputUrl.substring(index);
The API documentation for the String class should provide you with all the information you need here.
Use this:
if(inputURL.contains("http://")
inputURL = inputURL.substring(inputURL.indexOf("http://"));
Another option would be:
inputUrl = inputUrl.replaceAll(".*http://","http://");
it should work under all conditions (but I assume the regular expression is a bit less efficient).
Please note that provided answers assume that the string will be in lower case (no "HTTP" or "Http") and that no strings contain https://
Please help me for conversion my line of codes are mention below:
String deptName = "IT";
String dept_test = request.getParameter("deptName").trim();
System.out.println("Dept name vlue is"+dept_test);
// problem here for casting...
int dept_id = Integer.parseInt(dept_test);
I don't see any casting problems. If your text doesn't contain a parseable number you will get a NumberFormatException which you may need to catch with a try/catch block. What exactly is the problem you are having?
From looking at your code the most you get will be an error parsing the input (including if it is none. What may be an issue is that the variable deptName is never used. Did you mean to make the second line
String dept_test = request.getParameter(deptName).trim();
(notice no quotes) instead?