0

I am looking for a way to print an array of integers and insert a line break with a "\" at the end of the line, if the line exceeds 68 elements.

for (int i = 0; i<=n.length; i++) {
         System.out.print(n[i]);
     }

is used to print each element of the array, but how to I insert the line break and backslash after x (in this case 68) elements?

It should look like this:

146346...8\
37453

5
  • I added some code @Reimus. Commented Dec 4, 2018 at 21:49
  • 1
    when you say "elements" do you mean the length of the line is 68 or the actual number of items printed is 68 before the "/" Commented Dec 4, 2018 at 21:51
  • @LoganMurphy yes, I meant the length of the line. sorry for the unclear expression! Commented Dec 4, 2018 at 21:52
  • should the final line end with "/" regardless of size? Commented Dec 4, 2018 at 22:07
  • @Cauchy are you expecting all elements from the array to be grouped together before printing them out? Commented Dec 4, 2018 at 22:09

5 Answers 5

1

If you're using Java 8+, the Stream API makes for a very fluent alternative to the for loop construct:

int[] n = ...;
final int limit = 68;

System.out.println(""); // start on a new line

Arrays.stream(n)
      .mapToObj(i -> ((Integer) i).toString()) // convert ints to strings
      .forEach(s -> {
          int i = 0;
          while(i < s.length) {
              System.out.print(s.substring(i, i + limit));
              System.out.println(i + limit < s.length ? "\\" : "");
              i += limit;
          }
      });

Alternatively, you could do something similar with a for loop if you're on an older Java version:

int[] n = ...;
final int limit = 68;

System.out.println(""); // start on a new line

for (int i = 0; i < n.length; i++) {
    String s = Integer.toString(n[i]);
    int i = 0;

    while(i < s.length) {
        System.out.print(s.substring(i, i + limit));
        System.out.println(i + limit < s.length ? "\\" : "");
        i += limit;
    }
}

Either way, you just need to first convert the integer to a string, and then print only the substring up to 68 characters in length at a time. System.out.print prints output without adding a new line at the end.

Sign up to request clarification or add additional context in comments.

Comments

0

this will work for you

for (int i = 0; i< n.length; i++) {
     System.out.print(n[i]);
     // every 68 iterations prints a \ and new line
     if((i + 1) % 68 == 0) System.out.println("\\");
}

1 Comment

This code will throw an `ArrayIndexOutOfBoundsException on the last iteration. Make sure you fix this.
0

It might be easier to do in two loops

    String string = "";
    int size = 68;
    for(int i = 0; i < n.length; i++) {
        string += n[i];
    }
    for(int i = 0; i < string.length() / size + 1; i++) {
        System.out.print(string.substring(i * size, Math.min((i + 1) * size, string.length())));
        System.out.println("\\");
    }

Make sure you use i < n.length as i <= n.length could produce an error in your loop

Link to code

Comments

0

I solved it with the help of @elbraulio.

for (int i = 0; i<n.length; ++i)
        n[i]=i;
    for (int i = 0; i<n.length; ++i) {
         System.out.print(n[i]);
         if (i%68 == 0 && i !=0) System.out.println("\\");
     }

Thanks everyone!

2 Comments

this solution wont work for numbers of length greater than 1, also you should give elbraulio credit if possible, dont just add another answer
@Cauchy did you tried with my updated answer with i + 1 % 68? It is shorter.
0

This should help

int[] intArray = { 7, 9, 5, 1, 3,... };       
for(int i = 0; i<intArray.length; ++i) 
{
// print elements
 System.out.print(intArray[i]);
// if there is 68 element printed, then print \
 if(i % 68 == 0)
  System.out.print("\ \r\n");
}

2 Comments

This code doesn't compile. Make sure it's compilable.
make your if a boolean statement like this if(i % 68 == 0) also escape your ` by using two `

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.