Results 1 to 4 of 4

Thread: Java String to Int?

  1. #1
    Senior Member
    Join Date
    Oct 2005
    Posts
    106

    Java String to Int?

    OK, So I am working on a calculator that takes in LaTeX code and spits out step by step instructions on how to solve the problem. Right now I am working on the sigma series, but I am having some difficulty trying to get the value of a String in terms of integers.

    For example a hypothetical input would be "\sum^{5}_{i=0}i"
    I need to take it apart, the create a "for"-loop that runs while i<=5 and the solution is "0+1+2+3+4+5". However, the problem is taking the "^{5}" and making it into an integer value.

    Same goes for the "_{i=0}" clause. And I guess the same goes for the summation term.

    How would I do this?

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    If you can get it down to just the integer within that string you want you can use:
    Integer.parseInt(stringcontainingonlyinteger);
    I'm not sure if that's what you want or if you want to know how to actually break it down to just the integer.

  3. #3
    Senior Member
    Join Date
    Oct 2005
    Posts
    106
    That's exactly what I was looking for, thanks! I'll look this up to see how it is used...
    "The Texan turned out to be good-natured, generous and likeable. In three days no one could stand him." Catch 22 by Joseph Heller.

    Buddies? I have no buddies...


    Give the BSD daemon some love (proud FreeBSD user)

  4. #4
    Developer Extraordinar
    Join Date
    Jul 2002
    Location
    On the IRC
    Posts
    572
    Code:
    public class test {
       public static void main(String[] args){
          int getInt=0;
          String numString="454"; // The String to convert
          try{
             getInt = Integer.parseInt(numString); // Change the String to an int
             System.out.println(getInt); // Will output the number
          catch(NumberFormatException nfex){ 
             System.out.println("Could not convert your number");
          }
       }
    }
    Or something like that. The try-catch bit isn't needed, but it makes things smoother in the long run if something goes wrong.

    enmand

Posting Permissions

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