-6

I want to make a simple program that can convert currencies and weights, etc. The only problem is that I don't know how to separate a string so that I can get each individual word.

So, if I have the string "200 GBP USD", how can I separate it so that I get

int currency = 200;
String currencyFrom = "GBP";
String currencyTo = "USD";
mgthomas99
  • 5,402
  • 3
  • 19
  • 21

2 Answers2

4

Just split the string using the String.split function.

String[] Parts = input.split(" ");
if(Parts.length>2)
{
    int currency = Integer.parseInt(Parts[0]);
    String currencyFrom = Parts[1];
    String currencyTo = Parts[2];
}
James Hunt
  • 2,448
  • 11
  • 23
2

You can use the function split:

String[] words = yourString.split(" ");
int currency = Integer.parseInt(words[0]);
String currencyFrom = words[1];
String currencyTo = words[2];
Balduz
  • 3,560
  • 19
  • 35