0

i am looking to make a string of this, so it will let me print formatArray() in a print sentence. But im not quiet sure how to make a string of this, and stop it from printing the information on its own. this is the sentence i want to use; system.out.println("your numbers are"+formatArray())


here is my code.

 public static void formatArray(int[] tallrekke){
     for(int i=0; i<tallrekke.length; i++){
         if(i>0 && i < tallrekke.length -1) {
             System.out.print(", ");
         }
         else if (i> tallrekke.length -2) {
             System.out.print(" og ");
         }
         System.out.print(tallrekke[i]);
    }
}
1
  • Could you add an example array with values, and your expected output? Commented Oct 11, 2019 at 14:02

6 Answers 6

3

Make your method return a String:

public static String formatArray(int[] tallrekke){
    StringBuilder sb = new StringBuilder();
    for(int i=0; i<tallrekke.length; i++){
        if(i>0 && i < tallrekke.length -1) {
            sb.append(", ");
        }
        else if (i> tallrekke.length -2) {
            sb.append(" og ");
        }
        sb.append(tallrekke[i]);
   }
    return sb.toString();
   }

public static void main( String[] args) {

    int[] tab = {1,2,3};
    System.out.println( formatArray( tab ) );

}

Output:

1, 2 og 3
Sign up to request clarification or add additional context in comments.

Comments

3
public static String formatArray(int[] tallrekke) {
    String s = "";

    for (int i = 0; i < tallrekke.length; i++) {
        if (i > 0 && i < tallrekke.length - 1) {
            s += ", ";
        } else if (i > tallrekke.length - 2) {
            s += " og ";
        }
            s += tallrekke[i];
        }

    return s;

}

It will bring all your print-statements. Hovever, I would prefer to use StringBuilder instead of String. That means that your code will look the following

public static String formatArray(int[] tallrekke) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < tallrekke.length; i++) {
        if (i > 0 && i < tallrekke.length - 1) {
            sb.append(", ");
        } else if (i > tallrekke.length - 2) {
            sb.append(" og ");
        }
            sb.append(tallrekke[i]);
        }

    return sb.toString();

}

1 Comment

Minor note, he said he wants to be able to call it in a System.out.println(formatArrat(arr)), so he most likely wants it to return String.
2

first of all you need to set the return type of your method to String instead of void. Then you should use StringBuilder to create a string.

public static String formatArray(int[] tallrekke) {
    StringBuilder stringBuilder = new StringBuilder();
    for(int tall : tallrekke){
        stringBuilder.append(tall);
    }
    return stringBuilder.toString();
}

Comments

2

Despite your expected output is not clear, this is what you need:

public static String formatArray(int[] tallrekke){
    StringBuilder stringBuilder = new StringBuilder();
    for(int i=0; i<tallrekke.length; i++){
        if(i>0 && i < tallrekke.length -1) {
            stringBuilder.append(", ");
        }
        else if (i> tallrekke.length -2) {
            stringBuilder.append(" og ");
        }
        stringBuilder.append(tallrekke[i]);
    }
    return stringBuilder.toString();
}

And you should call this method passing by the parameter:

int[] tallrekke = {0,1,2};
System.out.println("your numbers are"+formatArray(tallrekke));

Comments

1

Similar to yours solution

public class Main {
    public static void main (String[]args) throws IOException, ParseException {
        int[] nums = {1,2,3,4,5};
        System.out.println("your numbers are "+formatArray(nums));
    }

    public static String formatArray(int[] tallrekke){
        String result = "";
        for(int i=0; i<tallrekke.length; i++){
            if(i>0 && i < tallrekke.length -1) {
                result +=  ", ";
            }
            else if (i> tallrekke.length -2) {
                result += " og ";
            }
            result +=tallrekke[i];
        }
        return result;
    }
}

output is

your numbers are 1, 2, 3, 4 og 5

Compact formatArray method:

 public static String formatArray(int[] tallrekke){
        String result = "";
        for (int i = 0; ; i++) {
            result +=tallrekke[i];
            if (i == tallrekke.length-1)
                return result;
            result+= i==tallrekke.length-2?" og ":", ";
        }
    }

Your formatArray method should return String. The String is concatenated inside the forLoop.

Comments

0

maybe instead of System.out.print use 'return' then you can print whatever function returns

public String formatArray(int[] tallrekke){
    for(int i=0; i<tallrekke.length; i++){
        if(i>0 && i < tallrekke.length -1) {
            return ", ";
        }
        else if (i> tallrekke.length -2) {
            return  " og ";
        }
        return  String.valueOf(tallrekke[i]);
    }
}

1 Comment

I think you misunderstand the question, he wants to add the Strings altogether, not just return a small piece of it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.