All pastes #273989 Raw Edit

Miscellany

public text v1 · immutable
·published 2006-12-10 09:42 UTC
rendered paste body
import java.util.*;
public class Scanner {
	Disk in, out; Set64 board; Set64 longSeq; //Collection board;
	
	/**
	 * @param filename the filename of the input text file containing the scanner array 
	 */
	public Scanner(String filename){
		in = new Disk(filename, "in");
	}
	
	/** 
	 * @param s a String containing no blanks
	 * @return its argument without modification
	 */
	protected String newWord(String s){
		return s;
	}
	
	/**
	 * @param s a String representing a line in a text file
	 * @return the resulting String after newWord has been applied to each word in s
	 */
	protected String lineHandler(String s){
		StringTokenizer toks = new StringTokenizer(s);
		String result = "";
		while (toks.hasMoreTokens()){
			result += newWord(toks.nextToken()) + " ";
		}
		return result;
	}
	
	/** 
	 * print 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();
	}
	
	public void maxHRun(String filename){
		Set64 board = new Set64();
		//Collection board = new ArrayList();
		Set64 longSeq = new Set64();
		String line = in.readLine();
		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++){
					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-i;
						   }
						   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-i;
						   }
						   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("new.txt");
	}
	
	public void printBoard(String filename){
		Disk out = new Disk(filename, "out");
		//Iterator boardItr = board.iterator();
		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);
		}
	}
	
	
	//public static void main(String [] args){
		//Scanner scan = new Scanner("in.txt");
		//scan.print("out.txt");
	//}

}