1

Hey I am new to Java programing and I am wrestling with changing varibles and looking for a bit of help. The question is below:

Create a program that asks the user for three numbers and then prints their sum. Use the following structure in your program:

    // TODO code application logic here
    Scanner reader = new Scanner(System.in);
    int sum = 0;
    int read;//`enter code here`
    // WRITE YOUR PROGRAM HERE
    // USE ONLY THE VARIABLES sum, reader AND read!
    System.out.println("Sum: " + sum);

This is what I wrote below and of course I am getting syntax errors:

    Scanner reader = new Scanner(System.in);
    int sum = 0;
    System.out.print("Type the first Number: ");
    int read = Integer.parseInt(reader.nextLine());
    System.out.print("Type the second number: ");
    int read = read + (Integer.parseInt(reader.nextLine()));
    System.out.print("Type the third number: ");
    int read = read + (Integer.parseInt(reader.nextLine()));

    sum = read;
    System.out.println("Sum: " + sum);
1
  • You should not be declaring your variables over and over again. int read is only required to create the variable. After that you can just use read. Also it's probably better to accumulate the result in sum instead of in read, that's it's purpose after all Commented Nov 12, 2013 at 20:48

4 Answers 4

1

Your code should be something like:

   Scanner reader = new Scanner(System.in);

    int sum = 0;
    System.out.print("Type the first Number: ");
    int read = Integer.parseInt(reader.nextLine());
    System.out.print("Type the second number: ");
    read = read + (Integer.parseInt(reader.nextLine()));
    System.out.print("Type the third number: ");
    read = read + (Integer.parseInt(reader.nextLine()));

    sum = read;
    System.out.println("Sum: " + sum);

You are getting the syntax errors because your variable read is being declared multiple times.

You could also use the compound assignment operator:

read += (Integer.parseInt(reader.nextInt())); //using .nextInt() per @Zong Zheng Li suggestion.
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe also use reader.nextInt() instead of Integer.parseInt(reader.nextLine()).
0

You declare the variable read over again, trying to declare the same variable again will give a syntax error.

int read = Integer.parseInt(reader.nextLine());
System.out.print("Type the second number: ");
read = read + (Integer.parseInt(reader.nextLine()));
System.out.print("Type the third number: ");
read = read + (Integer.parseInt(reader.nextLine()));

Comments

0

package ejercicio.pkg25.cambio.de.variables.suma.de.tres.numeros;

/** * @author Diego Urrea */ import java.util.Scanner;

public class Ejercicio25CambioDeVariablesSumaDeTresNumeros {

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    int sum = 0;
    int read;
    System.out.println("Digita el primer número");
    read = Integer.parseInt(reader.nextLine());
    sum = sum+read;
    System.out.println("Digita el segundo número");
    read = Integer.parseInt(reader.nextLine());
    sum = sum+read;
    System.out.println("Digita el número tres");
    read = Integer.parseInt(reader.nextLine());
    sum = sum+read;
    System.out.println("Sum: " + sum);
}

}

Comments

0

Actually for this short question, you don't even need to use read variable.

You could do it this way.

Scanner reader = new Scanner(System.in);
int sum = 0;

System.out.print("Type the first Number: ");
sum += Integer.parseInt(reader.nextLine());
System.out.print("Type the second number: ");
sum += Integer.parseInt(reader.nextLine());
System.out.print("Type the third number: ");
sum += Integer.parseInt(reader.nextLine());
System.out.println("Sum: " + sum);

INPUT:

1
2
3

OUTPUT:

6

1 Comment

@user2464795 Remarks: Your error is caused by creation of same variable within the same scope.

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.