Why does it output Integers instead of letters?In Java, is the result of the addition of two chars an int or...

How to loop for 3 times in bash script when docker push fails?

How to md5 a list of filepaths contained in a file?

Misspelling my name on my mathematical publications

Referring to different instances of the same character in time travel

Did the Vulgar Latin verb "toccare" exist?

What explains 9 speed cassettes price differences?

Was I subtly told to resign?

Should disabled buttons give feedback when clicked?

Combining latex input and sed

Cops: The Hidden OEIS Substring

Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?

Terry Pratchett book with a lawyer dragon and sheep

Does Lufthansa weigh your carry on luggage?

definition of "percentile"

How to memorize multiple pieces?

How many hours would it take to watch all of Doctor Who?

US Civil War story: man hanged from a bridge

Need help identifying planes, near Toronto

Does throwing a penny at a train stop the train?

What is a solution?

Are neural networks prone to catastrophic forgetting?

Why were Er and Onan punished if they were under 20?

Is Trump personally blocking people on Twitter?

Finding the nth term of sequence of 3, 10, 31, 94, 283...



Why does it output Integers instead of letters?


In Java, is the result of the addition of two chars an int or a char?String builder vs string concatenationWhat is a serialVersionUID and why should I use it?How do I generate random integers within a specific range in Java?Python join: why is it string.join(list) instead of list.join(string)?How do I make the first letter of a string uppercase in JavaScript?Does Python have a string 'contains' substring method?Why is subtracting these two times (in 1927) giving a strange result?Why don't Java's +=, -=, *=, /= compound assignment operators require casting?Why is char[] preferred over String for passwords?Why is processing a sorted array faster than processing an unsorted array?Why does this code using random strings print “hello world”?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







6















So this is the task: Given a string, return a string where for every char in the original, there are two chars.



And I don't understand why its output are numbers instead of letters, I tried doesn't work?



public String doubleChar(String str) {
String s = "";
for(int i=0; i<str.length(); i++){
s += str.charAt(i) + str.charAt(i);

}
return s;
}


Expected :




doubleChar("The") → "TThhee"



doubleChar("AAbb") → "AAAAbbbb"




Output:




doubleChar("The") → "168208202"



doubleChar("AAbb") → "130130196196"











share|improve this question




















  • 5





    Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

    – Andrey Akhmetov
    8 hours ago


















6















So this is the task: Given a string, return a string where for every char in the original, there are two chars.



And I don't understand why its output are numbers instead of letters, I tried doesn't work?



public String doubleChar(String str) {
String s = "";
for(int i=0; i<str.length(); i++){
s += str.charAt(i) + str.charAt(i);

}
return s;
}


Expected :




doubleChar("The") → "TThhee"



doubleChar("AAbb") → "AAAAbbbb"




Output:




doubleChar("The") → "168208202"



doubleChar("AAbb") → "130130196196"











share|improve this question




















  • 5





    Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

    – Andrey Akhmetov
    8 hours ago














6












6








6








So this is the task: Given a string, return a string where for every char in the original, there are two chars.



And I don't understand why its output are numbers instead of letters, I tried doesn't work?



public String doubleChar(String str) {
String s = "";
for(int i=0; i<str.length(); i++){
s += str.charAt(i) + str.charAt(i);

}
return s;
}


Expected :




doubleChar("The") → "TThhee"



doubleChar("AAbb") → "AAAAbbbb"




Output:




doubleChar("The") → "168208202"



doubleChar("AAbb") → "130130196196"











share|improve this question
















So this is the task: Given a string, return a string where for every char in the original, there are two chars.



And I don't understand why its output are numbers instead of letters, I tried doesn't work?



public String doubleChar(String str) {
String s = "";
for(int i=0; i<str.length(); i++){
s += str.charAt(i) + str.charAt(i);

}
return s;
}


Expected :




doubleChar("The") → "TThhee"



doubleChar("AAbb") → "AAAAbbbb"




Output:




doubleChar("The") → "168208202"



doubleChar("AAbb") → "130130196196"








java string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago







stevie lol

















asked 8 hours ago









stevie lolstevie lol

413 bronze badges




413 bronze badges








  • 5





    Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

    – Andrey Akhmetov
    8 hours ago














  • 5





    Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

    – Andrey Akhmetov
    8 hours ago








5




5





Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

– Andrey Akhmetov
8 hours ago





Adding two chars doesn't give you a string with those two characters, but rather an int with numerical value equal to the sum of the two characters' numerical values.

– Andrey Akhmetov
8 hours ago












