Results 1 to 6 of 6
  1. #1
    jenniferrlie is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default java homework help

    im working on some java homework. i was wondering if anyone can help me out. im trying to write a program that prompts the user to enter a decimal number and then outputs this number rounded to two decimal places.

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

    Default

    Read about System.out.printf.

  3. #3
    jenniferrlie is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default will you check my code please and tell me if its right

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.text.NumberFormat;

    public class CH3EX1{
    public static void main(String[] args) throws IOException{
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Please enter a decimal number.");
    String input = console.readLine();
    double decimal = Double.parseDouble(input);
    NumberFormat formatter = NumberFormat.getNumberInstance();
    formatter.setMaximumFractionDigits(2);
    formatter.setMinimumFractionDigits(2);
    System.out.println("Result:");
    System.out.println(formatter.format(decimal));
    System.exit(0);
    }
    }

  4. #4
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    i think you can do it like this but not sure, for you to figure out how...

    rounding double to two decimal places

    u can use the import java.util.Scanner; function to give your user the right to enter someting and read it.

    have fun,

    Dieter

  5. #5
    Dieter's Avatar
    Dieter is offline Member
    Join Date
    Sep 2009
    Posts
    91
    Rep Power
    0

    Default

    ow your in a console app, my bad then.

    We don't use that at school. We use the System.out.println

    my bad then

    all the best,

    Dieter

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

    Default

    Yep your code is right, well done.

    These days you can use the Scanner class to get input from the user though.

Similar Threads

  1. Having trouble with java homework...
    By purinlove88 in forum New To Java
    Replies: 3
    Last Post: 03-02-2009, 03:02 PM
  2. Java homework please
    By Indulgence in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 02:48 AM
  3. LF: Homework help with Java
    By excurssion in forum New To Java
    Replies: 2
    Last Post: 10-17-2008, 06:00 AM
  4. I need help with my java servlet homework(average)
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-23-2007, 09:57 PM
  5. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 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
  •