All pastes #274307 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-10 16:40 UTC
rendered paste body
import java.util.*;
public class Scanner {
	Disk in, out; Set64 board = new Set64(); 
	
	/**
	 * @param filename the filename of the input text file containing the scanner array 
	 */
	public Scanner(String filename){
		in = new Disk(filename, "in");
	}
	
	protected String newWord(String s){
		return s;
	}
	
	protected String lineHandler(String s){
		StringTokenizer toks = new StringTokenizer(s);
		String result = "";
		while (toks.hasMoreTokens()){
			result += newWord(toks.nextToken()) + " ";
		}
		return result;
	}
	
	/** 
	 * copies the scanner array to an output text file, unmodified
	 * @param filename the filename of the new text file to which the scanner array will be copied
	 */
	public void print(String filename){
		out = new Disk(filename, "out");
		String line = in.readLine();
		while(line != null){
			out.out.println(lineHandler(line));
			line =in.readLine();
		}
		out.out.close();
	}
	
	/**
	 * prints to the specified file an array with the same dimensions as the scanner array, preserving the elements of each maximal horizontal run in the scanner array. All other array elements are replaced by ‘-’ characters
	 * @param filename the filename of the new text file to which the scanner showing the horizontal runs will be printed
	 */
	
	public void maxHRun(String filename){
		String line = in.readLine();
		line.trim();
		while(line != null){
			if (line.length() > 0){
				String[] rowArray = line.split(" ");
				Integer [] columns = new Integer[rowArray.length];
				for(int i=0;i<rowArray.length;i++){
					//System.out.println("intro:" + line + ":end");
					columns[i] = Integer.decode(rowArray[i]);		
				}
			
			board.add(columns);
			}
			line = in.readLine();
		}
		
		board.reset();
		int currentLongestRun = 0;
		while(board.hasNext()){
			   Integer[] rowArr = (Integer[]) board.next();
			   for(int i=0;i<rowArr.length; i++){
				   int j = i+1;
				   for(;j<rowArr.length;j++){
					   if (rowArr[i].intValue() != rowArr[j].intValue()){
						   if ( (j - i) >= currentLongestRun ) {
							   currentLongestRun = j-i;
							   i=j-1;
						   }
						   break;
					   }
				   }
				   if (j == (rowArr.length)){
					   if ( (j - i) >= currentLongestRun ) {
						   currentLongestRun = j-i;
						   i = j-1;
					   }
				   }
			   }
		}
		board.reset();
		while(board.hasNext()){
			Integer[] rowArr = (Integer[]) board.next();
			   for(int i=0;i<rowArr.length; i++){
				   int j = i+1;
				   for(;j<rowArr.length;j++){
					   if (rowArr[i].intValue() != rowArr[j].intValue()){
						   if ( (j - i) == currentLongestRun ) {
							   i=j-1;
						   }
						   else{
							   rowArr[i] = new Integer(-1);
							   
						   }
						   break;
					   }
				   }
				   if (j == (rowArr.length)){
					   if ( (j - i) == currentLongestRun ) {
						   i = j-1;
					   }
					   else{
						   rowArr[i] = new Integer(-1);
					   }
				   }
			   }
		}
		printBoard(filename);
	}
	
	/**
	 * prints the longest horizontal runs to the console
	 *
	 */
	
