-2

I have a Java String such as "10 20 30 40 50". I want to process these values using StringTokenizer basing on some business requirements multiple times.

For example, for certain condition, I want to double each of the values i.e., "20 40 60 80 100" and for another condition, I want to treble i.e., "30 60 90 120 150".

I found it can be done by creating multiple StringTokenizer, one for each condition. My question is, can it be done using a single instance of StringTokenizer? Please give efficient solution if any.

Here is my coding effort:

String str = "10 20 30 40 50";
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens()){
    int i = Integer.parseInt(st.nextToken())*2;
    System.out.println(i);
}

Can I use the same instance 'st' for trebling the values?

EDIT : I know it can be done through another array or another list. But I want to know how I can reuse the same StringTokenizer for trebling as I did for doubling the values.

RLD
  • 1,867
  • 3
  • 15
  • 20
  • 2
    We won't just write code for you. Show your effort. – tnw Nov 29 '16 at 20:15
  • 1
    From the [JavaDoc for `StringTokenizer`](https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html): "_`StringTokenizer` is a legacy class that is retained for compatibility reasons although its use is discouraged in new code_". This is OracleSpeak for **do not use this `class`. Ever.** – Boris the Spider Nov 29 '16 at 20:16
  • 1
    Parse your string once, *store the values*, and then you can operate on them multiple times. But no, I'm not providing code, not for a question with so little effort put into it. – dcsohl Nov 29 '16 at 20:22
  • @dcsohl, you are right in your approach. But I want a solution with single StringTokenizer. I have shown my code. I don't know who has downvoted without understanding the question. – RLD Nov 29 '16 at 20:48
  • Ok, but my approach uses a single StringTokenizer. Store your values in, for example, a `List`. Then you can operate on them to your heart's content without ever tokenizing again. (Also, do consider that maybe people downvoted *because* they understood the question, that you're looking for someone to do your homework for you.) – dcsohl Nov 29 '16 at 21:02

2 Answers2

0

I would write a function that takes a string and a int(as a multiplier) and returns a string. Inside the function it parses the string based on spaces and then converts the strings into an array of ints. Then the code will multiply the array of ints by the int that you passed into the function. After all that it will concatonate the ints together into an empty string adding the spaces back in after each int it adds, in order to return that string at the end of the function.

As a new programmer, Train yourself to not use 3rd party libraries wherever possible. It might seem difficult at first, but it will pay off in the long run!

Sethmr
  • 3,046
  • 1
  • 24
  • 42
0

You can avoid using StringBuilder altogether, since it is discouraged. Following is a version which you can use/modify according to your needs. The code is pretty much self-explanatory:

    public static String operate(int[] intArrNumbers,int multiplier ){
        StringBuilder finalStr = new StringBuilder();
        for(int i: intArrNumbers){
            finalStr.append(i*multiplier);
            finalStr.append("\n");
        }
        return finalStr.toString();
    }


    public static void main(String[] args){
        String str = "10 20 30 40 50";
        String[] strArrNumbers = str.split(" ");
        int size = strArrNumbers.length;
        int[] intArrNumbers = new int[size];
        int index=0;
        for(String z:strArrNumbers){
            intArrNumbers[index]= Integer.parseInt(z);
            index++;
        }
        int multiplier = 3;

        String result = operate(intArrNumbers,multiplier);
        System.out.println(result);


}
Chetan Jadhav CD
  • 1,116
  • 8
  • 14