5 Answers
5






active

oldest

votes


















8














In Java the char primitive type is basically just a numeric value that maps to a character, so if you add two char values together they produce a number and not another char (and not a String) so you end up with an int as you're seeing.



To fix this you can use the Character.toString(char) method like this:



s += Character.toString(str.charAt(i)) + Character.toString(str.charAt(i))


But this is all fairly inefficient because you're doing this in a loop and so string concatenation is producing a lot of String objects needlessly. More efficient is to use a StringBuilder and its append(char) method like this:



StringBuilder sb = new StringBuilder(str.length() * 2);
for (int i = 0; i < str.length(); ++i) {
char c = str.charAt(i);
sb.append(c).append(c);
}
return sb.toString();





share|improve this answer



















  • 1





    This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

    – Hayes Roach
    8 hours ago



















7














You are adding numeric values of chars first before concatenating the result (now integer) to the string. Try debugging with print statements:



public static String doubleChar(String str) {
String s = "";
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
System.out.println(str.charAt(i) + str.charAt(i));
s += str.charAt(i) + str.charAt(i);
}
return s;
}


The more efficient way of doing what you want is:



public static String doubleChar(String str) {
StringBuilder sb = new StringBuilder(str.length() * 2);
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
sb.append(c).append(c);
}
return sb.toString();
}





share|improve this answer



















  • 2





    I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

    – Nexevis
    8 hours ago





















4














Why does it output integers?



The + operator is overloaded in Java to perform String concatenation only for Strings, not chars.



From the Java Spec:




If the type of either operand of a + operator is String, then the
operation is string concatenation.



Otherwise, the type of each of the operands of the + operator must be
a type that is convertible (§5.1.8) to a primitive numeric type, or a
compile-time error occurs.




In your case, char is converted to its primitive value (int), then added.



Instead, use StringBuilder.append(char) to concatenate them into a String.



If performance is not a concern, you could even do:



char c = 'A';
String s = "" + c + c;


and s += "" + c + c;



That will force the + String concatenation operator because it starts with a String (""). The Java Spec above explains with examples:




The + operator is syntactically left-associative, no matter whether it
is determined by type analysis to represent string concatenation or
numeric addition. In some cases care is required to get the desired
result. For example [...]



1 + 2 + " fiddlers" is "3 fiddlers"



but the result of:



"fiddlers " + 1 + 2 is "fiddlers 12"







share|improve this answer

































    1














    You are adding the value of two characters together. Change the String concatenation from:



    s +=  str.charAt(i) + str.charAt(i);


    To:



    s +=  str.charAt(i) + "" + str.charAt(i);


    Which will ensure the characters convert to a String.



    Note: This is a quick fix, and you should use StringBuilder when String concatenating inside of a loop. See the other answers for how this is done.






    share|improve this answer

































      1














      String.charAt() returns a char (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) so you're dealing with a single character. With onechar, you can perform operations on it like this (which will print "65" which is ASCII value for the 'A' character):



      System.out.println('A' + 0);


      you can print the ASCII value for the next character ("B") by adding 1 to 'A', like this:



      System.out.println('A' + 1);


      To make your code work – so that it doubles each character – there are number of options. You could append each character one at a time:



      s += str.charAt(i);
      s += str.charAt(i);


      or various ways of casting the operation to a string:



      s += "" + str.charAt(i) + str.charAt(i);





      share|improve this answer




























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56977717%2fwhy-does-it-output-integers-instead-of-letters%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        8














        In Java the char primitive type is basically just a numeric value that maps to a character, so if you add two char values together they produce a number and not another char (and not a String) so you end up with an int as you're seeing.



        To fix this you can use the Character.toString(char) method like this:



        s += Character.toString(str.charAt(i)) + Character.toString(str.charAt(i))


        But this is all fairly inefficient because you're doing this in a loop and so string concatenation is producing a lot of String objects needlessly. More efficient is to use a StringBuilder and its append(char) method like this:



        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); ++i) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();





        share|improve this answer



















        • 1





          This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

          – Hayes Roach
          8 hours ago
















        8














        In Java the char primitive type is basically just a numeric value that maps to a character, so if you add two char values together they produce a number and not another char (and not a String) so you end up with an int as you're seeing.



        To fix this you can use the Character.toString(char) method like this:



        s += Character.toString(str.charAt(i)) + Character.toString(str.charAt(i))


        But this is all fairly inefficient because you're doing this in a loop and so string concatenation is producing a lot of String objects needlessly. More efficient is to use a StringBuilder and its append(char) method like this:



        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); ++i) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();





        share|improve this answer



















        • 1





          This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

          – Hayes Roach
          8 hours ago














        8












        8








        8







        In Java the char primitive type is basically just a numeric value that maps to a character, so if you add two char values together they produce a number and not another char (and not a String) so you end up with an int as you're seeing.



        To fix this you can use the Character.toString(char) method like this:



        s += Character.toString(str.charAt(i)) + Character.toString(str.charAt(i))


        But this is all fairly inefficient because you're doing this in a loop and so string concatenation is producing a lot of String objects needlessly. More efficient is to use a StringBuilder and its append(char) method like this:



        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); ++i) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();





        share|improve this answer













        In Java the char primitive type is basically just a numeric value that maps to a character, so if you add two char values together they produce a number and not another char (and not a String) so you end up with an int as you're seeing.



        To fix this you can use the Character.toString(char) method like this:



        s += Character.toString(str.charAt(i)) + Character.toString(str.charAt(i))


        But this is all fairly inefficient because you're doing this in a loop and so string concatenation is producing a lot of String objects needlessly. More efficient is to use a StringBuilder and its append(char) method like this:



        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); ++i) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        BobulousBobulous

        10.2k4 gold badges30 silver badges53 bronze badges




        10.2k4 gold badges30 silver badges53 bronze badges








        • 1





          This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

          – Hayes Roach
          8 hours ago














        • 1





          This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

          – Hayes Roach
          8 hours ago








        1




        1





        This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

        – Hayes Roach
        8 hours ago





        This should be correct answer. Creating new String objects each loop iteration is not good practice. Using a StringBuilder is more efficient. Look here for exaplanation stackoverflow.com/a/18453485/8685250

        – Hayes Roach
        8 hours ago













        7














        You are adding numeric values of chars first before concatenating the result (now integer) to the string. Try debugging with print statements:



        public static String doubleChar(String str) {
        String s = "";
        for (int i = 0; i < str.length(); i++) {
        System.out.println(str.charAt(i));
        System.out.println(str.charAt(i) + str.charAt(i));
        s += str.charAt(i) + str.charAt(i);
        }
        return s;
        }


        The more efficient way of doing what you want is:



        public static String doubleChar(String str) {
        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();
        }





        share|improve this answer



















        • 2





          I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

          – Nexevis
          8 hours ago


















        7














        You are adding numeric values of chars first before concatenating the result (now integer) to the string. Try debugging with print statements:



        public static String doubleChar(String str) {
        String s = "";
        for (int i = 0; i < str.length(); i++) {
        System.out.println(str.charAt(i));
        System.out.println(str.charAt(i) + str.charAt(i));
        s += str.charAt(i) + str.charAt(i);
        }
        return s;
        }


        The more efficient way of doing what you want is:



        public static String doubleChar(String str) {
        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();
        }





        share|improve this answer



















        • 2





          I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

          – Nexevis
          8 hours ago
















        7












        7








        7







        You are adding numeric values of chars first before concatenating the result (now integer) to the string. Try debugging with print statements:



        public static String doubleChar(String str) {
        String s = "";
        for (int i = 0; i < str.length(); i++) {
        System.out.println(str.charAt(i));
        System.out.println(str.charAt(i) + str.charAt(i));
        s += str.charAt(i) + str.charAt(i);
        }
        return s;
        }


        The more efficient way of doing what you want is:



        public static String doubleChar(String str) {
        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();
        }





        share|improve this answer













        You are adding numeric values of chars first before concatenating the result (now integer) to the string. Try debugging with print statements:



        public static String doubleChar(String str) {
        String s = "";
        for (int i = 0; i < str.length(); i++) {
        System.out.println(str.charAt(i));
        System.out.println(str.charAt(i) + str.charAt(i));
        s += str.charAt(i) + str.charAt(i);
        }
        return s;
        }


        The more efficient way of doing what you want is:



        public static String doubleChar(String str) {
        StringBuilder sb = new StringBuilder(str.length() * 2);
        for (int i = 0; i < str.length(); i++) {
        char c = str.charAt(i);
        sb.append(c).append(c);
        }
        return sb.toString();
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        LeffeBruneLeffeBrune

        2,5221 gold badge18 silver badges30 bronze badges




        2,5221 gold badge18 silver badges30 bronze badges








        • 2





          I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

          – Nexevis
          8 hours ago
















        • 2





          I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

          – Nexevis
          8 hours ago










        2




        2





        I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

        – Nexevis
        8 hours ago







        I agree he should be using StringBuilder since he is concatenating a String inside of a loop, but it may be helpful to explain briefly why it is better.

        – Nexevis
        8 hours ago













        4














        Why does it output integers?



        The + operator is overloaded in Java to perform String concatenation only for Strings, not chars.



        From the Java Spec:




        If the type of either operand of a + operator is String, then the
        operation is string concatenation.



        Otherwise, the type of each of the operands of the + operator must be
        a type that is convertible (§5.1.8) to a primitive numeric type, or a
        compile-time error occurs.




        In your case, char is converted to its primitive value (int), then added.



        Instead, use StringBuilder.append(char) to concatenate them into a String.



        If performance is not a concern, you could even do:



        char c = 'A';
        String s = "" + c + c;


        and s += "" + c + c;



        That will force the + String concatenation operator because it starts with a String (""). The Java Spec above explains with examples:




        The + operator is syntactically left-associative, no matter whether it
        is determined by type analysis to represent string concatenation or
        numeric addition. In some cases care is required to get the desired
        result. For example [...]



        1 + 2 + " fiddlers" is "3 fiddlers"



        but the result of:



        "fiddlers " + 1 + 2 is "fiddlers 12"







        share|improve this answer






























          4














          Why does it output integers?



          The + operator is overloaded in Java to perform String concatenation only for Strings, not chars.



          From the Java Spec:




          If the type of either operand of a + operator is String, then the
          operation is string concatenation.



          Otherwise, the type of each of the operands of the + operator must be
          a type that is convertible (§5.1.8) to a primitive numeric type, or a
          compile-time error occurs.




          In your case, char is converted to its primitive value (int), then added.



          Instead, use StringBuilder.append(char) to concatenate them into a String.



          If performance is not a concern, you could even do:



          char c = 'A';
          String s = "" + c + c;


          and s += "" + c + c;



          That will force the + String concatenation operator because it starts with a String (""). The Java Spec above explains with examples:




          The + operator is syntactically left-associative, no matter whether it
          is determined by type analysis to represent string concatenation or
          numeric addition. In some cases care is required to get the desired
          result. For example [...]



          1 + 2 + " fiddlers" is "3 fiddlers"



          but the result of:



          "fiddlers " + 1 + 2 is "fiddlers 12"







          share|improve this answer




























            4












            4








            4







            Why does it output integers?



            The + operator is overloaded in Java to perform String concatenation only for Strings, not chars.



            From the Java Spec:




            If the type of either operand of a + operator is String, then the
            operation is string concatenation.



            Otherwise, the type of each of the operands of the + operator must be
            a type that is convertible (§5.1.8) to a primitive numeric type, or a
            compile-time error occurs.




            In your case, char is converted to its primitive value (int), then added.



            Instead, use StringBuilder.append(char) to concatenate them into a String.



            If performance is not a concern, you could even do:



            char c = 'A';
            String s = "" + c + c;


            and s += "" + c + c;



            That will force the + String concatenation operator because it starts with a String (""). The Java Spec above explains with examples:




            The + operator is syntactically left-associative, no matter whether it
            is determined by type analysis to represent string concatenation or
            numeric addition. In some cases care is required to get the desired
            result. For example [...]



            1 + 2 + " fiddlers" is "3 fiddlers"



            but the result of:



            "fiddlers " + 1 + 2 is "fiddlers 12"







            share|improve this answer















            Why does it output integers?



            The + operator is overloaded in Java to perform String concatenation only for Strings, not chars.



            From the Java Spec:




            If the type of either operand of a + operator is String, then the
            operation is string concatenation.



            Otherwise, the type of each of the operands of the + operator must be
            a type that is convertible (§5.1.8) to a primitive numeric type, or a
            compile-time error occurs.




            In your case, char is converted to its primitive value (int), then added.



            Instead, use StringBuilder.append(char) to concatenate them into a String.



            If performance is not a concern, you could even do:



            char c = 'A';
            String s = "" + c + c;


            and s += "" + c + c;



            That will force the + String concatenation operator because it starts with a String (""). The Java Spec above explains with examples:




            The + operator is syntactically left-associative, no matter whether it
            is determined by type analysis to represent string concatenation or
            numeric addition. In some cases care is required to get the desired
            result. For example [...]



            1 + 2 + " fiddlers" is "3 fiddlers"



            but the result of:



            "fiddlers " + 1 + 2 is "fiddlers 12"








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago

























            answered 8 hours ago









            EdwardEdward

            414 bronze badges




            414 bronze badges























                1














                You are adding the value of two characters together. Change the String concatenation from:



                s +=  str.charAt(i) + str.charAt(i);


                To:



                s +=  str.charAt(i) + "" + str.charAt(i);


                Which will ensure the characters convert to a String.



                Note: This is a quick fix, and you should use StringBuilder when String concatenating inside of a loop. See the other answers for how this is done.






                share|improve this answer






























                  1














                  You are adding the value of two characters together. Change the String concatenation from:



                  s +=  str.charAt(i) + str.charAt(i);


                  To:



                  s +=  str.charAt(i) + "" + str.charAt(i);


                  Which will ensure the characters convert to a String.



                  Note: This is a quick fix, and you should use StringBuilder when String concatenating inside of a loop. See the other answers for how this is done.






                  share|improve this answer




























                    1












                    1








                    1







                    You are adding the value of two characters together. Change the String concatenation from:



                    s +=  str.charAt(i) + str.charAt(i);


                    To:



                    s +=  str.charAt(i) + "" + str.charAt(i);


                    Which will ensure the characters convert to a String.



                    Note: This is a quick fix, and you should use StringBuilder when String concatenating inside of a loop. See the other answers for how this is done.






                    share|improve this answer















                    You are adding the value of two characters together. Change the String concatenation from:



                    s +=  str.charAt(i) + str.charAt(i);


                    To:



                    s +=  str.charAt(i) + "" + str.charAt(i);


                    Which will ensure the characters convert to a String.



                    Note: This is a quick fix, and you should use StringBuilder when String concatenating inside of a loop. See the other answers for how this is done.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 8 hours ago

























                    answered 8 hours ago









                    NexevisNexevis

                    6801 silver badge11 bronze badges




                    6801 silver badge11 bronze badges























                        1














                        String.charAt() returns a char (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) so you're dealing with a single character. With onechar, you can perform operations on it like this (which will print "65" which is ASCII value for the 'A' character):



                        System.out.println('A' + 0);


                        you can print the ASCII value for the next character ("B") by adding 1 to 'A', like this:



                        System.out.println('A' + 1);


                        To make your code work – so that it doubles each character – there are number of options. You could append each character one at a time:



                        s += str.charAt(i);
                        s += str.charAt(i);


                        or various ways of casting the operation to a string:



                        s += "" + str.charAt(i) + str.charAt(i);





                        share|improve this answer






























                          1














                          String.charAt() returns a char (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) so you're dealing with a single character. With onechar, you can perform operations on it like this (which will print "65" which is ASCII value for the 'A' character):



                          System.out.println('A' + 0);


                          you can print the ASCII value for the next character ("B") by adding 1 to 'A', like this:



                          System.out.println('A' + 1);


                          To make your code work – so that it doubles each character – there are number of options. You could append each character one at a time:



                          s += str.charAt(i);
                          s += str.charAt(i);


                          or various ways of casting the operation to a string:



                          s += "" + str.charAt(i) + str.charAt(i);





                          share|improve this answer




























                            1












                            1








                            1







                            String.charAt() returns a char (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) so you're dealing with a single character. With onechar, you can perform operations on it like this (which will print "65" which is ASCII value for the 'A' character):



                            System.out.println('A' + 0);


                            you can print the ASCII value for the next character ("B") by adding 1 to 'A', like this:



                            System.out.println('A' + 1);


                            To make your code work – so that it doubles each character – there are number of options. You could append each character one at a time:



                            s += str.charAt(i);
                            s += str.charAt(i);


                            or various ways of casting the operation to a string:



                            s += "" + str.charAt(i) + str.charAt(i);





                            share|improve this answer















                            String.charAt() returns a char (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#charAt(int)) so you're dealing with a single character. With onechar, you can perform operations on it like this (which will print "65" which is ASCII value for the 'A' character):



                            System.out.println('A' + 0);


                            you can print the ASCII value for the next character ("B") by adding 1 to 'A', like this:



                            System.out.println('A' + 1);


                            To make your code work – so that it doubles each character – there are number of options. You could append each character one at a time:



                            s += str.charAt(i);
                            s += str.charAt(i);


                            or various ways of casting the operation to a string:



                            s += "" + str.charAt(i) + str.charAt(i);






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 6 hours ago

























                            answered 8 hours ago









                            kaankaan

                            344 bronze badges




                            344 bronze badges






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56977717%2fwhy-does-it-output-integers-instead-of-letters%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m