Java/Numeric: Difference between revisions
From charlesreid1
No edit summary |
|||
| Line 18: | Line 18: | ||
</pre> | </pre> | ||
==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: | |||
<pre> | |||
String n = "283982749847594837"; | |||
// Populate a queue with characters | |||
Queue<Character> q = new LinkedList<Character>(); | |||
char[] narr = n.toCharArray(); | |||
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) { | |||
z = -1; | |||
if( c.isDigit() ) { | |||
z = Character.digit(c,10); | |||
} else { | |||
throw new IllegalArgumentException("Malformed input. Numeric strings only."); | |||
} | |||
if(j<q.size()-1) { | |||
System.out.println(" , "); | |||
} | |||
} | |||
</pre> | |||
=Flags= | =Flags= | ||
{{CSFlag}} | {{CSFlag}} | ||
Revision as of 20:43, 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:
String n = "283982749847594837";
// Populate a queue with characters
Queue<Character> q = new LinkedList<Character>();
char[] narr = n.toCharArray();
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) {
z = -1;
if( c.isDigit() ) {
z = Character.digit(c,10);
} else {
throw new IllegalArgumentException("Malformed input. Numeric strings only.");
}
if(j<q.size()-1) {
System.out.println(" , ");
}
}
Flags
| Computer Science notes on computer science topics on the wiki, for educational and learning purposes
Part of the 2017 CS Study Plan.
Python/Exceptions · Python/Assertions · Python/Decorators Python/Os (os module) · Python/Strings Python/Splat · Python/Iterators · Python/Generators Python/Comparators · Python/Lambdas
Builtin features of Java: Java/Exceptions · Java/Assertions · Java/Memory · Java/Interfaces Java/Generics · Java/Decorators · Java/Diamond Notation Java/Iterators · Java/Iterable · Iterators vs Iterable Java/Comparators · Java/Comparable · Comparators vs Comparable Java/Numeric · Java/TypeChecking · Java/Testing · Java/Timing · Java/Profiling Documentation: Javadocs · Java/Documentation Tools and functionality: Java/URLs · Java/CSV External libraries: Guava · Fastutil · Eclipse Collections OOP: OOP Checklist · Java/Abstract Class · Java/Encapsulation · Java/Generics
|
See also: