* 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
Roller Coaster Kingdom Level HackRoller Coaster Kingdom level hack for Facebook. Get to level 25 in no time using a simple cheat.
String Wall Designs for Your HomeIf you're looking for a new way to punch up those old walls, decorate them with string! I'll tell you how....
The Importance of Being the Alpha DogSince dogs are pack animals, they expect to have a leader, an alpha dog. If you are not the alpha dog, then your dog will take over the role and you will spend your life cater...- Teen Clothing Stores in Columbus, OhioTeen clothing store in Columbus, Ohio are a great source for age appropriate clothing for your teenager. Teen clothing stores in Columbus, Ohio offer a selection of the latest trends in clothing for teenagers.
- Top Three Vintage Clothing Stores in Phoenix, ArizonaVintage clothing stores in Phoenix, Arizona are a great source for inexpensive clothing. Vintage clothing stores in Phoenix, Arizona are loaded with one of a kind long ignored clothing pieces that are sure to add pizz...
- Your First Java Program
- How to Read from an Input File in Java
- A Guide to Start Programming in Java
- The String Quartet Tribute to New Order and Joy Division
- Teen Clothing Stores in San Jose, California
- String Art, Decor and Craft Projects
- Computer High Level Language



