How do I add chars from array of chars to a string in Java












0















I searched the site and found the partially useful StringBuilder method in an answer to another question, but it doesn't seem to want to take the char at index x and y of a 2d char array as a parameter.



Can you suggest a better and more simple method to do this? I say simple because there is a chance something like this will come up in the final, and I would like to be able to remember it. If it's "exotic" to me as a beginning programmer, I probably won't.



I took a fundamentals of programming course in c++, and if memory servers, all I had to do was str+=arrChars[x][y]; That doesn't work for Java, though.



Here is the present state of my toString() method:



public String toString()    {

StringBuilder gardenPrint= new StringBuilder(" | 0 1 2 3n");
int i, j;
for(i=0;i<garden.length;i++)
gardenPrint.append(i+ " | ");
for(j=0;j<garden.length;j++)
gardenPrint.append(garden[i][j]+" "+(j==garden.length-1) ? "n":"");
return gardenPrint;
}









share|improve this question

























  • return Arrays.deepToString(garden);

    – Elliott Frisch
    Nov 24 '18 at 4:22











  • I need to put the indices on the axes, too.

    – mtlchk
    Nov 24 '18 at 4:24











  • I wouldn't do that with the toString() myself.

    – Elliott Frisch
    Nov 24 '18 at 4:26











  • It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

    – mtlchk
    Nov 24 '18 at 4:30
















0















I searched the site and found the partially useful StringBuilder method in an answer to another question, but it doesn't seem to want to take the char at index x and y of a 2d char array as a parameter.



Can you suggest a better and more simple method to do this? I say simple because there is a chance something like this will come up in the final, and I would like to be able to remember it. If it's "exotic" to me as a beginning programmer, I probably won't.



I took a fundamentals of programming course in c++, and if memory servers, all I had to do was str+=arrChars[x][y]; That doesn't work for Java, though.



Here is the present state of my toString() method:



public String toString()    {

StringBuilder gardenPrint= new StringBuilder(" | 0 1 2 3n");
int i, j;
for(i=0;i<garden.length;i++)
gardenPrint.append(i+ " | ");
for(j=0;j<garden.length;j++)
gardenPrint.append(garden[i][j]+" "+(j==garden.length-1) ? "n":"");
return gardenPrint;
}









share|improve this question

























  • return Arrays.deepToString(garden);

    – Elliott Frisch
    Nov 24 '18 at 4:22











  • I need to put the indices on the axes, too.

    – mtlchk
    Nov 24 '18 at 4:24











  • I wouldn't do that with the toString() myself.

    – Elliott Frisch
    Nov 24 '18 at 4:26











  • It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

    – mtlchk
    Nov 24 '18 at 4:30














0












0








0








I searched the site and found the partially useful StringBuilder method in an answer to another question, but it doesn't seem to want to take the char at index x and y of a 2d char array as a parameter.



Can you suggest a better and more simple method to do this? I say simple because there is a chance something like this will come up in the final, and I would like to be able to remember it. If it's "exotic" to me as a beginning programmer, I probably won't.



I took a fundamentals of programming course in c++, and if memory servers, all I had to do was str+=arrChars[x][y]; That doesn't work for Java, though.



Here is the present state of my toString() method:



public String toString()    {

StringBuilder gardenPrint= new StringBuilder(" | 0 1 2 3n");
int i, j;
for(i=0;i<garden.length;i++)
gardenPrint.append(i+ " | ");
for(j=0;j<garden.length;j++)
gardenPrint.append(garden[i][j]+" "+(j==garden.length-1) ? "n":"");
return gardenPrint;
}









share|improve this question
















I searched the site and found the partially useful StringBuilder method in an answer to another question, but it doesn't seem to want to take the char at index x and y of a 2d char array as a parameter.



Can you suggest a better and more simple method to do this? I say simple because there is a chance something like this will come up in the final, and I would like to be able to remember it. If it's "exotic" to me as a beginning programmer, I probably won't.



I took a fundamentals of programming course in c++, and if memory servers, all I had to do was str+=arrChars[x][y]; That doesn't work for Java, though.



Here is the present state of my toString() method:



public String toString()    {

StringBuilder gardenPrint= new StringBuilder(" | 0 1 2 3n");
int i, j;
for(i=0;i<garden.length;i++)
gardenPrint.append(i+ " | ");
for(j=0;j<garden.length;j++)
gardenPrint.append(garden[i][j]+" "+(j==garden.length-1) ? "n":"");
return gardenPrint;
}






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 4:23







mtlchk

















asked Nov 24 '18 at 4:14









mtlchkmtlchk

72




72













  • return Arrays.deepToString(garden);

    – Elliott Frisch
    Nov 24 '18 at 4:22











  • I need to put the indices on the axes, too.

    – mtlchk
    Nov 24 '18 at 4:24











  • I wouldn't do that with the toString() myself.

    – Elliott Frisch
    Nov 24 '18 at 4:26











  • It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

    – mtlchk
    Nov 24 '18 at 4:30



















  • return Arrays.deepToString(garden);

    – Elliott Frisch
    Nov 24 '18 at 4:22











  • I need to put the indices on the axes, too.

    – mtlchk
    Nov 24 '18 at 4:24











  • I wouldn't do that with the toString() myself.

    – Elliott Frisch
    Nov 24 '18 at 4:26











  • It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

    – mtlchk
    Nov 24 '18 at 4:30

















return Arrays.deepToString(garden);

– Elliott Frisch
Nov 24 '18 at 4:22





return Arrays.deepToString(garden);

– Elliott Frisch
Nov 24 '18 at 4:22













I need to put the indices on the axes, too.

– mtlchk
Nov 24 '18 at 4:24





I need to put the indices on the axes, too.

– mtlchk
Nov 24 '18 at 4:24













I wouldn't do that with the toString() myself.

– Elliott Frisch
Nov 24 '18 at 4:26





I wouldn't do that with the toString() myself.

– Elliott Frisch
Nov 24 '18 at 4:26













It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

– mtlchk
Nov 24 '18 at 4:30





It's part of the assignment. The char array is the board representing a garden where you plant things and a rabbit eats your stuff. The methods are in the assignment spec, and we have to comply with it

– mtlchk
Nov 24 '18 at 4:30












2 Answers
2






active

oldest

votes


















1














Java (nor C++) loops are controlled by indentation. You need braces and your ternary needs parenthesis and you have to return a String. Like,



public String toString() {
StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3n");
int i, j;
for (i = 0; i < garden.length; i++) {
gardenPrint.append(i + " | ");
for (j = 0; j < garden.length; j++)
gardenPrint.append(garden[i][j] + " " + ((j == garden.length - 1) ? "n" : ""));
}
return gardenPrint.toString();
}


However, we can implement this in a number of ways. For example,



public String toString() {
StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3") //
.append(System.lineSeparator());
for (int i = 0; i < garden.length; i++) {
gardenPrint.append(i + " |");
for (char ch : garden[i]) {
gardenPrint.append(" ").append(ch);
}
gardenPrint.append(System.lineSeparator());
}
return gardenPrint.toString();
}





share|improve this answer
























  • Thanks much. My bad.

    – mtlchk
    Nov 24 '18 at 5:03



















0














In Java, you can use the following two ways to convert a char array to a String:-



public class CharArrayToString {

public static void main(String args) {

char chArr = new char{'1', '2', '3', 'A', 'B', 'C'};

String str1 = new String(chArr);
System.out.println("1st String: " + str1);

String str2;
str2 = String.valueOf(chArr);
System.out.println("2nd String: " + str2);

}

}


Output



1st String: 123ABC



