Add element to a string [closed] - java

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 5 years ago.
Improve this question
I use java and I have the following string:
points="335,234 285,320 185,320 135,234 186,147 285,147 335,233 ";
How it is possible to add 2 to each number?...for example:
points="337,236 287,322 187,322 137,236 188,149 287,149 337,235 ";

You can use String#split to get all the separate numbers in an array, then use a for to iterate through them:
String points = "335,234,285,320,185,320,135,234,186,147,285,147,335,233";
String[] indvPoints = points.split(",");
for(int i = 0; i < indvPoints.length; i++) {
indvPoints[i] = String.valueOf(Integer.parseInt(indvPoints[i]) + 2);
}
points = Arrays.toString(indvPoints).replaceAll("[\\[\\] ]", "");
System.out.println(points);
Although I suggest you just use an int array to begin with, it would be much more efficient and less likely to encounter errors:
int[] points = {335,234,285,320,185,320,135,234,186,147,285,147,335,233};
for(int i = 0; i < points.length; i++) {
points[i] += 2;
}
System.out.println(Arrays.toString(points));

Related

check regex for integers bigger than the negative -328 [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 2 years ago.
Improve this question
I'm looking for a regex that is able to match numbers bigger than -328, and if it is possible to provide another solution to match the same pattern but without the zero. I tried many things but still not sure about how it works, for example, ^\-?[0-9]\d{3,}$
I'm using it with the com.jfoenix.validation.RegexValidator in order to check the pattern in a textfield.
Thanks
Try this.
String pat = "^-(32[0-7]|3[0-1]\\d|[1-2]\\d\\d|\\d{1,2})|\\d+$";
for (int i = -1000; i <= 1000; ++i) {
String s = Integer.toString(i);
boolean result = s.matches(pat);
if (result != (i > -328))
System.out.println(i + " fail!");
}

How tho check in Java if there is same characters in a String? [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 5 years ago.
Improve this question
I've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}

How to decrease my repeated code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hi How I write these several code in only one line
if (stimee.getText().toString().equals("0")) {stimevar="00"; }
if (stimee.getText().toString().equals("1")) {stimevar="01"; }
if (stimee.getText().toString().equals("2")) {stimevar="02"; }
if (stimee.getText().toString().equals("3")) {stimevar="03"; }
if (stimee.getText().toString().equals("4")) {stimevar="04"; }
if (stimee.getText().toString().equals("5")) {stimevar="05"; }
if (stimee.getText().toString().equals("6")) {stimevar="06"; }
if (stimee.getText().toString().equals("7")) {stimevar="07"; }
if (stimee.getText().toString().equals("8")) {stimevar="08"; }
if (stimee.getText().toString().equals("9")) {stimevar="09"; }
all you really need is:
stimevar="0"+stimee.getText().toString();
since i dont see an else clause in your question, this must solve your problem.
You can use a Map to store the values and then set the variable.
Map<String, String> map = new HashMap<>();
map.put("0", "00");
map.put("0", "01");
...
map.put("9", "09");
stimevar = map.get(stimee.getText().toString());
String str = stimee.getText().toString();
for (int i = 0; i <= 9; i++)
if (("" + i).equals(str))
stimevar = "0" + i;
// parse input value as integer
int value = Integer.parseInt(stimee.getText().toString());
// check input for values from 0 to 9
for (int i = 0; i < 10; i ++) {
// if found a match
if (value == i) {
// set variable
stimevar = String.format("%02d", i);
// and stop checking
break;
}
}
EDIT: Thanks to #Tom for pointing out parsing the String only once beforehand would be more effective.

Convert string to 2d double array [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
I want to convert string to 2d double array.My string is :"(-34.17141334413566, 148.1231689453125),(-34.371148707267096, 149.0130615234375),(-34.475366823896806, 147.919921875)". And the result is like
double[3][3] y = {{-34.17141334413566, 148.1231689453125},{-34.371148707267096,149.0130615234375},{-34.475366823896806, 147.919921875}}.I am new to java.help anybody thanks in advance.
Try this: (str is your input)
String str = "(-34.17141334413566, 148.1231689453125)," +
"(-34.371148707267096, 149.0130615234375)," +
"(-34.475366823896806, 147.919921875)";
str = str.replace("(", "");
String[] rows = StringUtils.split(str, "),");
double[][] doubles = new double[rows.length][StringUtils.split(rows[0], ", ").length];
for (int i = 0; i < rows.length; i++)
{
String[] cols = StringUtils.split(rows[i], ", ");
for (int j = 0; j < cols.length; j++)
{
cols[j] = cols[j].replace(")", "");
doubles[i][j] = Double.parseDouble(cols[j]);
}
}
Make sure there is at least one row (check it). StringUtils is an Apache Commons object, get its JAR for free from here: http://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.3.2
If I understand the question, you want to convert doubles data from a String (text file, scanner, ...)
You can use something based on that then :
Double[] array = new Double[2];
String s = "-34.17141334413566, 148.12316894531252";
String[] stringArray = s.split(",");
array[0] = Double.parseDouble(stringArray[0].trim());
array[1] = Double.parseDouble(stringArray[1].trim());
Be careful with Double.parseDouble(String s) and his exception.
And if I didn't understand the question, sorry ;)
EDIT : Of course my example is for a one dimension array, use a different character splitter for your second dimension.

Comparing multiple strings in Java [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
I have 5 distinct strings called, say, string1 through string5.
I want to write a simple if statement that runs if any two of the five strings contain the same string. How would I do that?
Thanks in advance!
Comparison is a binary operation, therefore you can always compare only two objects at a time. I would suggest using a cycle and comparing each string to the remaining ones.
public boolean multipleStringEquals(String[] strings) {
for (int i = 0; i < strings.length; i++) {
for (int j = i + 1; j < strings.length; j++) {
if (strings[i].equals(strings[j])) {
return true;
}
}
}
return false;
}

Categories