1

I'm trying to write a program that reads an integer from the user (through the keyboard), adds 100 to it and displays the result. All I can do is get them to concatenate like 2 strings, instead of adding the numbers together. I can't understand why it won't add them.

import java.io.*;  
public class Program  { 
   public static void main(String[] args) throws IOException { 
      InputStreamReader isr = new InputStreamReader(System.in); 
      BufferedReader br = new BufferedReader(isr); 

      System.out.print("Enter some text: "); 
      String text = br.readLine();  
      int number = Integer.parseInt(text);

      System.out.println(" Your value + 100 is " + ( 100 + text));
     }
  }

is the code I am using and:

Enter some text: 66
 Your value + 100 is 10066

is what is printed on the screen.

1
  • 5
    That should be 100 + number. Commented Oct 22, 2013 at 14:12

3 Answers 3

7

You are adding the wrong variable. Use this instead:

System.out.println(" Your value + 100 is " + ( 100 + number));
Sign up to request clarification or add additional context in comments.

Comments

2

Text is the String, number an int, thus use:

System.out.println(" Your value + 100 is " + ( 100 + number));

For Strings + concatenates.

Comments

1
int number = Integer.parseInt(text) + 100;

 System.out.println(" Your value + 100 is " + ( number));

or

System.out.println(" Your value + 100 is " + ( 100 + number));

For a string "+" works to concatenate , i.e append the string together :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.