user2464795
user2464795

Reputation: 7

Changing Variables with user inputs

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);

Upvotes: 1

Views: 753

Answers (4)

user3437460
user3437460

Reputation: 17454

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

Upvotes: 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);
}

}

Upvotes: 0

Culyx
Culyx

Reputation: 539

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()));

Upvotes: 0

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

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.

Upvotes: 1

Related Questions