Hello, I have a current assignment to make a program copy what is put in, and place it into a paragraph using word wrap. If there is no room for the word+ a space then print the line and that word+ space starts the next line.

public class ReportWriter {
	private String author;
	private int noColumns;
	private int noRows;
	
	public ReportWriter(int noColumns, int noRows, String author) {
		super();
		this.author = author;
		this.noColumns = noColumns;
		this.noRows = noRows;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}
	
	public void printParagraph(String msg){
		String line = "";
		
		while (msg.length() > 0) {
			int spaceInx = msg.indexOf(' ');
			if (spaceInx < 0) {
				System.out.println(msg);
				msg = " ";
			} else if (spaceInx == 0) {
				msg = msg.substring(1);
			} else {
				String word = msg.substring(0, spaceInx);
				msg = msg.substring(spaceInx + 1);
				// check if there is space on the for the word and a space
				
				if (line.length() > noColumns){
				
					
				}

					
				
				     // no room print the line and set line to "";
				
				if (line.length() >0) {
					line += " ";
				}
				
				line += word;
				
				
					
					
				
			}
			
		}
		System.out.print(line);
		
		// print remaining line
	}

This is the test class. There is more to it but it should wrap 50 chars in.

public class Test {

	
	public static void main(String[] args) {
		ReportWriter reportWriter = new ReportWriter(50, 20, "Smith");
		reportWriter.printParagraph("Java was developed by Sun Microsystems in 1995 (Sun 1995).  It is an object oriented language that was derived from C and C++. ");

	}
}

Edited 5 Years Ago by Snoozey: n/a

This article has been dead for over six months. Start a new discussion instead.