How to get only integer part from String variable? [closed] - java

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);

Related

How to convert String value to Integer value and want to provide by 'SendKeys' in webdriver [closed]

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.valueo‌​f(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());'

How to separating parts of a java string? [closed]

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);
}
}

Split email address containing multiple delimiters? [closed]

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
How to extract email address from String like below with delimiter as "AND" ?
String str="abb#AND.comANDbbb.comANDccc#xxd.AND";
Try with following code segment,
public class TokenizerTest
{
private final String testStr = "abb#AND.comANDbbb.comANDccc#xxd.AND";
public static void main(String[] args)
{
TokenizerTest tst = new TokenizerTest();
tst.tokenize();
}
public void tokenize()
{
if(testStr != null)
{
Pattern p = Pattern.compile("[*]*AND*[^.AND]");
String[] tokens = testStr.split(p.pattern());
for(String token:tokens)
{
System.out.println(token);
}
}
}
}
It results in
abb#AND.com
bb.com
cc#xxd.AND
Short answer: Computer says no.
Based on the string you provided, it can probably not be done in a reliable way, since it is not clear what is an email address, and what is a delimiter. In fact, it is difficult to extract more than one or two valid email-addresses at all from that line.
I recommend you first check your dataset to see if it is corrupted, then find a way to use a different delimiter.

to get the next character in the string [closed]

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
please help me find the code for getting the next character in the string
for example:
input string = "abcd"
output string = "bcde"
I can able to iterate only one character at the time.
thanks in advance
Get the ASCII of the last character in the String and increase the ASCII value by 1.
try this,
String sample = "abcd";
int value = (int) sample.charAt(sample.length() - 1); // here you get the ASCII value
System.out.println("" +((value < ((int)'z')) ? sample.substring(1) + (char) (value + 1) : sample));
Simply add one to the char values:
char[] chars = "abcd".toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] += 1;
}
String nextChars = new String(chars);

how to get last three words of a string? [closed]

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) );

Categories