[Java] How do I (when I read in a text file) wrap the words at a given line length, and keep track of longest and shortest lines? via /r/learnprogramming

[Java] How do I (when I read in a text file) wrap the words at a given line length, and keep track of longest and shortest lines?

I've commented on my issues throughout the program. Need to wrap the words at a given line length that the user inputs, and from that also keep track of the longest and shortest lines. If you run this on code, just create a simple text file with a few lines of code with varying lengths per line.

 import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ParseAndRewrap { public static final int ERROR_CODE = 1; public static void main(String[] args) { Scanner scan = new Scanner(System.in); //user enters the file name System.out.print("Please enter a plain text file name: "); String filename = scan.nextLine().trim(); //user enters max numbers of characters per line. System.out.println("Please enter the maximum number of characters in a single line: "); int maxLineSize = scan.nextInt(); scan.close(); //variables for longest/shortest lines. int longestLine = 0; int shortestLine = 999; //variables for concatenating tokens. String st = ""; String total = ""; // creating our file object File file = new File(filename); try { Scanner fileScan = new Scanner(file); while (fileScan.hasNextLine()) { // read one line String line = fileScan.nextLine(); // create an additional Scanner to break the current line into // individual tokens (words) Scanner lineScan = new Scanner(line); // finding longest and shortest lines // this is not working if (line.length() > longestLine) { longestLine = line.length(); if (line.length() < shortestLine) { shortestLine = line.length(); } } // this if statement will print out each line in the document //up until maxLineSize, but then do a carriage return and start //on the next line without finishing the first AND it's cutting off //words which is bad. Needs to add up that last word and if it's greater //than max it should do carriage return and print the rest on the next line. if (line.length() >= maxLineSize) { System.out.print(line.substring(0, maxLineSize) + "\n"); } // read each token from the line until we run out of tokens //I'm not sure how to deal with the tokens in regards to maxLineSize //and keeping track of largest and smallest tokens. while (lineScan.hasNext()) { // read the next token/word String token = lineScan.next(); //trying to combine the tokens and get their length //then use that length to compare to maxLineSize. //to do a carriage return if the next token is > maxLineSize total = st + token; int totals = total.length(); } // close line scanner. lineScan.close(); } // close the file scanner. fileScan.close(); System.out.println("\n\tLongest line: " + longestLine); System.out.println("\tShorest line: " + shortestLine); } catch (FileNotFoundException e) { // print a helpful message before exiting System.out.println("File \"" + filename + "\" could not be opened."); System.out.println(e.getMessage()); System.exit(ERROR_CODE); } } } 

Submitted July 09, 2017 at 01:29PM by TheCyberTechnician
via reddit http://ift.tt/2sDJ45C


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s