From charlesreid1

 
(3 intermediate revisions by the same user not shown)
Line 23: Line 23:


<pre>
<pre>
///////////////////////////////////////////////////////////
// Create a container of digit-characters
String n = "2432902008176640000";
char[] narr = n.toCharArray();
///////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////
// Create a container of digit-characters
String n = "2432902008176640000";
char[] narr = n.toCharArray();


///////////////////////////////////////////////////////////
// Populate a queue with characters
Queue<Character> q = new LinkedList<Character>();
for(int i=0; i<narr.length; i++) {
    q.add(narr[i]);
}


// Print out the square of each character interpreted as a digit
 
int j = 0;
///////////////////////////////////////////////////////////
for(Character c : q) {
// Populate a queue with characters
    z = -10000;
Queue<Character> q = new LinkedList<Character>();
    if( c.isDigit() ) {  
for(int i=0; i<narr.length; i++) {
        z = Character.digit(c,10);
    q.add(narr[i]);
    } else {
}
        throw new IllegalArgumentException("Malformed input. Numeric strings only.");
 
    }
 
    if(j<q.size()-1) {
///////////////////////////////////////////////////////////
        System.out.println(" , ");
// Print out the square of each character interpreted as a digit
    }
StringBuilder sb = new StringBuilder();
}
 
int j = 0;
for(Character c : q) {
    int z = -10000;
// Character.isDigit() and Character.digit() are static methods
    if( Character.isDigit(c) ) {
        z = Character.digit(c,10);
sb.append(z*z);
if(j<q.size()-1) {
        sb.append(", ");
    }
    } else {
        throw new IllegalArgumentException("Malformed input. Numeric strings only.");
    }
    j++;
}
 
 
System.out.println(sb.toString());
</pre>
</pre>



Latest revision as of 20:56, 18 June 2017

Check if a string is numeric

To check if a String in Java is numeric, build a simple utility function:

public static boolean isNumeric(String str)  
{  
    try  
    {  
        double d = Double.parseDouble(str);  
    }  
      catch(NumberFormatException nfe)  
    {  
        return false;  
    }  
    return true;  
}

Interpret character as digit

The Character class has the static digit method, which takes a character and an integer radix, and returns an integer - the character interpreted as an integer. It also has a boolean isDigit() to check if the character is a digit. Use them in combo:


		///////////////////////////////////////////////////////////
		// Create a container of digit-characters
		String n = "2432902008176640000";
		char[] narr = n.toCharArray();



		///////////////////////////////////////////////////////////
		// Populate a queue with characters
		Queue<Character> q = new LinkedList<Character>();
		for(int i=0; i<narr.length; i++) {
		    q.add(narr[i]);
		}


		///////////////////////////////////////////////////////////
		// Print out the square of each character interpreted as a digit
		StringBuilder sb = new StringBuilder();

		int j = 0;
		for(Character c : q) {
		    int z = -10000;
			// Character.isDigit() and Character.digit() are static methods
		    if( Character.isDigit(c) ) {
		        z = Character.digit(c,10);
			sb.append(z*z);
			if(j<q.size()-1) {
		    	    sb.append(", ");
		    	}
		    } else {
		        throw new IllegalArgumentException("Malformed input. Numeric strings only.");
		    }
		    j++;
		}


		System.out.println(sb.toString());

Flags





See also: