0

Hello I want to string concatenate two numbers as in the code below:

tmp2 = Integer.toString(preresult) + tmp2.substring(2, tmp2.length());

Tmp2 is a string declared from before. Preresult is a integer that contains a number. If I print this line it adds the two values instead of string concatenation.

If I change Integer.toString(preresult) to for example Integer.toString(5) it string concatenates as I would like it to do. But having Integer.toString(preresult) it adds the two numbers instead of string concatenating.

The code for preresult :

preresult = Integer.parseInt(tmp2.substring(0, 1)) + Integer.parseInt(tmp2.substring(1, 2));

//It picks numbers from tmp2 and adds them together. If I print preresult it gives me a int (for example 9)

Once again please help me concatenate these two strings instead of adding them:

tmp2 = Integer.toString(preresult) + tmp2.substring(2, tmp2.length()); 

New to java please mercy :)

6
  • 1
    The expression Integer.toString(preresult) + tmp2.substring(2, tmp2.length()) will most certainly concatenate Strings and not sum numbers. Please provide a complete example that explains, what makes you think that there is arithmetics involved here. Commented Sep 22, 2015 at 18:11
  • Thank you for fast answer. I've tried printing before and after tmp2 = Integer.toString(preresult) + tmp2.substring(2, tmp2.length());. It does not concatenate the values instead it adds them. As i said earlier if i change interger.toString(preresult) to maybe Integer.toString(6) it concatenates them. Commented Sep 22, 2015 at 18:18
  • It does not concatenate the values instead it adds them. Now that's just a claim that cannot be true. We would like to help you figuring out, how you get to that conclusion. But in order to do so, we need some more insight. Please provide more than just code snippets. Edit your question and provide one whole main method you used for testing, including many System.out.println calls in order to print intermediate results and show us, which values those println calls print out. That is something we could work with. Commented Sep 22, 2015 at 18:22
  • What is the output for the println(tmp2);, before and after the "concatenation line"? Commented Sep 22, 2015 at 18:32
  • No problem, we like helping other with programming questions. But, please, provide a short, complete and self-contained example next time. At first, you provided only 1 line. That way we couldn't see the relevant part (because it wasn't there). Then you went to the other extreme and gave us the whole class, which is too much irrelevant code and doesn't isolate the code (we don't know, who calls it and how). If possible, try to reproduce your problem in an example as one single, short main method and that contains println calls, what they print and what you expected them to print. Commented Sep 22, 2015 at 18:54

2 Answers 2

1

Are you looking for this type of operation

class String1
{
    public static void main(String args[])
    {
        int a = 100;
        int b = 200;
        String s1 = Integer.toString(a);
        String s2 = Integer.toString(b);
        System.out.println(s1+s2);
    }
}

Output - 100200

Sign up to request clarification or add additional context in comments.

Comments

0

The line you are giving us does concatenate the Strings, but maybe you got confused because the line before sums them:

// tmp2 == "45" (taken from your comment)

preresult = Integer.parseInt(tmp2.substring(0, 1)) + Integer.parseInt(tmp2.substring(1, 2));
//                           |------ "4" -------|                     |-------- "5" -----|
//          |--------------- 4 -----------------|    |------------------ 5 --------------|
// preresult == 4 + 5 == 9

println(tmp2); // prints the "45" (unchanged)

tmp2 = Integer.toString(preresult) + tmp2.substring(2, tmp2.length());
//                      |-- 9 --|                     |"45"|
//     |----------- "9" ---------|   |------------- "" -------------|
// tmp2 == "9" + "" == "9"

println(tmp2); // prints "9"

So your first line sums the two digits 4 and 5, which results in 9. The line you had given us just concatenates that result "9" (as String) with another empty String and therefore keeps the "9".

1 Comment

Yeah, thanks I got confused. Thank you for your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.