	public void maxHRun(){
		String line = in.readLine();
		line.trim();
		while(line != null){
			if (line.length() > 0){
				String[] rowArray = line.split(" ");
				Integer [] columns = new Integer[rowArray.length];
				for(int i=0;i<rowArray.length;i++){
					//System.out.println("intro:" + line + ":end");
					columns[i] = Integer.decode(rowArray[i]);		
				}
			
			board.add(columns);
			}
			line = in.readLine();
		}
		
		board.reset();
		int currentLongestRun = 0;
		while(board.hasNext()){
			   Integer[] rowArr = (Integer[]) board.next();
			   for(int i=0;i<rowArr.length; i++){
				   int j = i+1;
				   for(;j<rowArr.length;j++){
					   if (rowArr[i].intValue() != rowArr[j].intValue()){
						   if ( (j - i) >= currentLongestRun ) {
							   currentLongestRun = j-i;
							   i=j-1;
						   }
						   break;
					   }
				   }
				   if (j == (rowArr.length)){
					   if ( (j - i) >= currentLongestRun ) {
						   currentLongestRun = j-i;
						   i = j-1;
					   }
				   }
			   }
		}
		board.reset();
		while(board.hasNext()){
			Integer[] rowArr = (Integer[]) board.next();
			   for(int i=0;i<rowArr.length; i++){
				   int j = i+1;
				   for(;j<rowArr.length;j++){
					   if (rowArr[i].intValue() != rowArr[j].intValue()){
						   if ( (j - i) == currentLongestRun ) {
							   i=j-1;
						   }
						   else{
							   rowArr[i] = new Integer(-1);
							   
						   }
						   break;
					   }
				   }
				   if (j == (rowArr.length)){
					   if ( (j - i) == currentLongestRun ) {
						   i = j-1;
					   }
					   else{
						   rowArr[i] = new Integer(-1);
					   }
				   }
			   }
		}
		printBoardConsole();
	}
	
	
	//public void maxVRun(String filename){
		//String line = in.readLine();
		//line.trim();
		//while(line != null){
			//if (line.length() > 0){
				//String[] rowArray = line.split(" ");
				//Integer [] columns = new Integer[rowArray.length];
				//for(int i=0;i<rowArray.length;i++){
					//System.out.println("intro:" + line + ":end");
					//columns[i] = Integer.decode(rowArray[i]);		
				//}
			
			//board.add(columns);
			//}
			//line = in.readLine();
		//}
		//Set64 vBoard = new Set64();
		//while(board.hasNext()){
			//Integer[] rowArr = (Integer[]) board.next();
			//vBoard.add(rowArr);
		//}
	//}
	
	
	
	public void printBoard(String filename){
		Disk out = new Disk(filename, "out");
		board.reset();
		while(board.hasNext()){
			Integer [] printIntArr = (Integer [])board.next();
			String line = new String("");
			for (int i=0;i<printIntArr.length;i++) {
				if(printIntArr[i].intValue()== -1){
					line+= ("-" + " ");
				}
				else{
					line+= (printIntArr[i].toString() + " ");
				}
			}
			out.out.println(line);
		}
		out.out.close();
	}
	public void printBoardConsole(){
		board.reset();
		while(board.hasNext()){
			Integer [] printIntArr = (Integer [])board.next();
			String line = new String("");
			for (int i=0;i<printIntArr.length;i++) {
				if(printIntArr[i].intValue()== -1){
					line+= ("-" + " ");
				}
				else{
					line+= (printIntArr[i].toString() + " ");
				}
			}
			System.out.println(line);
		}
	}
	
	public boolean shapeEquals(Scanner scan){
		if((this.rows() == scan.rows()) && (this.columns() == scan.columns())){
			return true;
		}
		else{
			return false;
		}
	}
	
	public int rows(){
		int counter=0;
		String line = in.readLine();
		line.trim();
		while(line !=null){
			if(line.length() > 0){
				counter++;
			}
			line = in.readLine();
		}
		System.out.println(counter);
		return counter;
	}

	public int columns(){
		String line = in.readLine();
		line.trim();
		String[] arr = line.split(" ");
		System.out.println(arr.length);
		return arr.length;
	}
	
	public boolean equals(Scanner scan){
		if(this.shapeEquals(scan) == false){
			System.out.println("false");
			return false;
		}
		else{
			String thisLine = in.readLine();
			thisLine.trim();
			String scanLine = scan.in.readLine();
			scanLine.trim();
			while(thisLine != null){
				if(thisLine.length() > 0){
					if(thisLine != scanLine){
						System.out.println("false");
						return false;
					}
				}
				thisLine = in.readLine();
				scanLine = scan.in.readLine();
			}
			System.out.println("true");
			return true;
		}
	}
	
	public static void main(String [] args){
		Scanner scan = new Scanner("in3.txt");
		Scanner scan2= new Scanner("in2.txt");
		Scanner scan3= new Scanner("in3.txt");
		scan.equals(scan2);
		scan.equals(scan3);
		//scan.print("out.txt");
		//scan.maxHRun("out.txt");
		//scan.rows();
		//scan.columns();
	}

}