Convert string to 2d double array [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 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.

Related

Add element to 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 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));

How to substract two strings [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 started learning Java and have some across some difficulties. I'm trying to subtract two strings.
for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032".
I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings.
I do not expect for anyone to do this for me, but I would love to get some insight,or to tell me that i'm headed in the right direction
thanks
public static String appliquerCoup( String combinaison, String coup ) {
String nouveauCoup="";
if(combinaison!=null&&coup!=null){
for(int i=0;i>combinaison.length();i++){
int a = Integer.parseInt(combinaison.substring(i, i + 1));
int b = Integer.parseInt(coup.substring(i, i + 1));
nouveauCoup=String.valueOf(a-b);
if(a-b<0){
nouveauCoup=0;
}
}
} // main
return nouveauCoup;
}
If I understand you question correctly. you want to subtract each digit individually.
So (0-1), (3-0), (2-0). The following program does this (yields -132):
public static void main(String[] args) {
String A = "032";
String B = "100";
String str = "";
for(int i = 0; i < A.length(); i++)
{
int a = Integer.parseInt(A.substring(i, i + 1));
int b = Integer.parseInt(B.substring(i, i + 1));
int c = a - b;
str += String.valueOf(c < 0 ? 0 : c);
}
System.out.println(str);
}
Essentially, extract the i-th character of each string, convert them to integers, then do the subtraction. Convert the result back to a string and append it to the result string.

String Array to Integer Array Java [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 8 years ago.
Improve this question
I have a string array for an app I am making. It loads an EditText, so I will instead create a dummy one.
String[] s = ["1","2","3"];
How would I change String s into an integer array, such as Int i?
Int[] i = [1,2,3];
I have tried Integer.parseInt on the string array but it does not work. Does anyone know how to convert string arrays into integer arrays?
Something like the following should work:
string[] s = ["1","2","3"];
int[] i = new int[s.length()];
for (int j = 0; j < s.length(); j++)
i[j] = Integer.parseInt(s[j]);

Java : Reverse the String but remain the index of the comma [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Reversing the string while maintaining the index of COMMA.
the string that need to be reversed : "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g".
Expected output: "g,od ,yzal ,eht ,revo, spmuj ,xof nw,orb kc,iuq eh,T"
use StringBuffer and Stack first remove all commas and save the index of them on stack after reversing insert commas at location that you saved them on stack. use stringBufferObject.deleteCharAt(index); for deleting a character and use stringBufferObject.reverse(); for reversing string.
Here is the code with your input
public class Test {
public static void main(String [] args){
String original = "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g";
String reverse = "";
int length = original.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);
System.out.println("Reverse of entered string is: "+reverse);
}
}
Here this is code you can use it
String original = "T,he ,quick, bro,wn f,ox jump,s over, the l,azy do,g";
String reverse = original.replaceAll(",","");
StringBuilder rev = new StringBuilder(reverse);
rev = rev.reverse();
String output="";
int j = 0;
for (int i = 0; i < original.length(); i++) {
if(original.charAt(i) == ','){
output += ",";
}else{
output+=rev.charAt(j++);
}
}
System.out.println(output);

how to create array from string in java with respective data items [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 9 years ago.
Improve this question
How to create an array from a string with two respective data items.
String str="USA*2*Japan*8^2*India*5^4^2*Germany*5*";
Here, I want to create an array of two items in .
Like this:
Arraylist<string> arr= [USA*2,Japan*8^2, India*5^4^2,Germany*5];
Here * is indicating main items and ^ is indicating sub items.
You are using the * to separate "main items" but it can also be inside the main item.
Your requirements look odd, but lets assume for the sake of argument that you are getting your input data like this and you want to split it like you suggested.
That means that every * that is preceded by a number is a separator, but a * that is not preceded by a number is not.
You can achieve that using regular expressions: (with a positive look-behind expression (?<=expr)
String str = "USA*2*Japan*8^2*India*5^4^2*Germany*5";
List<String> lst = Arrays.asList(Pattern.compile("(?<=\\d)\\*").split(str));
System.out.println(lst);
Prints:
[USA*2, Japan*8^2, India*5^4^2, Germany*5]
After further clarification in the comment below, it seems that the problem is more generic than the initial example; the question becomes:
How do I split a string on a separator, but only after 2 occurrences
of the separator.
Although it's possible to do with a regex, it may be easier to do and understand in a for loop like this:
public static List<String> split(String str, char splitChar, int afterOccurrences) {
List<String> lst = new ArrayList<>();
int occurrencesSeen = 0;
int start = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (ch == splitChar) {
occurrencesSeen++;
if (occurrencesSeen >= afterOccurrences) {
lst.add(str.substring(start, i));
start = i + 1;
occurrencesSeen = 0;
}
}
}
if (start < str.length() - 1)
lst.add(str.substring(start));
return lst;
}
public static void main(String[] args) {
String str = "USA*2*Japan*8^2*India*5^4^2*Germany*5";
System.out.println(split(str, '*', 2));
}
This method also allows you to split after 3 or any other number of occurrences.

Categories