I'm attempting to learn Java. I'm finding it to be frustrating and quite foreign from most other languages. I have something here typed exactly as the book has it. According to the book, it should be working, but it's not. Any tips would be SO great! I really feel like an idiot asking for help with this simplistic of a program.

import java.util.Scanner;


public class Addition
{

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

        int num1;  //first num to add
        int num2;  //second num to add
        int sum;  //sum of added nums

        System.out.print("Enter first integer: ");
        num1 = input.nextInt();  //read first num from user

        System.out.print("Enter second integer: ");
        num2 = input.nextInt();  //read second num from user

        sum = num1 + num2;  //add numbers

        System.out.printf("Sum is %d\n", sum);  //display sum
    }

}

It's the lines

Scanner input == new Scanner(System.in);

num1 = input.nextInt();  //read first num from user

that NetBeans isn't liking. It says "variable input might not have been initialized".

Recommended Answers

All 2 Replies

I think your problem is that you are using '==' when you need to use '='.
a == b means true if a is the same as b whereas
a = b means assign the value of b to a.

So try replacing
Scanner input == new Scanner(System.in);
with
Scanner input = new Scanner(System.in);

P.S. I agree that java sucks :)

P.S. I agree that java sucks :)

Why? Because Someone accidentally misplaced this '=' with this '=='.
I am not accusing anyone since I have made such mistakes when copying code, but tell me one language that doesn't use this operator: '=' for assignment.

I'm finding it to be frustrating and quite foreign from most other languages

And what other languages are you referring to, since most languages have if-else statements, assignment operators '=' and for loops.

But do other languages have such good documentation?
http://java.sun.com/javase/6/docs/api/overview-summary.html
There you can find the Scanner class and see all the methods with their description:

import java.util.Scanner;

You will have to go to the link:
>java.util<

After you have that, all you need to know is how to call a method and get what it returned, which is the same in all languages:

num1 = input.nextInt();

Calling method: nextInt with no arguments: nextInt() that returns an int:
num1 = input.nextInt()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.