
-----------------------------------
JayLo
Mon Apr 05, 2004 5:13 pm

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?  :o

-----------------------------------
nate
Mon Apr 05, 2004 6:03 pm


-----------------------------------


%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
Mon Apr 05, 2004 9:05 pm


-----------------------------------
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 
String reply;
reply = JOptionPane.showInputDialog("This sucks");
insert checker here.

-----------------------------------
wtd
Mon Apr 05, 2004 9:42 pm


-----------------------------------
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
Mon Apr 05, 2004 9:47 pm


-----------------------------------
oh, sorry!!
lol
i just read string - int and was like "Hey i know that".

Oh well, i learned something new. Thanks WTD
