javaaddition

Print the formula used to describe the addition of two numbers


I am stuck in the U of Helsinki Java MOOC:

Create a program that can be used to add two integers together. In the beginning, the user is asked to give two integers that are to be summed. The program then prints the formula that describes the addition of the numbers.

Example output:

Give the first number:
5
Give the second number:
4
5 + 4 = 9

I am trying to get the system to print " "first" + "second" is "result". For some reason I am stumped on this otherwise easy question. My code is always throwing an error. What am I doing wrong in the last line?

import java.util.Scanner;

public class AdditionFormula {

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // write your program here
    System.out.println("Give the first number: ");
    int first = Integer.valueOf(scanner.nextLine());
    
    System.out.println("Give the second number: ");
    int second = Integer.valueOf(scanner.nextLine());
    
    //System.out.println("first" " + " Integer.valueOf(first) + Integer.valueOf(second));
    System.out.println(first + " + " + second " = " + (first + second));
}

Solution

  • The code you provided does not compile

    Change to

    System.out.println(first + " + " + second + " = " + (first + second));