1. This forum is in read-only mode.

Basic Java

Discussion in 'Computers & Modding' started by Blade5406, Jul 18, 2009.

  1. Blade5406

    Blade5406 Well-Known Member

    Could someone check this?
    I'm confused ??? :'(

    import java.io.*;
    public class act5
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader UInput = new BufferedReader(new InputStreamReader(System.in));
    int num;
    int num2;
    int num3;
    int s;
    int p;
    int d;
    double q;
    String tempNum;
    String tempNum2;
    String tempNum3;


    System.out.print("Enter the first integer: ");
    tempNum = UInput.readLine();
    num = Integer.parseInt(tempNum);
    System.out.print("Enter the second integer: ");
    tempNum2 = UInput.readLine();
    num2 = Integer.parseInt(tempNum2);
    System.out.print("Enter the third integer: ");
    tempNum3 = UInput.readLine();
    num3 = Integer.parseInt(tempNum3);
    s = num + num2 + num3;
    p = num * num2 * num3;
    d = num - num2 - num3;
    q = num / num2 /num3;

    System.out.println("The sum is " + s);
    System.out.println();
    System.out.println("The product is " + p);
    System.out.println();
    System.out.println("The difference is " +d);
    System.out.println();
    System.out.println("The quotient is " +q);


    }
    }

    I don't get how I should do the quotient part. I need help.
     
  2. The_Fox

    The_Fox Well-Known Member

    Ahhh coding, my first internet love.

    I refuse to actually do it for you or give you a hint (personal rule) you're obvious a beginning, which is cool, still good to see some people learning different languages instead of the C family and perl.

    There are plenty of really good e-books out there.

    A few my friends have wrote, I'll give him a bell later and grab it from him, if you want.
     
  3. Blade5406

    Blade5406 Well-Known Member

    If that's what you want. but my problem is, I can't get the quotient part of the code to work properly, the result is always = 0.0 .
     
  4. Loonylion

    Loonylion Administrator Staff Member

    why are you using strings to hold numbers? it should be int or float. Also check the variables in the quotient line.
     
  5. Blade5406

    Blade5406 Well-Known Member

    I'm just following what our instructor told us to do.
    The string input is converted to a number, I think.
     
  6. Loonylion

    Loonylion Administrator Staff Member

    that's just dumb of him then. you use the correct types for the data in question
     
  7. Blade5406

    Blade5406 Well-Known Member

    I'm really confused about this now, could you point out the incorrect ones?
     
  8. Loonylion

    Loonylion Administrator Staff Member

    you fixed the problem I saw.

    Why is q a double, when s, p, and d are all ints? If anything, p should be a double or long int. while q should really be a float.

    Can you give some example input/output figures?
     
  9. Blade5406

    Blade5406 Well-Known Member

    q(holds the value of a quotient value), when dividing numbers, you end up with those numbers(9 / 27 / 36 = 0.009259259259),
    the instructor said that we either use double or float regarding these kind of numbers

    I didn't do much with the s(sum), p(product), d(difference), because we're dealing with simple numbers.

    Input/output figures? the java.io figures?
     
  10. Loonylion

    Loonylion Administrator Staff Member

    double may not be able to hold decimal numbers (depends on the language, it could just be a long int). p needs to be a double because there's a strong possibility if you're using large numbers the result of the calculation could be higher than the upper boundary of the int datatype, in which case you will be given an incorrect answer.

    I'm assuming println does not format the output and just prints it as is.
     
  11. Blade5406

    Blade5406 Well-Known Member

    so float could work then? how do I use the "f" tag with the numbers used then?

    and yes, println doesn't format the output,

    It seems all I needed to do is to to change "int" to "double"

    int num;
    int num2;
    int num3;

    double num;
    double num2;
    double num3

    these variables receive the converted string...,it'll have to do for the time being.

    EDIT: Sorry for the double post. and thanks for the help.

    Note: I might be updating this thread when I'll have another problem.
     
  12. nomercy

    nomercy Well-Known Member

    Do you now know why you had to change the int to double?

    Say you have int a and int b. If you do: a / b, then it's (int / int) and Java (and any other language) in an attempt to maintain some speed decides you want a fast integer division, instead of a slow float or double division.

    So if you do 9 / 3, there's no reason to make it float, as the answer is int. But if you do 1 / 2, the result is also an integer (whole number). So 1 / 2 in int becomes 0, and 3 / 2 becomes 1. They're rounded.
     
  13. Blade5406

    Blade5406 Well-Known Member

    Could someone point me to a very good and detailed java tutorial?
     
  14. meganova

    meganova Well-Known Member

    Psst Blade..
    I know how to multiply or subtract or etc using Java.
     
  15. Blade5406

    Blade5406 Well-Known Member

    You know a code that can result to this:
     
  16. meganova

    meganova Well-Known Member

    Oh.. -_-"

    What about the "prompt" then.
     
  17. Blade5406

    Blade5406 Well-Known Member

    prompt?