Results 1 to 17 of 17
Thread: Really Basics
- 10-07-2009, 08:07 PM #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 class upgraded
{
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;
System.out.print( "Enter your first number madafaka: " );
x = input.nextInt();
System.out.print( "Enter your SECOND number madafakaaa: " );
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.
- 10-07-2009, 08:37 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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 #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?
- 10-07-2009, 08:50 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You do know what initialize means, right?
Assign an initial value.
- 10-07-2009, 08:53 PM #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.
- 10-07-2009, 09:10 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
.Java Code:sum = 0;
- 10-07-2009, 09:15 PM #7
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
- 10-07-2009, 09:20 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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 #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-07-2009, 09:53 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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 #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.
- 10-07-2009, 10:36 PM #12
We can't help if you don't post your latest code.
also, use code tags.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 10-08-2009, 07:43 AM #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 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 #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-08-2009, 08:37 AM #15
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
- 10-08-2009, 09:09 AM #16
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 10-08-2009, 09:43 AM #17
Member
- Join Date
- Oct 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Help in basics of database..
By vinoth in forum JDBCReplies: 5Last Post: 09-08-2009, 07:01 PM -
About swing basics
By harithaspa in forum AWT / SwingReplies: 2Last Post: 12-13-2008, 12:07 PM -
Basics of Servlet
By pcvardhan in forum Advanced JavaReplies: 1Last Post: 06-13-2008, 02:07 PM -
Basics
By AKP in forum New To JavaReplies: 7Last Post: 05-23-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks