Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing a file as the first parameter, and an int width specifying the output text width. Your method writes the text file contents to the console in full justification form (I'm sure you've seen this in Microsoft Word full.docx ). For example, if a Scanner is reading an input file containing the following text:
Four score and
seven years ago our
fathers brought forth
on this continent
a new
nation.
Then a call to justifyText(input,30); will output the following to the console:
Four score and seven years ago
our fathers brought forth on
this continent a new nation.
Hint: Start by removing all the extra space from the text (a similar Exercise in text). Then calculate the number of spaces you need to add back in, to achieve the desired width, then System.out the text one line at a time. You may assume that we need to add only one or two spaces between words, and do not hyphenate words as other fancy programs might do. Keep it simple (well it's not that simple). And be certain that what you submit passes the Java compiler and does produce some output!!! A few points is infinitely better than a zero.....
To run above example, I had the following file in my Eclipse project to read these data from the text: spaces.txt
Here's my test code:
/*
public class Chapter6 {
public static void main(String[] args) throws
FileNotFoundException {
File spaces = new
File("spaces.txt");
Scanner input = new
Scanner(spaces);
justifyText(input ,30);
}
// Additional methods added below:
}
Code:
import java.io.File;
import java.util.Scanner;
//class named CHAPTER6
public class chapter6
{
//method main
public static void main(String[] args)
{
//try block
try
{
//opening the file spaces.txt
File spaces= new File("spaces.txt");
Scanner input = new Scanner(spaces);
//passing value to the justifyText method
justifyText(input, 30);
}
//catch block
catch (Exception e)
{
}
}
//method for defining text justification
public static void justifyText(Scanner inputval, int
widthVal)
{
//try block
try
{
//catch block
String totalStringVal = "";
while (inputval.hasNextLine())
{
//total string getting
totalStringVal += inputval.nextLine();
}
//trim function is used for replacing all
totalStringVal = totalStringVal.replaceAll("\\s+", "
").trim();
printStringValue(totalStringVal, 30); inputval.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
//method to print the string in the sequence
public static void printStringValue(String inStrCal, int
widthof)
{
//initialising minimum
int minimum = 0;
while (true)
{
//checking the string length
if (inStrCal.length() <= widthof)
{
widthof = inStrCal.length();
}
//string temporarly manipulating the width
String tempString = inStrCal.substring(minimum
widthof);
int lastIndexVal = tempString.lastIndexOf(" ");
//checking the final index
if (lastIndexVal == 0)
{
System.out.println(inStrCal.trim());
break;
}
//checking the string line
String stringLine = inStrCal.substring(minimum
lastIndexVal);
inStrCal = inStrCal.substring(lastIndexVal
inStrCal.length());
System.out.println(stringLine.trim());
}
}
}
Submit Chapter6.java with a public static method named justifyText that accepts two parameters; a Scanner representing...
JAVA Overview Create a method public static void spongeBobify(String inputPath, String outputPath, Mode mode) that will convert a file to Mocking Sponge Bob text in 3 different modes. EveryOther capitalize the first letter of the string capitalize every other letter (ignoring non-letter character like punctuation and spaces). non-letter characters don't count toward the every other count Example: mocking sponge bob! → MoCkInG sPoNgE bOb! Vowels capitalize every vowel (not including y) Random capitalize each letter with a probability of 35%....
The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...
import java.util.Scanner; public class Lab6d { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); // TODO: get user choice // TODO: call printTable method passing choice as the parameter } public static void printTable(int stop) { // TODO: print header // TODO: loop to print table rows up to stop value } Write a Java program where the main () method prompts the user to select an integer value between 1 and...
Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...
(TCO 1) The JVM executes a Java application by (Points : 6) executing the public static main method of the class. creating an object of the class and executing that object’s main method. executing the class constructor. compiling the Java code into byte code. (TCO 1) Which method call converts the value in variable stringVariable to a double? (Points : 6) Double.parseDouble( stringVariable ); Convert.toDouble( stringVariable ); Convert.parseDouble( stringVariable ); Float.parseFloat( stringVariable ); (TCO 1) Which of the following can...
Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...
4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...
Help in Java 01 Modify the following cat method so that it will compile public static void catFile file) t Random.AccessFile input null; String line nul; try input-new RandomAcce ssFile (file, "r) while (line inputreadLineO)!- null)f tle. f System.out.prindn(line); retum; finally if (input!null) t input.close;
Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...
Overview: Pattern-Matching (aka String Search) is the process of algorithmically finding copies of a pattern P inside a (generally much larger) text T. The goal is to implement and compare four classical string-matching algorithms. Input: Your code should work for any text either inputted directly or read in from a file. However, for testing - input file has been provided: The Gettysburg Address (by President Abraham Lincoln, 1863) You should minimally search for these three patterns in each text: FREE,...