Converting types

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javatech007
    New Member
    • Nov 2007
    • 51

    Converting types

    How would you convert a string that is loaded from a txt file into a integer so it can be used for calculations?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Originally posted by javatech007
    How would you convert a string that is loaded from a txt file into a integer so it can be used for calculations?
    The Integer class has parsing methods: http://java.sun.com/javase/6/docs/ap...g/Integer.html

    Comment

    • rodeoval
      New Member
      • May 2007
      • 15

      #3
      String intValue;
      Integer integer = new Integer(intValu e);
      int value = integer.intValu e();

      Comment

      • Doegon
        New Member
        • Feb 2008
        • 14

        #4
        alternatively you could use a scanner object to read from the file and use the function of parseInt to convert from string to integer and use the value for calculations.
        i.e
        Scanner inFile = new Scanner(new FileReader("fil ename"));
        String x = inFile.next();
        int x2 = Integer.parseIn t(x);

        then int sqr = Math.pow(x2,2); //this is just an example of a calculation you can do after converting

        Note: read more on the Scanner class API and Integer to understand what i just wrote.

        hope its easier to understand and helps you.
        good luck

        Comment

        • BigDaddyLH
          Recognized Expert Top Contributor
          • Dec 2007
          • 1216

          #5
          Originally posted by rodeoval
          String intValue;
          Integer integer = new Integer(intValu e);
          int value = integer.intValu e();
          Or, in one line:
          [CODE=Java]int value = Integer.parseIn t(str);[/CODE]

          Comment

          Working...