Author |
Message |
JayLo
|
Posted: Mon Apr 05, 2004 5:13 pm Post subject: Is there a strintok/intstrok function in java? |
|
|
I was just wondering if there was a function to check if a string could be parsed as an integer/double or vice versa. Any takers? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
nate
|
Posted: Mon Apr 05, 2004 6:03 pm Post subject: (No subject) |
|
|
code: |
%Going from string - integer
int number = Integer.parseInt("10");
%Going from string - double
double number = Double.parseDouble("10.2");
%Going from double or integer - string
String word = number.toString();
|
Hope that helps you |
|
|
|
|
|
JayLo
|
Posted: Mon Apr 05, 2004 9:05 pm Post subject: (No subject) |
|
|
thanks for that review, nate. yes, i know that integer.parseint (mind my capitalization) is to parse strings to int, but i was asking for a command to CHECK if a string variable can be parsed.
for example
code: | String reply;
reply = JOptionPane.showInputDialog("This sucks");
insert checker here. |
|
|
|
|
|
|
wtd
|
Posted: Mon Apr 05, 2004 9:42 pm Post subject: (No subject) |
|
|
code: | import java.lang.*;
import java.io.*;
import java.util.*;
import java.util.regex.*;
class Test {
public static boolean checkForIntegers(String input) {
Pattern intPattern = Pattern.compile("\d+");
Matcher intMatcher = intPattern.matcher(input);
return intMatcher.matches();
}
public static void main(String[] args) {
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
String userInput = keyboard.readLine();
if (checkForIntegers(userInput))
System.out.println("\"" + userInput + "\" contains an integer");
else
System.out.println("\"" + userInput + "\" does not contain an integer");
}
} |
|
|
|
|
|
|
nate
|
Posted: Mon Apr 05, 2004 9:47 pm Post subject: (No subject) |
|
|
oh, sorry!!
lol
i just read string - int and was like "Hey i know that".
Oh well, i learned something new. Thanks WTD |
|
|
|
|
|
|