Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: how to add strings as numbers?

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to add strings as numbers?

    hey

    i am trying to add two strings as numbers, as an exampel :

    String numb1 ="30";
    String numb2 ="30";

    where the answer would be 60 and not 3030. any ideas how to do that ?


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to add strings as numbers?

    Quote Originally Posted by Dixxon View Post
    i am trying to add two strings as numbers, as an exampel :

    String numb1 ="30";
    String numb2 ="30";

    where the answer would be 60 and not 3030. any ideas how to do that ?
    Convert (better to say "parse") String to int. See parseInt, static method of java.lang.Integer.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to add strings as numbers?

    will that work ? as the method i am using is a string method , looks like this
    public static String add (String numb1, String nub2) 
     {
    	 // any code
     }

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to add strings as numbers?

    Quote Originally Posted by Dixxon View Post
    will that work ?
    How/where you encapsulate the sum of two string-numbers is not very much important. The important base concept is the conversion from String to int (or long or double or other primitive, you can choose). And I repeat: see the parseInt method of Integer.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to add strings as numbers?

    i think it would look like :
     public static String addera (String tal1, String tal2) 
     {
    	 String Numb1= "30";
    	 String Numb2 ="30";
    	 int IntNumb1 = Integer.parseInt(Numb1);
    	 int IntNumb2 = Integer.parseInt(Numb2);
    	 return(IntNumb1 + IntNumb2);
     // gives an error "Type mismatch: cannot convert from int to String"
     }

    PS: i am new to java so excuse my questions....

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to add strings as numbers?

    Quote Originally Posted by Dixxon View Post
    i think it would look like :
     return(IntNumb1 + IntNumb2);
     // gives an error "Type mismatch: cannot convert from int to String"
    return "" + IntNumb1 + IntNumb2;

    or (better)

    return Integer.toString(IntNumb1 + IntNumb2);

    or also (Integer.toString is called under the hood)

    return String.valueOf(IntNumb1 + IntNumb2);
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. The Following User Says Thank You to andbin For This Useful Post:

    Dixxon (December 8th, 2013)

  8. #7
    Junior Member
    Join Date
    Nov 2013
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to add strings as numbers?

    oh yeah thanks , i got it work with String.valueOf()

    but while adding 3333333333333 + 3 it gives me an error :

    Exception in thread "main" java.lang.NumberFormatException: For input string: "3333333333"
    at java.lang.NumberFormatException.forInputString(Unk nown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at OperationerMedNaturligaHeltalGivnaSomTeckenstranga r.addera(OperationerMedNaturligaHeltalGivnaSomTeck enstrangar.java:60)
    at OperationerMedNaturligaHeltalGivnaSomTeckenstranga r.main(OperationerMedNaturligaHeltalGivnaSomTecken strangar.java:19)

    any ideas?

  9. #8
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: how to add strings as numbers?

    Quote Originally Posted by Dixxon View Post
    but while adding 3333333333333 + 3 it gives me an error :

    Exception in thread "main" java.lang.NumberFormatException: For input string: "3333333333"
    3333333333333 is more than the largest int value. An int is 32 bit, ranges from -2147483648 to +2147483647.

    Quote Originally Posted by Dixxon View Post
    any ideas?
    Use long (64 bit). And please, see Long.parseLong method.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. How to add the numbers in the following series?
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2013, 07:23 PM
  2. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Swing Tutorials
    Replies: 11
    Last Post: October 15th, 2013, 10:22 PM
  3. How to add strings to this Code?
    By HARMAN in forum Java Theory & Questions
    Replies: 3
    Last Post: June 8th, 2013, 08:00 PM
  4. How to add line numbers in Eclipse
    By Brt93yoda in forum Java JDK & IDE Tutorials
    Replies: 1
    Last Post: January 5th, 2012, 11:30 PM
  5. How To: Add line numbers to your JTextArea
    By Freaky Chris in forum Java Code Snippets and Tutorials
    Replies: 10
    Last Post: March 31st, 2011, 05:18 AM