-5

For example I have a String like this:

String myString = "Money = 10
                   Arrows = 4"

I want to edit the arrows, so I have to find the word "Arrow" in the String and edit the number "4". Any idea how to do that? Thanks!

Skypit
  • 11
  • 4
  • use the `.contains()` method, i.e. `if (myString.contains("Arrow"))` – smoggers Feb 02 '17 at 14:40
  • 2
    you could use regex with a positive lookahead in order to replace the value. on the other side, having multiple different logical values and keys in a single variable isn´t the best idea. Before trying to find the value i´d reconsider the current implementation – SomeJavaGuy Feb 02 '17 at 14:40
  • try using `Hashmaps` instead of having `String` to do such things. – jack jay Feb 02 '17 at 14:42

2 Answers2

0

If you want to edit a value easily based on something else in the program, you can make it so the number is a variable instead. Judging by the code as well, you want there to be a new line, currently it's not doing that since you need to use "\n"

So the code should look like:

int arrows = 4;

String myString = "Money = 10" + "\n" + "Arrows = " + arrows;

If you then change the value of the integer arrows before declaring the string it will be different.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
0

I don't use java that much but if you need to find out is something is a letter you can use

  System.out.println(Character.isLetter('c'));
  System.out.println(Character.isLetter('5'));

And since your data is in a string you can loop trough it like trough an array, as far as I remember.

for(int i =0; i < yourStringName.length; i++)

But I must agree with @jack jay. So here is a helpful post Java associative-array

Community
  • 1
  • 1
user3350597
  • 471
  • 1
  • 4
  • 14