Results 1 to 7 of 7
  1. #1
    LAW
    LAW is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default how to convert a string to an integer

    Can you convert a string like "Sunday" to the integer 0 in order to incerment to the next day or decrement to the previous day?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    put it in an array of Strings and then increment the index of the array. Use the mod operator (%) if you want to be able to loop through the array. For instance if you have an array of week days, you'd increment the index then mod it by 7 so that when you went past 6 (Saturday), you'd next hit 0 (Sunday).

  3. #3
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    public class ConvertStringToInt {
    public static void main(String[] args) {
    String aString = "78";
    int aInt = Integer.parseInt(aString);

    System.out.println(aInt);
    }
    }
    Last edited by Fubarable; 11-08-2009 at 07:15 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    raq, your post has nothing to do with the OP's question and contains spam advertisement in your signature which I've removed. Please consider yourself warned to cease and desist.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by Fubarable View Post
    raq, your post has nothing to do with the OP's question and contains spam advertisement in your signature which I've removed. Please consider yourself warned to cease and desist.
    Did you ban him Fubarable. I just try to do it, but fail. Seems you've done it.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I did it for a few days, but then un-banned him as I saw that he/she posted something relatively helpful in another thread.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by LAW View Post
    Can you convert a string like "Sunday" to the integer 0 in order to incerment to the next day or decrement to the previous day?
    If you want to deal with days of a week (to keep set of constant) enum is much better.

    Enum Types (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 12:31 PM
  2. How to convert Integer[] to int[]
    By Nithya in forum New To Java
    Replies: 26
    Last Post: 02-11-2010, 05:41 PM
  3. Integer to String
    By zervine in forum Forum Lobby
    Replies: 3
    Last Post: 09-12-2008, 12:07 PM
  4. Replies: 0
    Last Post: 04-06-2008, 07:40 PM
  5. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •