olepea
olepea

Reputation: 21

make string in a class, java

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]);
    }
}

Upvotes: 0

Views: 116

Answers (6)

Ioannis Barakos
Ioannis Barakos

Reputation: 1369

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.

Upvotes: 1

alea
alea

Reputation: 1081

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();

}

Upvotes: 3

Villat
Villat

Reputation: 1475

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));

Upvotes: 2

Witold
Witold

Reputation: 108

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]);
    }
}

Upvotes: 0

StephaneM
StephaneM

Reputation: 4899

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

Upvotes: 3

Tarik Weiss
Tarik Weiss

Reputation: 345

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();
}

Upvotes: 2

Related Questions