Results 1 to 17 of 17

Thread: Really Basics

  1. #1
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default 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 09:09 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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. #3
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    yes i read it but the thing is i didnt get the initalizing part, how to initialize them?

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You do know what initialize means, right?
    Assign an initial value.

  5. #5
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    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. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Java Code:
    sum = 0;
    .

  7. #7
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by Taluntain View Post

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

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Inside an if block. The compiler is smart enough to realize that the block may never be executed so it complains anyway.

  9. #9
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    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. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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. #11
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    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. #12
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    We can't help if you don't post your latest code.
    also, use code tags.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  13. #13
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    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

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Taluntain View Post
    .... 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. #15
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    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. #16
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Taluntain View Post
    ... 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. #17
    Taluntain is offline Member
    Join Date
    Oct 2009
    Posts
    9
    Rep Power
    0

    Default

    Solved the problem. Required {} brackets after if close. thank you for your helps totally.

Similar Threads

  1. Help in basics of database..
    By vinoth in forum JDBC
    Replies: 5
    Last Post: 09-08-2009, 08:01 PM
  2. About swing basics
    By harithaspa in forum AWT / Swing
    Replies: 2
    Last Post: 12-13-2008, 01:07 PM
  3. Basics of Servlet
    By pcvardhan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 03:07 PM
  4. Basics
    By AKP in forum New To Java
    Replies: 7
    Last Post: 05-23-2008, 01:06 PM

Posting Permissions

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