1. Member
Join Date
Oct 2009
Posts
9
Rep Power
0

## Really Basics

// Calculate sum, product, difference and quotient
import java.util.Scanner;
import javax.swing.JOptionPane;

{
public static void main( String[] args )
{
JOptionPane.showMessageDialog( null, "Welcome To calculator" );

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

int x;
int y;
int sum;
int difference;
int product;
int division;
int remainder;
int z;

x = input.nextInt();

y = input.nextInt();

System.out.println( "For the procedure you want to do type and enter, \n1 for sum \n 2 for difference\n 3 for division \n4 for multiplacation \n5 for remainder " );
z = input.nextInt();

if ( z == 1)

sum = x + y;

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

if ( z == 2 )

difference = x - y;

System.out.printf( "Difference is %d\n", difference );

if ( z == 3 )

product = x * y;

System.out.printf( "Product is %d\n", product );

if ( z == 4 )

division = x / y;

System.out.printf( "Quotient is %d\n", division );

if ( z == 5 )

remainder = x%y;

System.out.printf( "Remainder is %d\n", remainder );

}
}
}

when i compile this one ( i know its not well-written, lets say its just a simple one) it gives the error

upgraded.java:36: variable sum might not have been initialized
System.out.printf< "Sum is %d\n", sum >;
^

and same for the other 5 systemoutprintf lines, too.

The problem is always seems like with the last word, such as sum - difference etc.

Thats it. Any ideas why that happens?
Last edited by Taluntain; 10-07-2009 at 08:09 PM.

2. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Read the message to understand what it is saying. That's why it's being printed.
Did you initialize those variable before using them?

3. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
yes i read it but the thing is i didnt get the initalizing part, how to initialize them?

4. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
You do know what initialize means, right?
Assign an initial value.

5. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
afaik initialize means getting ready to start. as i said above im just very beginner with java so my questions might be really silly sorry for that.

lets say how can i give sum an initial value i dont know it.

6. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Java Code:
`sum = 0;`
.

7. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
Originally Posted by Taluntain

Java Code:
` sum = x + y;`
i wrote that shouldn't be enough?
Last edited by Taluntain; 10-07-2009 at 09:15 PM. Reason: typo

8. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Inside an if block. The compiler is smart enough to realize that the block may never be executed so it complains anyway.

9. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
please tell me if im getting you pissed of :)

Can you give the true code because when i put int in if block like :
if ( z == 4 )
int division;
division = x / y;
System.out.printf( "Quotient is %d\n", division );

it gives another error which says that int is not a statement and .class expected.

10. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
I meant that you initialized the sum inside an if block, so the compiler can't be sure if it will be initialized since the if test may return false and still leave sum uninitialized.

11. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
I got the error, also got the reason of the error.

But still I tried many things none of them fixed my error. Sorry to fail you tho.

if + int made me confused.

12. We can't help if you don't post your latest code.
also, use code tags.

13. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
Java Code:
```// Calculate sum, product, difference, quotient and remainder
import java.util.Scanner;
import javax.swing.JOptionPane;

{
public static void main( String[] args )
{
JOptionPane.showMessageDialog( null, "Welcome To Calculator \n © Hakan Incesulu of Ozyegin University"  );

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

int x;
int y;
int z;
int sum;
int difference;
int product;
int division;
int remainder;

System.out.print( "Enter your first number darling: " );
x = input.nextInt();

System.out.print( "Enter your second number teddy bear: " );
y = input.nextInt();

System.out.println( "Select a number to decide which operation you want to do \n1 for sum\n2 for difference\n3 for division\n4 for multiplacation\n5 for remainder " );
z = input.nextInt();

if ( z == 1)
sum = x + y;
System.out.printf( "Sum is %d\n", sum );

if ( z == 2 )
difference = x - y;
System.out.printf( "Difference is %d\n", difference );

if ( z == 3 )
product = x * y;
System.out.printf( "Product is %d\n", product );

if ( z == 4 )
division = x / y;
System.out.printf( "Quotient is %d\n", division );

if ( z == 5 )
remainder = x % y;
System.out.printf( "Remainder is %d\n", remainder );

}
}
}```

that's the full code. i am still stuck

14. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Originally Posted by Taluntain
.... i am still stuck
Because you seem to be unable to read or understand the responses posted to you. I, for one, don't see any more reasons for posting further responses.

15. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
Originally Posted by r035198x
I meant that you initialized the sum inside an if block, so the compiler can't be sure if it will be initialized since the if test may return false and still leave sum uninitialized.
actually I get what you say. if i put int inside if block it still doesnt accept it. i asked for a simple answer, where should I put the int sum to get it accepted. can you just correct my code?

16. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
10
Originally Posted by Taluntain
... can you just correct my code?
No. The object of the exercise is not for you to submit correct code. It's for you to understand the concepts and syntax rules behind the correct code.
Think about it. Where would you initialize so that it will be definitely assigned at all points in the method.

17. Member
Join Date
Oct 2009
Posts
9
Rep Power
0
Solved the problem. Required {} brackets after if close. thank you for your helps totally.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•