Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Is there a strintok/intstrok function in java?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JayLo




PostPosted: 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? Surprised
Sponsor
Sponsor
Sponsor
sponsor
nate




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: