# Really Basics

• 10-07-2009, 08:07 PM
Taluntain
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?
• 10-07-2009, 08:37 PM
r035198x
Read the message to understand what it is saying. That's why it's being printed.
Did you initialize those variable before using them?
• 10-07-2009, 08:44 PM
Taluntain
yes i read it but the thing is i didnt get the initalizing part, how to initialize them?
• 10-07-2009, 08:50 PM
r035198x
You do know what initialize means, right?
Assign an initial value.
• 10-07-2009, 08:53 PM
Taluntain
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.
• 10-07-2009, 09:10 PM
r035198x
Code:

`sum = 0;`
.
• 10-07-2009, 09:15 PM
Taluntain
Quote:

Originally Posted by Taluntain

Code:

` sum = x + y;`

i wrote that shouldn't be enough?
• 10-07-2009, 09:20 PM
r035198x
Inside an if block. The compiler is smart enough to realize that the block may never be executed so it complains anyway.
• 10-07-2009, 09:28 PM
Taluntain
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-07-2009, 09:53 PM
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.
• 10-07-2009, 10:04 PM
Taluntain
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.
• 10-07-2009, 10:36 PM
angryboy
We can't help if you don't post your latest code.
also, use code tags.
• 10-08-2009, 07:43 AM
Taluntain
Code:

```// Calculate sum, product, difference, quotient and remainder import java.util.Scanner; import javax.swing.JOptionPane; public class upgraded {   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
• 10-08-2009, 08:33 AM
r035198x
Quote:

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.
• 10-08-2009, 08:37 AM
Taluntain
Quote:

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?
• 10-08-2009, 09:09 AM
r035198x
Quote:

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.
• 10-08-2009, 09:43 AM
Taluntain
Solved the problem. Required {} brackets after if close. thank you for your helps totally.