2nd String: 123ABC






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%2f53455094%2fhow-do-i-add-chars-from-array-of-chars-to-a-string-in-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Java (nor C++) loops are controlled by indentation. You need braces and your ternary needs parenthesis and you have to return a String. Like,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3n");
    int i, j;
    for (i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " | ");
    for (j = 0; j < garden.length; j++)
    gardenPrint.append(garden[i][j] + " " + ((j == garden.length - 1) ? "n" : ""));
    }
    return gardenPrint.toString();
    }


    However, we can implement this in a number of ways. For example,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3") //
    .append(System.lineSeparator());
    for (int i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " |");
    for (char ch : garden[i]) {
    gardenPrint.append(" ").append(ch);
    }
    gardenPrint.append(System.lineSeparator());
    }
    return gardenPrint.toString();
    }





    share|improve this answer
























    • Thanks much. My bad.

      – mtlchk
      Nov 24 '18 at 5:03
















    1














    Java (nor C++) loops are controlled by indentation. You need braces and your ternary needs parenthesis and you have to return a String. Like,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3n");
    int i, j;
    for (i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " | ");
    for (j = 0; j < garden.length; j++)
    gardenPrint.append(garden[i][j] + " " + ((j == garden.length - 1) ? "n" : ""));
    }
    return gardenPrint.toString();
    }


    However, we can implement this in a number of ways. For example,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3") //
    .append(System.lineSeparator());
    for (int i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " |");
    for (char ch : garden[i]) {
    gardenPrint.append(" ").append(ch);
    }
    gardenPrint.append(System.lineSeparator());
    }
    return gardenPrint.toString();
    }





    share|improve this answer
























    • Thanks much. My bad.

      – mtlchk
      Nov 24 '18 at 5:03














    1












    1








    1







    Java (nor C++) loops are controlled by indentation. You need braces and your ternary needs parenthesis and you have to return a String. Like,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3n");
    int i, j;
    for (i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " | ");
    for (j = 0; j < garden.length; j++)
    gardenPrint.append(garden[i][j] + " " + ((j == garden.length - 1) ? "n" : ""));
    }
    return gardenPrint.toString();
    }


    However, we can implement this in a number of ways. For example,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3") //
    .append(System.lineSeparator());
    for (int i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " |");
    for (char ch : garden[i]) {
    gardenPrint.append(" ").append(ch);
    }
    gardenPrint.append(System.lineSeparator());
    }
    return gardenPrint.toString();
    }





    share|improve this answer













    Java (nor C++) loops are controlled by indentation. You need braces and your ternary needs parenthesis and you have to return a String. Like,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3n");
    int i, j;
    for (i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " | ");
    for (j = 0; j < garden.length; j++)
    gardenPrint.append(garden[i][j] + " " + ((j == garden.length - 1) ? "n" : ""));
    }
    return gardenPrint.toString();
    }


    However, we can implement this in a number of ways. For example,



    public String toString() {
    StringBuilder gardenPrint = new StringBuilder(" | 0 1 2 3") //
    .append(System.lineSeparator());
    for (int i = 0; i < garden.length; i++) {
    gardenPrint.append(i + " |");
    for (char ch : garden[i]) {
    gardenPrint.append(" ").append(ch);
    }
    gardenPrint.append(System.lineSeparator());
    }
    return gardenPrint.toString();
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 24 '18 at 4:41









    Elliott FrischElliott Frisch

    154k1393182




    154k1393182













    • Thanks much. My bad.

      – mtlchk
      Nov 24 '18 at 5:03



















    • Thanks much. My bad.

      – mtlchk
      Nov 24 '18 at 5:03

















    Thanks much. My bad.

    – mtlchk
    Nov 24 '18 at 5:03





    Thanks much. My bad.

    – mtlchk
    Nov 24 '18 at 5:03













    0














    In Java, you can use the following two ways to convert a char array to a String:-



    public class CharArrayToString {

    public static void main(String args) {

    char chArr = new char{'1', '2', '3', 'A', 'B', 'C'};

    String str1 = new String(chArr);
    System.out.println("1st String: " + str1);

    String str2;
    str2 = String.valueOf(chArr);
    System.out.println("2nd String: " + str2);

    }

    }


    Output



    1st String: 123ABC



    2nd String: 123ABC






    share|improve this answer






























      0














      In Java, you can use the following two ways to convert a char array to a String:-



      public class CharArrayToString {

      public static void main(String args) {

      char chArr = new char{'1', '2', '3', 'A', 'B', 'C'};

      String str1 = new String(chArr);
      System.out.println("1st String: " + str1);

      String str2;
      str2 = String.valueOf(chArr);
      System.out.println("2nd String: " + str2);

      }

      }


      Output



      1st String: 123ABC



      2nd String: 123ABC






      share|improve this answer




























        0












        0








        0







        In Java, you can use the following two ways to convert a char array to a String:-



        public class CharArrayToString {

        public static void main(String args) {

        char chArr = new char{'1', '2', '3', 'A', 'B', 'C'};

        String str1 = new String(chArr);
        System.out.println("1st String: " + str1);

        String str2;
        str2 = String.valueOf(chArr);
        System.out.println("2nd String: " + str2);

        }

        }


        Output



        1st String: 123ABC



        2nd String: 123ABC






        share|improve this answer















        In Java, you can use the following two ways to convert a char array to a String:-



        public class CharArrayToString {

        public static void main(String args) {

        char chArr = new char{'1', '2', '3', 'A', 'B', 'C'};

        String str1 = new String(chArr);
        System.out.println("1st String: " + str1);

        String str2;
        str2 = String.valueOf(chArr);
        System.out.println("2nd String: " + str2);

        }

        }


        Output



        1st String: 123ABC



        2nd String: 123ABC







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 4:26

























        answered Nov 24 '18 at 4:19









        AbhinavAbhinav

        380412




        380412






























            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%2f53455094%2fhow-do-i-add-chars-from-array-of-chars-to-a-string-in-java%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

            Wiesbaden

            Marschland

            Dieringhausen