Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this problem, you will create a program that can read in a series of files wh

ID: 3820404 • Letter: I

Question

In this problem, you will create a program that can read in a series of files which are passed in as command line arguments. Your program will read through these files and generate listings with line numbers. While generating the listings your program will check for the correctness of the indentation used in the program using a "stack" algorithm. When improper indentation is found, your program will stop processing this file and move onto the next file.

Here are the rules for proper indentation:

completely blank lines are ignored.

the only thing we are looking at is the column number of the first non-blank character. We don't care about the actual text on the line or what the first character is ... it can be: if, {, for, etc. and it just doesn't matter.

When a line of text is indented, we don't care how many spaces it is indented ... i.e. 1, 3, 4, 8, etc. is just fine

When a line of text is indented less than the previous line, the column number must match the column number of a previous line which is still "in play". The following text will attempt to clarify the rules:

The files that I want you to test your code with are found in the following links:

properlyIndented.txt - This file should pass

properlyIndented2.txt- This file should pass

notIndentedProperly.txt - This file should fail

notIndentedProperly2.txt- This file should fail

notIndentedProperly3.txt- This file should fail

So when you run your program, you will be using the command line arguments of:

properlyIndented.txt    properlyIndented2.txt     notIndentedProperly.txt     notIndentedProperly2.txt    notIndentedProperly3.txt

Here is the output of my published answer for this problem with the command line arguments of:

properlyIndented2.txt     notIndentedProperly.txt     notIndentedProperly2.txt

Explanation / Answer

// main.java
public class main {
public static void main(String[] args) {
for(String str : args)
new IndentChecker(str).readFile();
}

}

==============================================================================

//IndentChecker.java

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;

public class IndentChecker {
private BufferedReader reader;
private Stack<Integer> indentStack = new Stack<Integer>();
private String fileName;

public IndentChecker(String fileName) {
this.fileName = fileName;
try {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
this.reader = reader;
} catch (IOException e) {
System.out.println("File not found");
}
}


public void readFile() {
int count = 1;
try {
String line;
while ((line = this.reader.readLine()) != null) {
System.out.println(count + ": " + line);
processLine(line,count);
count++;
}
System.out.println("***************"+this.fileName + " is indented properly.");
} catch (Exception e) {
System.out.println("Error - " + e);
}
}

private void processLine(String line, int lineNumber){
int index = findFirstNonBlankLine(line);

if (index == -1)
return;
if (this.indentStack.size() == 0) {
this.indentStack.push(index);
return;
}
if (index > this.indentStack.peek()) {
this.indentStack.push(index);
return;
}
while(this.indentStack.peek() > index)
this.indentStack.pop();
if(this.indentStack.peek() != index){
throw new BadIndentationException("Bad indintation at line: "+String.valueOf(lineNumber));
}

}

private int findFirstNonBlankLine(String line) {
int index = 0;
for (char a : line.toCharArray()) {
if (a != ' ')
return index;
index++;
}
return -1;
}


}

===============================================================================

//BadIndentationException.java
public class BadIndentationException extends RuntimeException {
BadIndentationException(String error){
super(error);
}
}

==============================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote