HW1: Sarah Thayer


Sthayer (Talk | contribs)
(solution to hw1)
Newer edit →

Current revision as of 16:08, 22 September 2009

My solution to the 'cleanup' program.

	
	
	import java.io.BufferedReader;
	import java.io.IOException;
	import java.io.InputStreamReader;
/*
 * This class accepts a String input from the user and removes all non-numeric 
 * characters from the string and displays the result. 
 * @author Sarah Thayer
 * 9/22/09	
 */
public class homework1 {
	/*
	 * removeElement() removes an element from the given array at specified
	 * index
	 * @param array is the array
	 * @param index is the index of the element to be removed
	 */
	public static void removeChar(char[] array, int index )
	{
	   int numElmts = array.length - (index+1); //determine total number of elements
	   //copy and shift elements over by 1, resulting in 'deletion' of an element
	   System.arraycopy(array, index+1, array, index, numElmts);
	}

	/*
	 * cleanup() determines if there are any non-numeric characters in a String
	 * and removes them, returning a "clean" number without these non-numerics
	 * @param number is the input String
	 * @return is a number without non-numeric characters
	 */
	public static String cleanup(String number){
		//convert String number to a character array
		char array[] = number.toCharArray();
		//in order to return a string with the correct number of
		//elements, it is necessary to determine how many elements were removed
		//from the string.
		int removed=0; 
		for (int i=0; i<number.length(); i++) { //loop through the array
			if (!Character.isDigit(array[i])){ //if the character is NOT a digit
				removeChar(array, i); //remove that character
				removed++; //increment the number of removed elements
				i--; //decrement i so that it doesn't skip the next element in the array
			//System.out.println(array); (test code)
			}
		}
		String clean = new String(array); //convert array back into string
		
		//get a substring of the string so that it doesn't include extra elements
		//added onto the end in removeElement(), and returns only the numeric
		//elements of the string
		clean = clean.substring(0,number.length()-removed);
		return clean;
	}
		/*
		 * main() runs the program and I/O
		 * @param args takes an array of String arguments
		 */
	public static void main(String[] args) {
	  //initialize a new BufferedReader for input     
	  BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
	   String number = "";  //initialize number variable
	   System.out.println("Enter a number");
	       
	   // Put input into variable
	        try {
	            number = dataIn.readLine();
	        }
	        catch(IOException e) {
	            System.out.println("Error!");
	        }
	       
	   // Output 'clean' number
	       System.out.println("Result:" + cleanup(number));
	    }
}

Note: it didn't come out as perfect as I'd wanted, as I couldn't figure out how to make it entirely remove letters, no matter where they were located. For example, 1234qwerty1234 accurately resulted in 12341234, but hi21hi went into an infinite loop because it couldn't handle beginning with a letter instead of a number - it threw the indices required for the cleanup() for loop and the removeChar() method completely off. I've always had a bit of a love/hate relationship with both arrays and for loops, though, so it's likely a simple fix I will feel like an idiot for not getting. Ideas are appreciated. I did begin this assignment trying it in PHP, but was frustratingly unsuccessful. I intend to try until I am successful, and if that ever comes I'll post that as an example as well.