GregB GregB - 1 year ago 92
Java Question

Understanding string utilisation in concatenation of integer to a textview output

Am having trouble with an example from a textbook I am working from. Am an absolute beginner so do not fully understand classes and static methods so think that the problem may be somewhere there but would love any clarity or perhaps redirection anyone could provide.

Anyhow, I am writing a math game which multiplies two numbers(int partA and partB)and asks you to give the result. The problem that I am experiencing is that when I use the code line example.setText("" + partA) to put the value of part A on to the android devices display I get a concatenation error despite it being what the textbook says to use. To try and avoid this I have deployed a string which I hope does the same thing using this code
however this generates a whole new error which is
Static member 'java.lang.String.valueOf(int)' accessed via instance reference less...
Shows references to static methods and fields via class instance rather than a class itself.
Can anyone give me a clue as to how to get this to work. I feel confident in the rest of the code and its really annoying to have the app crash just because of some concatenating disagreement!

Any help gratefully received and if you need to see more of the code let me know which bits and I'll copy and paste,

Have read through the answers and it looks like I am not doing anything exactly wrong so have attached the code I'm using to see if anyone can spot the error which is causing the app to stop working when it loads this script. I have tried running with and without this chunk of code and everything seems to run with it out and crash with it in.

/*Now put the variables in the text holes we've created
//And the answer variables on the buttons
//now we listen for presses of answer buttons

also the logcat for the error is as follows:-

at java.lang.ThreadGroup.uncaughtException(
at java.lang.ThreadGroup.uncaughtException(
at dalvik.system.NativeStart.main(*/

Please let me know if any further info is useful.

Thanks again,

Answer Source

Android-Studio is warning you about two different issues. Neither of them will crash your app.

  1. Concatenation issue:

    Android-Studio detects that your message has a text part ("") and a number part (partA), and you are concatenating both, without defining a string resource.

    In your case, this is a false positive, as your sole purpose with the concatenation ("" + partA) is to convert the number to a String.

    However, if you wanted your TextView to display the text "Part 1: number", you would probably be inclined to it like so: example.setText("Part A: " + partA). However, that is not totally correct, because that is not good for multi-language support.

    In that case, you should define a resource string in your strings.xml file, like so:

    <string name="part_a">Part A: %d</string>

    Having in mind that %d is a placeholder for your number, you should then reference that resource string, by calling Context#getString(int, Object...) or Resources#getString(int, Object...).

    In your case, and assuming you are calling it from an Activity, you should do it like so:

    example.setText(getString(R.string.part_a, partA))

  2. Static issue:

    In Java, Classes can define static methods.

    static methods are shared across all instances of a Class, and don't depend on any single instance of that Class.

    Since the result doesn't change depending on the instance, it defeats the purpose of calling the method directly from it.

    So, using your example, calling stringPartA.valueOf(partA) would yield exactly the same result as calling stringPartB.valueOf(partA), because valueOf() is a static method of the String class.

    In that sense, it is more correct to call String.valueOf(partA).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download