Results 1 to 6 of 6
  1. #1
    simar is offline Member
    Join Date
    Aug 2014
    Posts
    27
    Rep Power
    0

    Default flush and try catch

    in below prog
    1. why we use try and catch what kind of exception id thrown in try block
    and in catch what is I/O exception and wat is e and also tel me why we
    systm .out.prntln("I/O error"); and what is system .exit(1); y we use exit 1....???
    2.y we use flush method what does it work for...??
    3. y we use principalamount.floatValue in below line
    float value = loan(principalamount.floatValue(),interestrate.flo atValue(),numyear);
    Java Code:
    import  java.io.*;
    class invest
    {
    public static void main (String args[])
    {
    Float principalamount = new Float(0);
    Float interestrate = nw Float(0);
    int numyear = 0;
    try
    {
    DataInputStream in = new DataInputStream(System.in);
    System.out.print("enter principalamount: ");
    System.out.flush();
    String principalstring = in.readLine();
    principalamount = Float.valueOf(principalstring);
    System.out.print("enter interestrate");
    System.out.flush();
    String intereststring = in.readLine();
    interestrate = Float.valueOf(intereststring);
    System.out.print("Enter numyear:");
    String yearstring = in.readLine();
    }
    catch (IOException e)
    {
    System.out.println("I/O error");
    System.exit(1);
    }
    float value = loan(principalamount.floatValue(),interestrate.floatValue(),numyear);
    printline();
    System.out.println("final value = " +value);
    printline();
    }
    static float loan(float p,float r,int n)
    {
    int year = 1;
    float sum = p;
    while (year <= n)
    {
    sum = sum * (1+r);
    year = year + 1;
    }
    return sum;
    }
    static void printline()
    {
    for(int i = 1; i <= 30; i++)
    {
    System.out.print("=");
    }
    System.out.println(" ");
    }
    }
    Last edited by simar; 09-04-2014 at 02:57 PM.

  2. #2
    simar is offline Member
    Join Date
    Aug 2014
    Posts
    27
    Rep Power
    0

    Default Re: flush and try catch

    even it gives wrong output it should be 8745.03but it gives 5000.00

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,496
    Rep Power
    6

    Default Re: flush and try catch

    1) in this code: nothing really. But if you make DataInputStream point to a socket over the internet, the network connection might just drop while the code is executing

    2) in this code: nothing really. But if you use classes which buffer data, flush() will make sure that any data still in the buffer is sent to where it needs to go

    3) This is not even your code, is it?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,824
    Blog Entries
    7
    Rep Power
    21

    Default Re: flush and try catch

    Quote Originally Posted by simar View Post
    even it gives wrong output it should be 8745.03but it gives 5000.00
    Does the code even compile? It even contains html code (which is definitely not Java). Please show us the code that runs for you, not some arbitrary gibberish,

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    simar is offline Member
    Join Date
    Aug 2014
    Posts
    27
    Rep Power
    0

    Default Re: flush and try catch

    i compile this code sr. i m following a book on which it is given its given under wrraper class so i m nw one i dont know the meaning of it if u people hlp me its vry thankful so plz i hav problm with
    why we use try and catch what kind of exception is thrown in try block
    and in catch what is I/O exception and wat is e and also tel me why we use
    systm .out.prntln("I/O error"); and what is system .exit(1);

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,266
    Rep Power
    20

    Default Re: flush and try catch

    Please don't use txt spk.
    It makes understanding what you're asking that much harder.

    Also, can you wrap your code in [code] tags [/code] so it retains its formatting.
    That lot in your first post is also hard to follow.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. how to flush the buffer?
    By oszc in forum New To Java
    Replies: 2
    Last Post: 08-30-2011, 10:37 AM
  2. OutputStream.flush() problem
    By Godjikung in forum Advanced Java
    Replies: 2
    Last Post: 01-29-2011, 08:48 PM
  3. GZIPOutputstream flush
    By Nardian in forum Networking
    Replies: 4
    Last Post: 09-25-2010, 05:09 PM
  4. How to flush gui from using old data
    By ShinTec in forum AWT / Swing
    Replies: 1
    Last Post: 06-10-2010, 02:18 PM
  5. flush method
    By bugger in forum New To Java
    Replies: 0
    Last Post: 11-11-2007, 08:39 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
  •