• Post Reply Bookmark Topic Watch Topic
  • New Topic

Addition of 2 strings  RSS feed

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Total_2Strings {
public static void main(String[] args) {
String k="10";
String j="10";
String total=k+j;
System.out.println(total);
}
}
output:1010.
My questions: Here i would like to print 20 instead 1010.
please any one solve this one.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:

output:1010.
My questions: Here i would like to print 20 instead 1010.
please any one solve this one.


Welcome to the Ranch, Slaxmi!

When you post code, please UseCodeTags(⇐click) so it will be readable. I've added them where I quoted you here, along with some indenting.

First of all, do you understand why you're getting "1010" as the output? Once you understand that, you should have an idea of what you need to do in order to get "20" instead.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Slaxi,

You need to understand Java data types at a very basic level.

If I add the Strings "Slaxmi" to "Raj", will I get a number in return?

WP

 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William P O'Sullivan wrote:Hi Slaxi,

You need to understand Java data types at a very basic level.

If I add the Strings "Slaxmi" to "Raj", will I get a number in return?

WP


thank you for your reply.
here my questions is :i would like to total 20.
so i have to add the values in those strings.how can i add the values in the strings?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:thank you for your reply.
here my questions is :i would like to total 20.
so i have to add the values in those strings.how can i add the values in the strings?



One option is parse the string to integer values first. For something as simple as this, you can even use one of the simple parsers -- such as the one provided by the java.lang.Integer class.

Henry
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:
William P O'Sullivan wrote:Hi Slaxi,

You need to understand Java data types at a very basic level.

If I add the Strings "Slaxmi" to "Raj", will I get a number in return?

WP


thank you for your reply.
here my questions is :i would like to total 20.


Yes, that much is clear. We're trying to get you to understand WHY you're seeing "1010" now, as a step to helping you figure out how to get the results you want.

So, do you understand WHY you're seeing "1010" now? If not, what would expect if you had Strings "a" and "b" and you used the + operator to "add" them?

On the other hand, you probably know of some situation in which 10 + 10 will get you 20 instead "1010", right? Can you show some java code for that situation?

People here are trying to get you to think about your problem so that you can understand it well enough to be able to figure out your own solution.
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you need to cast the string to integer or some numeric data and the add both of them . then go for displaying results
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote:First you need to cast the string to integer ...

You cannot cast a String to an Integer. You can convert it, though. Type casting does not do any kind of conversion.
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i mean to say is [deleted]



edit - please don't just hand out the answer - fbr
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote:i mean to say is [deleted]

@Vinayak I think you must be knowing that the folks around here helping others are skilled enough to put out this basic thing up front. They chose to not to do it for a reason. Please look into the post before your first post by Jeff. He is trying to make Slaxmi learn an important step, so he/she not only gets his/her doubts resolved but also learn in the process.

Giving straight answers is easy, but may not be the most helpful thing always.
 
Slaxmi Raj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vinayak jog wrote:i mean to say is Integer.parseInt("your string");

Thank your reply, i wrote the below code and executed and i got the total 20.
public class Total_2Strings {
public static void main(String[] args) {
String k="10";
String j="10";
int a=Integer.parseInt(k);
int b=Integer.parseInt(j);
System.out.println("total="+(a+b));
}
}
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Slaxmi Raj wrote:
Thank your reply, i wrote the below code and executed and i got the total 20.


It would have been better if you had taken the hints you were given and used them to figure out for yourself what was going on. Just getting answers handed to you is not an effective way to learn.
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done Slaxmi,

But do you understand what you are doing?

That is the only way you can evolve and learn.

WP
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!