Parsing and iterating String to arraylist [closed] - java

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 a String with value as:
String lstr = [12.88, 77.56],[12.81, 77.7156]
....so on
I need to parse and iterate it and somehow substitute the values as :
final ArrayList<Coordinate> points = new ArrayList<Coordinate>();
points.add(new Coordinate(12.88, 77.56));
points.add(new Coordinate(12.81, 77.7156));
i tried converting string to List and then iterating it using for loop, but it is not working, either it goes out of bound or extra square bracket throws an exception.
What is the best way to parse, format and iterate a string like this?

You can do something like in the example below. Note you can improve your regex to check for spaces etc.:
String lstr = "[12.88, 77.56],[12.81, 77.7156]";
List<Coordinate> cors = new ArrayList<Coordinate>();
String []coordinates = lstr.split("\\],\\[");
for(String cordinate:coordinates)
{
String []xy = cordinate.split(",");
cors.add(new Coordinate(xy[0],xy[1]));
}
System.out.println(cors);

Related

What do i need to do, to make this trim method work? [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 6 years ago.
Improve this question
enter image description hereI'm trying to use trim to figure out if someone imputed an empty string, and return the response " Say something, please". This is the peace of code:
else if(statement.trim().length() == 0 )
{
response = "Say something, please";
}
To invoke the methods from String, you invoke from the String variable. Not the String class.
You probably wanted:
else if(userInput.trim().length() == 0)
where userInput is the string object you are interested to check whether it is empty.
Similar to what Danny said.
Before your if/else branches you should have a string variable already. Then you simply call trim on that variable.
String s = "Hey this isn't empty!! ";
if(false){
// never runs
else if(s.trim().length() == 0){
response = "Say something please";
}
You need first to create an instance of String
String Str = new String();
Then invocke trim methid
str.trim();

How to increase the value by +1 in the string(java) and the string is mentioned below [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 6 years ago.
Improve this question
"Sales Docket successfully saved and sent for approval. Please note your document number. JBHL/39/16-17"
i want only the number 39 in the string should be increased by +1 when we run the method
Use regular expression to find the number, then build new string:
private static String increment(String input) {
Matcher m = Pattern.compile("/(\\d+)/").matcher(input);
if (! m.find())
throw new IllegalArgumentException("Invalid document number: " + input);
int newNumber = Integer.parseInt(m.group(1)) + 1;
return input.substring(0, m.start(1)) + newNumber + input.substring(m.end(1));
}
Test
System.out.println(increment("JBHL/39/16-17"));
Output
JBHL/40/16-17

best and simple String parsing in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Ex String Param=Value1:100,Value2:2000,Value3:30000:
What is the best way in java to trim the above mentioned string format to get the First element's value?
Result should be 100 (from the above mentioned Ex string param).
Assuming you have a String e = "Value1:100,Value2:2000,Value3:30000";
Your next step would be to analyze its structure. In this case it is very simple key:value which is comma separated.
We have to split at every "," and then ":".
String[] keyValues = e.split(",");
for(String keyValue : keyValues) {
String tupel = keyValue.split(":");
// tupel[0] is your key and tupel [1] is your value
}
You can now work with this. You can add these to a map to access it by name.
Also this How to search a string of key/value pairs in Java could be worth looking at.
If you only want the first value, you can take a substring up to the first ',':
String p = "Value1:100,Value2:2000,Value3:30000:";
int firstComma = p.indexOf(',');
if(firstComma >= 0) {
p = p.substring(0, firstComma);
}
String tuple[] = p.split(":");

php take raw input to a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Consider this piece of Java code
I need to do this in php
String SEPARATOR = "S39Er#T0R";
String input = "someS39Er#T0RDataS39Er#T0Rhere";
String[] val = input.split(SEPERATOR);
for (int i = 0; i < val.length; i++) {
}
I need to store the data received from file_get_contents('php://input') which returns the raw post
and then from that string I need it to split and run a for loop
Can any one suggest how to implement the same code in php ?
explode function splits a string by a string;
preg_split splits a string by a regex;
foreach construct iterates over arrays or objects;
Combination of these is pretty straightforward:
$separator = 'S39Er#T0R';
$postData = file_get_contents('php://input');
$splitPostData = explode($separator, $postData);
foreach($splitPostData as $postDataItem)
{
// do something with $postDataItem
}

Put each character in a string into its own string - java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
I have a string and I want to have that string get split up so that each individual character is in its own string. The string will vary in length as it is user inputted. Thanks in advance
If you actually want an array of strings from a string you can try this
String[] chars = myString.split("");
String str = /*Your String here*/;
char[] charArray = str.toCharArray();
String[] strArray = new String[charArray.length];
String strChars = "";
for (Character c : charArray){
int i=0;
strChars = c.toString();
strArray[i] = strChars;
System.out.println(strChars);
i++;
}
System.out.println(strArray.length);

Categories

Resources