Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to segment my current output of code into seperate lines of 8. What

ID: 3872686 • Letter: I

Question

I'm trying to segment my current output of code into seperate lines of 8. What can I add to my current code to make this happen? This is my final question for the month, so your help is greatly appreciated.

I've already recieved the answer "Either use System.out.println(F.next()); or System.out.print(F.next()+"ln");" but this won't work because I'm going to search for palindromes after I figure this out, and the list needs to be segmented by 8 until the end i reached. There are a lot more integers than what I'm showing, so the actual output is a lot longer.

My code is reading from a txt file with lines of integers, and each has a space between them. The data from the text file looks like the following:

My current output is one singular line that looks like this:

I'm looking for this:

Here is my code thus far that is generating the output (What can be added to it to segment the data into 8 for each line?):

0 001 000 0 1 10 1 001 1 1 0 1 1 0 1 0 0

Explanation / Answer

A simple solution is to get your complete string into a string variable(without newlines into it), then we can parse the strign into 8 characters at a time and pritnt it.

Use below function to print the string:


public void printIt(String s) {

if(s.length() > 8) {

// string has more than 8 characters, hence break it into 8 characters and print

System.out.println(s.substring(0, 8));

// then pass the remaining string to the print function again

printIt(s.substring(8));

} else {

// string is less than 8 characters, hence no need to break now

System.out.println(s);

}

}


========
You need to pass the complete string into this function.

Let me know in comments if you face some problem.