0

I have an array of String, containing doubles. An example could be a String containing [5, ., 2]. I want to be the double 2,5.

I know this is very confusing, but the way I got those numbers like that, is from an input like: "y=2.5x+4.3" (the variable called ligning).

while(erTal(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1))) == true){
                        xVærdi.add(ligning.substring((ligning.indexOf("x")-i),(ligning.indexOf("x")-i+1)));
                        System.out.println("There's a number to the left of x. Der er tilføjet " + i + " tal i alt");
                        i++;
                    }

Method to check if String is number:

public static boolean erTal(String str)  {
        if(str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") || str.equals("=")){
            return false;
        } else {
            return true;
        }     
    }
Bassusour
  • 119
  • 1
  • 12

2 Answers2

1

You can join the elements using empty string as the delimeter (basically undoing ligning) and then parse this string.

String[] ligning = {"2", ".", "5"};
String[] ligningReversed = new String[ligning.length];
for (int i = 0; i < ligning.length; i++) {
    ligningReversed[i] = ligning[ligning.length - i - 1];
}
double result = Double.parseDouble(String.join("", ligningReversed));

This will work even if the elements aren't single digits.

Mateusz
  • 2,873
  • 4
  • 25
  • 41
  • Thanks for this, but it doesn't reverse the digits. Lets say the input (ligning) is 2.3x, I willl get 3.2 this way. How do I reverse it so I'll get 2.3? – Bassusour May 01 '17 at 11:02
  • @Bassusour you need to reverse the array, there're numerous ways to do that. For a simple example please see the updated answer. – Mateusz May 01 '17 at 11:14
  • Thanks for the code, but I get an error at `ligningReversed[i] = ligning[ligning.length - i - 1];` telling "array required, but String found". – Bassusour May 01 '17 at 18:21
  • Instead of the hard-coded `ligning = {"2", ".", "5"};` it is actually a variable called xVærdi that contains those elements; not ligning. My bad. Moreover, xVærdi is an ArrayList, and not an array, so I cannot use it in your example. Could you show me how to do it, when the String is from ArrayList instead of array? – Bassusour May 01 '17 at 20:03
  • @Bassusour In that case you can use `Collections.reverse(xVærdi)` instead of the `for` loop for reversing. For converting between array list and array see [this](http://stackoverflow.com/questions/9572795/convert-list-to-array-in-java). – Mateusz May 01 '17 at 20:28
0

Try this, i think that what you are looking for:

    // reverse the string 
    String s = new StringBuilder("[5,.,2]").reverse().toString();
    // split the string with break expression 
    String[] str = s.split("\\b");
    //join the string with required format
    StringJoiner sj = new StringJoiner(".");
    for (String t : str) {
        // to get digits only
        if(t.matches("\\d+")){
            sj.add(t);
        }
    }
    System.out.println(sj.toString());

Hope that helps.

Ali
  • 26
  • 8