need to use nested loops to produce the indexes needed to make substrings. Strin
ID: 3726630 • Letter: N
Question
need to use nested loops to produce the indexes needed to make substrings. String is user input. Must build formatting string
to produce the indexes needed to generate the substrings you want. With those indexes, use the substring method to create the substring. 2. First try to produce the substrings on a specific row. (See the pattern of the indexes that give all the substrings of a specific length 3. Next, see how you can use an outer loop to make the above code (that produces one row) repeat. 4. In order to be able to p od ce this f matting, you must Bu D e fo matting tring For example for car instead of harkod ng string is given, it will build the formatting string with the correet string length. See what this code does you tad the length afst ng a d user ge eat tan Thai. y ul tal at String word "cool" int N:word. length); string foreatStr. " 1%-- + N + "s l". System.out-printf(fornatStr, "c"): System, out println): System.out.printf(foreatStr, "co); System.out.printin); System. out.printf(formatStr, "coo1'); System.out.printlnO; Sample run This progran will generate all the substrings of a given word, in increasing order of substring length Please enter a word or q to quit: cat I cat l Please enter a word or q to quit: elephant le ha I lep l eph l pha l ant I lepho 1 ephan phant l lephan I ephantI l elephan I lephant l 8 HWS-WevelCount CSE1310 Homew. Big Jawa Late Objec. O Type here to searchExplanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
// UnComment the lines if horizontal dotted lines required
public class StringFormatEx {
public static void main(String[] args) {
String str = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while(true){
System.out.println("Please enter a word or q to quit");
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(str.equalsIgnoreCase("q")){
break;
}
else{
displayFormat(str);
}
}
}
public static void displayFormat(String str){
int index=1;
int length = -1*str.length();
String format = "| %"+length+"s ";
//int words = str.length();
//printHorizontalLine(words, str.length());
System.out.println();
for(int i=0;i<str.length();i++){
for(int j=0;j<str.length();j++){
if(index+j<=str.length())
System.out.printf(format,str.substring(j,j+index));
}
System.out.println("|");
//words--;
//printHorizontalLine(words, str.length());
index++;
System.out.println();
}
//printHorizontalLine(1, str.length());
System.out.println();
}
public static void printHorizontalLine(int words,int length){
for(int i=0;i<(words*(length+3));i++){
System.out.print("-");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.