Alpha Nesting - Java Pt. 3

Chris Chen
/*
* Purpose: to create a class which will check for alphabetical order within a hierarchy of a file
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class AlphaNest {

private Scanner sc; // stores the scanner object
private Stack stck; // the stack which is used to store lines of the file

//constructor passing in the name of the file to be working with
public AlphaNest(String fName){

sc = null;

//try catch used to create a Scanner which read in from a file
try{

sc = new Scanner(new File(fName));

}catch(FileNotFoundException e){

System.out.println("File could not be found");
System.exit(-1);

}//end try catch block

stck = new Stack();

}//end constructor

//determines if the file is alphabetized according to its hierarchy
public void checkAlpha(){

String space = ""; // stores the number of proceeding spaces of a level in the form of a string
String line = ""; //to store each line of the file one at a time
String trimLine = ""; // to store each line of the file without its proceeding white space
int numSpace = 0, prevNumSpace = 0; // stores the number of spaces before a level in the form of an integer for both the current line and the line on the top of the stack

//traverses file
while(sc.hasNext()){

line = sc.nextLine(); //each line of the file one at a time

//checks if the line is empty
if(line.length() > 0){

space = level(line);
numSpace = space.length();
prevNumSpace = 0;

//checks if the stack is empty
if(!stck.isEmpty())
prevNumSpace = stck.peek().length();

trimLine = line.trim();

//traverse the stack until the top line is on the same level as the current line read in from the file
while(numSpace < prevNumSpace){

stck.pop();
stck.pop();

prevNumSpace = stck.peek().length();

}//end while

//checks if the current line from the file is on the same level as the last line from the file and that the stack is not empty
if(numSpace == prevNumSpace && !stck.isEmpty()){

stck.pop();
String oldWord = stck.pop();

//checks if the two lines are in alphabetical order
if(trimLine.compareTo(oldWord) < 0){

System.out.println(oldWord + "\n" + trimLine);
System.exit(-1);

}//end if

}//end if

stck.push(trimLine);
stck.push(space);

}//end if

}//end while

System.out.println("The File is properly Alpha - Nested.");

}//end checkAlpha

//helper method to return a string representation of the level which the line of the file lies
private String level(String line){

String toRet = ""; //stores string to be returned
int i = line.length() - line.trim().length(); //stores the different in length between the line and the line without white space

//checks if the string to be returned needs to be modified
if(i > 0)
//loops until the string to return is the correct length in spaces
for (int n = i ; n > 0 ; n--)
toRet += " ";

return toRet;

}//end level

}//end AlphaNest

Published by Chris Chen

Chris is currently attending the University of California, Berkeley seeking an undergraduate's degree in Electrical Engineering Computer Science. He enjoys playing basketball, practicing kendo, hanging out w...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.