Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got this string:
"type":"image","originX":"center","originY":"center","left":135,"top":259,"width":270,"height":519,"fill":"rgb(0,0,0)","overlayFill":null,"stroke":null,"strokeWidth":1,"strokeDashArray":null,"strokeLineCap":"butt","strokeLineJoin":"miter","strokeMiterLimit":10,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1,"shadow":null,"visible":true,"clipTo":null,"src":"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg","filters":[]},
each part is seperated by a comma, i want to be able to extract a few of the numbers into doubles. The ones i want are left, top, scaleX, scaleY and angle. How shall i approch this?
thanks
If you don't want to rely on using JSON parsers (you should, though, if you are using JSON a lot), you could use the split-method on the entire string and split according to , (comma), find the chunks of data that you want, split those according to : and read the data directly from the 2nd slot in the resulting array.
You may need to substring the last " to be able to parse the numbers directly, though.
But like I said, you really do want to use a JSON parser of some kind if you are using JSON more than a few times in your program.
Code example:
String abc = "ABC:123,DEF:456,GHI:789";
String[] chucks = abc.split(",");
String[] oneToThree = chunks[0].split(":");
String nums = oneToThree[1];
System.out.println(nums);
//This will print 123
I know that someone already replied, but I've been doing this, hope that help too:
public class HelloWorld{
public static void main(String []args){
String text ="\"type\":\"image\",\"originX\":\"center\",\"originY\":\"center\",\"left\":135,\"top\":259,\"width\":270,\"height\":519,\"fill\":\"rgb(0,0,0)\",\"overlayFill\":null,\"stroke\":null,\"strokeWidth\":1,\"strokeDashArray\":null,\"strokeLineCap\":\"butt\",\"strokeLineJoin\":\"miter\",\"strokeMiterLimit\":10,\"scaleX\":1,\"scaleY\":1,\"angle\":0,\"flipX\":false,\"flipY\":false,\"opacity\":1,\"shadow\":null,\"visible\":true,\"clipTo\":null,\"src\":\"file:///C:/Users/Alvin%20Combrink/Dropbox/Entrepren%C3%B6rskap/Design/Hemsidan/Backgrunder/Labyrint.jpg\"";
//Just left and scaleX for example
String left = readValue(text, "left");
String scaleX = readValue(text, "scaleX");
System.out.println("left:" + left);
System.out.println("scaleX:" + scaleX);
}
public static String readValue(String text, String key)
{
//search for the init of the value
int start = text.indexOf("\"" + key + "\"");
//search for the end of the value
int end = text.indexOf(",", start + key.length() + 3);
//return the value. these + 3 , is for quotes and ":"
return text.substring(start + key.length() + 3,end);
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have one read method from excel file and return me String value.
ReqQty = 2 in my excel value but it is in string and from UI side application is not allowing to enter string value in "ReqQty" it is allowing only Integer value . so here i am not able to provide value in 'ReqQty' field by 'SendKeys' method .
The code is:
String Str = ExcelUtils.getcell(1,2) ;
Integer x = Integer.Valueof(x);
driver.findelement(By.xpath("xpath").sendkeys(Integer.valueof(x)); –
Here is something to get you going:
#Test
public void testParsing() {
String input = "ReqQty = 2";
Pattern pattern = Pattern.compile("ReqQty.*=.*([\\d]+)");
Matcher matcher = pattern.matcher(input);
if (!matcher.find()) {
throw new IllegalArgumentException("failed to parse digits from: " + input);
}
System.out.println(Integer.parseInt(matcher.group(1)));
}
The key elements:
a regular expression pattern used to "match" for a number of digits within a string
Integer.parseInt() to turn that substring of digits into a number
Beyond that: it looks like you are overburdening yourself. The error message you got is quite clear "ReqQty = 2" is not a string that only contains a number. Thus you have to extract that number part from this string. This is really basic stuff. If that is already beyond your skills, then fetching data from excel to pass that into selenium is probably beyond your current skills!
Thus: the real answer is - step back and study the java basics!
you can convert a string into an integer using Integer.parseInt(String x)
String x = "17";
int xAsInt = Integer.parseInt(x);
System.out.println(xAsInt);
To convert a string value into an integer you can try this
'int x = Integer.parseInt("1234");'
or if you are using StringBuilder or StringBuffer you can try this
'Integer.parseInt(myBuilderOrBuffer.toString());'
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to convert a string to an int but i take an error. Here is my code:
int foo = Integer.parseInt(ABoard[first]);
if (computerBoard[foo] != -1) {
second = computerBoard[foo];
} else {
second = board.getRandomPosition();
while (first==second) {
second = board.getRandomPosition();
}
}
I want to take a number (int) from a string array and then to take the value of this number from an int array if the value of this number isn't -1.
You need to trim off all of the whitespaces before you parse,
int foo = Integer.parseInt(ABoard[first].trim());
Or replace all non numeric digits
int foo = Integer.parseInt(ABoard[first].replaceAll("[^0-9\\-]", ""); //including 0-9 and -
In order to parse String to int you need to remove all non-numeric characters from it (assuming we are not interested in negative integers).
Here is the code to do that:
int foo = Integer.parseInt(ABoard[first].replaceAll("[^0-9]", ""));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to find all three letter substrings from a string in Java.
For example from the string "example string" I should get "exa", "xam", "amp", "mpl", "ple", "str", "tri", "rin", "ing".
I tried using the Java Regular expression "([a-zA-Z]){3}" but I only got "exa", "mpl", "str", "ing".
Can someone tell me a regex or method to correct this.
Implementing Juvanis' idea somewhat, iterate to get your substrings, then use a regular expression to make sure the substring is all letters:
String s = "example string";
for (int i = 0; i <= s.length() - 3; i++) {
String substr = s.substring(i, i + 3);
if (substr.matches("[a-zA-Z]+")) { System.out.println(substr); }
}
When a character is consumed in one regex, it cannot be used in other regexes. In your example, a is consumed in exa so amp will not be listed as output. You should try traditional iterative approach. It is easier to implement.
try this
Matcher m = Pattern.compile("([a-zA-Z]){3}").matcher("example string");
for (int i = 0; m.find(i); i = m.start() + 1) {
System.out.print(m.group() + " ");
}
output
exa xam amp mpl ple str tri rin ing
This can be done using regex as follows:
Find the position of all matches for the string using the regex \w(?=\w\w). This will give you the start index of the first character of each required sub-string.
In this case, you would get: 0, 1, 2, 3, 4, 8, 9, 10 and 11.
Get what you need by taking the sub-strings starting from each position going upto that plus 2.
In this case, that would mean, my_string.substring(0,3), my_string.substring(1,4) and so on, as the begin index parameter is inclusive while the end index parameter is exclusive.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string containing this:
D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo.
I want to get just this part:
wt\lifecycle\StateRB
How can I do that?
You can simply spilt whole path to parts and then get the parts you want.
String path = "D:\ptc\Windchill_10.0\Windchill\wtCustom\wt\lifecycle\StateRB.rbInfo";
String[] parts = path.split("\\");
parts = Arrays.copyOfRange(parts, parts.length-3, parts.length);
Or you can get throught string using loop (this seems to be better)
int index = 0, i = 0;
Stack<String> al = new Stack<String>();
while((index = path.lastIndexOf()))!=-1 && i < 3) {
al.push((path = path.substring(index)));
i++;
}
String[] parts = (String[])al.toArray(); //if you don't have array elements
// in correct order, you can use
// Collections.reverse with Arrays.asList
// applied on array
You can use string tokeniezer with \ delimiter and fetch only last three string tokens. i hope that above path going to be constant always.
http://www.tutorialspoint.com/java/java_string_substring.htm
Check the above link
example :
String Str = new String("Welcome to Tutorialspoint.com");
System.out.print("Return Value :" );
System.out.println(Str.substring(10) );
System.out.print("Return Value :" );
System.out.println(Str.substring(10, 15) );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have some string type data in my database table like as D-101 and D-102.
I want to get automatically next data in front end (a JSF web application) to send to database which is D-103.
For this I want to get only int data from string.
How can I do this?
final Pattern lastIntPattern = Pattern.compile("[^0-9]+([0-9]+)$");
String input = D101;
Matcher matcher = lastIntPattern.matcher(input);
if (matcher.find()) {
String someNumberStr = matcher.group(1);
int lastNumberInt = Integer.parseInt(someNumberStr);
System.out.println("Test int output - " + lastNumberInt);
inputList.add(lastNumberInt);
}
then compare max value
int currentBranchCode = 0;
for (Integer CCS : companyArrayList) {
if (currentBranchCode <= CCS) {
currentBranchCode = CCS;
}
}
and then
currentBranchCode =currentBranchCode +1;
String codegenerate="D"+currentBranchCode;
you must try it.
In your managed bean you can use split() function.
String s = "D-101";
String[] arr = s.split("[^\\d]+");
System.out.println(arr[1]); //prints 101
OR
In the xhtml page you can write an EL like this. Note that myBean is the name of your bean and getColumnValue() method returns a value of a column(i.e. "D-101").
#{myBean.columnValue.split('[^\\d]+')[1]}
This can be done in just a couple of lines:
int i = Intger.parseInt(input.replaceAll(".*(?<!\\d)(\\d+)", "$1");
String next = input.replaceAll("(.*)(?<!\\d)\\d+", "$1"+ ++i);