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 ?
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.
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 ?
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
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 }
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
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....
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
Dixxon (December 8th, 2013)
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?
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code