In this lab we look at the solution to the TabStop
labtest problem.
The problem statement is here.
It is often useful to sketch out a solution using plain English before attempting to write code. To make it easier to translate your solution to Java you can use Java-like structures such as loops in your sketch, but generally you leave out the details.
The first step of the solution is to read in the line of tab stops:
prompt and read line of tab stops
You might find it useful to count the number of tab stops; it's not necessary, but it makes it a little easier to write a solution:
prompt and read line of tab stops
count the number of tab stops
No input validation is required so you can move on to reading the lines of text. The problem says that your program should read in lines of text until the user enters Ctrl-D. This requires a loop that accepts input until there is nothing left:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
Given a line of text, you can process the line to make it tab aligned. Of course, you will have to store the processed line somewhere.
Processing a line of text requires you to find every adjacent pair of tab stops (the first and second, the second and third, the third and fourth, and so on). The first tab stop in a pair tells you the location to start printing a word, and the second tab stop tells you the location one space after the end of the word.
Because you have to find every pair of tab stops, you need another loop:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
Once you have the first and second tab stop indexes, you can format a word from the line of text:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
The innermost loop processes one line of text. The output should have a newline after every line of text, so you need to add a newline character to result:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
At the end of the outermost loop your program should ask the user for the next line of input:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
Finally, your program should output the result:
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
import java.io.PrintStream; import java.util.Scanner; import java.util.StringTokenizer; public class TabStop { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Enter tab mark line:"); String tabMarks = input.nextLine(); final char TAB = 'x'; int numTabs = 0; for (int i = 0; i < tabMarks.length(); i++) { if (tabMarks.charAt(i) == TAB) { numTabs++; } } String result = ""; // To accumulate the tab-aligned output rows. output.println("Enter row:"); while (input.hasNext()) { } } }
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
import java.io.PrintStream; import java.util.Scanner; import java.util.StringTokenizer; public class TabStop { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Enter tab mark line:"); String tabMarks = input.nextLine(); final char TAB = 'x'; int numTabs = 0; for (int i = 0; i < tabMarks.length(); i++) { if (tabMarks.charAt(i) == TAB) { numTabs++; } } String result = ""; // To accumulate the tab-aligned output rows. output.println("Enter row:"); while (input.hasNext()) { String line = input.nextLine(); int first = 0; // index in tabStops of first tab in pair int second = 0; // index in tabStops of second tab in pair for (int i = 0; i < numTabs - 1; i++) { } } } }
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
import java.io.PrintStream; import java.util.Scanner; import java.util.StringTokenizer; public class TabStop { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Enter tab mark line:"); String tabMarks = input.nextLine(); final char TAB = 'x'; int numTabs = 0; for (int i = 0; i < tabMarks.length(); i++) { if (tabMarks.charAt(i) == TAB) { numTabs++; } } String result = ""; // To accumulate the tab-aligned output rows. output.println("Enter row:"); while (input.hasNext()) { String line = input.nextLine(); StringTokenizer tokenizer = new StringTokenizer(line); int first = 0; // index in tabMarks of first tab in pair int second = 0; // index in tabMarks of second tab in pair for (int i = 0; i < numTabs - 1; i++) { // NOTE: first is already correct the first time through the loop. // The next time through the loop, first is the same as second // this time through the loop. second = tabMarks.indexOf(TAB, first + 1); first = second; } } } }
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
import java.io.PrintStream; import java.util.Scanner; import java.util.StringTokenizer; public class TabStop { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Enter tab mark line:"); String tabMarks = input.nextLine(); final char TAB = 'x'; int numTabs = 0; for (int i = 0; i < tabMarks.length(); i++) { if (tabMarks.charAt(i) == TAB) { numTabs++; } } String result = ""; // To accumulate the tab-aligned output rows. output.println("Enter row:"); while (input.hasNext()) { String line = input.nextLine(); StringTokenizer tokenizer = new StringTokenizer(line); int first = 0; // index in tabMarks of first tab in pair int second = 0; // index in tabMarks of second tab in pair for (int i = 0; i < numTabs - 1; i++) { second = tabMarks.indexOf(TAB, first + 1); rows += String.format("%-" + (second - first) + "." + (second - first) + "s", tokenizer.nextToken()); first = second; } } } }
prompt and read line of tab stops
count the number of tab stops
while there is still input
read a line of text
for each pair of tab stops
find the index of the first tab stop in the pair
find the index of the second tab stop in the pair
format a word from the line of text and append it to the result
append a newline to the result
prompt for next line of text
output tab stops
output result
import java.io.PrintStream; import java.util.Scanner; import java.util.StringTokenizer; public class TabStop { public static void main(String[] args) { PrintStream output = System.out; Scanner input = new Scanner(System.in); output.println("Enter tab mark line:"); String tabMarks = input.nextLine(); final char TAB = 'x'; int numTabs = 0; for (int i = 0; i < tabMarks.length(); i++) { if (tabMarks.charAt(i) == TAB) { numTabs++; } } String result = ""; // To accumulate the tab-aligned output rows. output.println("Enter row:"); while (input.hasNext()) { String line = input.nextLine(); StringTokenizer tokenizer = new StringTokenizer(line); int first = 0; // index in tabMarks of first tab in pair int second = 0; // index in tabMarks of second tab in pair for (int i = 0; i < numTabs - 1; i++) { second = tabMarks.indexOf(TAB, first + 1); result += String.format("%-" + (second - first) + "." + (second - first) + "s", tokenizer.nextToken()); first = second; } result += "\n"; output.println("Enter row:"); } output.println(tabMarks); output.print(result); } }