Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Java Basics

  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java Basics

    Code:
    import java.io.*;//Ask what for or when to use
    
    public class SAMPLE //Ask what for when to use
    {
    
         public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
         public static BufferedReader input = new BufferedReader(reader); //for input/output            
         public static void main (String [] args) //constant statement in java what for?
         throws Exception{ //for num1=Integer.parseInt(input.readLine());
    hey whats up! hi im new to java. can you please help me understand what these codes are used for.

    A.)
    Code:
    public class SAMPLE
    what does public class mean?

    All i know is the "SAMPLE" statement. It is the filename of the program and it CASE SENSITIVE

    B.)
    Code:
    import java.io.*;
    I heard this was a library? i also heard aboutjava.util, java.swing. Can you please explain to me the use of these libraries. Is there a website containing all possible libraries in java which shows their complete function.

    C.)
    Code:
    throws Exception
    Whats this code for? when is it used?

    D.)
    Code:
    public static void main (String [] args)
    I heard this is a constant statement in java. Can someone please tell me whats this statement for

    E.)
    Code:
    public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
         public static BufferedReader input = new BufferedReader(reader); //for input/output
    Someone told me these codes are used if you have an input/output
    in your program. Can someone explain word for word the purpose of these codes

    THANKS in advance =)
    by the way this is kind of silly should i call these block of sentences CODES OR A STATEMENT

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I'll move the thread to the JAVA Forum
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by jpnv8 View Post
    Code:
    import java.io.*;//Ask what for or when to use
    
    public class SAMPLE //Ask what for when to use
    {
    
         public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
         public static BufferedReader input = new BufferedReader(reader); //for input/output            
         public static void main (String [] args) //constant statement in java what for?
         throws Exception{ //for num1=Integer.parseInt(input.readLine());
    hey whats up! hi im new to java. can you please help me understand what these codes are used for.

    A.)
    Code:
    public class SAMPLE
    what does public class mean?

    All i know is the "SAMPLE" statement. It is the filename of the program and it CASE SENSITIVE

    B.)
    Code:
    import java.io.*;
    I heard this was a library? i also heard aboutjava.util, java.swing. Can you please explain to me the use of these libraries. Is there a website containing all possible libraries in java which shows their complete function.

    C.)
    Code:
    throws Exception
    Whats this code for? when is it used?

    D.)
    Code:
    public static void main (String [] args)
    I heard this is a constant statement in java. Can someone please tell me whats this statement for

    E.)
    Code:
    public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
         public static BufferedReader input = new BufferedReader(reader); //for input/output
    Someone told me these codes are used if you have an input/output
    in your program. Can someone explain word for word the purpose of these codes

    THANKS in advance =)
    by the way this is kind of silly should i call these block of sentences CODES OR A STATEMENT
    After I'm done explaining most of this, I suggest that you look at this official java tutorial:

    Sun Tutorial

    Now for your questions

    A) public class is the beginning of any class in Java. public means that the class is public to anybody. So users can use the class and see it outside of the class statement. class is just stating that we are declaring a class

    B) http://java.sun.com/javase/6/docs/api/

    That is the API used for java. It contains all java libraries and classes that one can use from the default libraries.

    java.io.* means that we are importing from standard java, in the io package, and everything inside of it.

    C) Throws Exception is a way to tell the program that it is possible that this class / method will throw an exception. Typically this way is frowned upon as it make more sense to use a try catch block to catch exceptions and handle them in the way you want. For more information, see the tutorial I pointed to.

    D) This statement is used to tell java where the main part of our program is. That is, what code java should execute first.

    E)
    Code:
    public static InputStreamReader reader = new InputStreamReader(System.in); //for input/output
         public static BufferedReader input = new BufferedReader(reader); //for input/output
    Sure! What's going on is the first thing you do is create a streamreader with an input device of System.in.

    typically System.in is the keyboard, but there are many different types of streams that can be inputted into here. However, if we want keyboard input, System.in is what we want!

    We want an InputStreamReader because we need it to be a stream that we can read input from.

    We then place this reader into a BufferedReader in order to allow for some type of easy interface with the stream. Once again, more details can be read in the tutorial.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • Users who have thanked Aradon for this post:

    Kor (03-25-2008)

  • #4
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thx bro

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
    num1=  Float.parseFloat(input.readLine());

    //num1 --->(the variable)
    //Integer.parseInt---> (data type)
    //Double.parseDouble , Character.parsechar (other data types)
    //(input.readLine()); /**ask what for**/


    what does (input.readLine()) and parse, means? parseInt?


    thanks in advance

  • #6
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    parse takes one thing and attempts to turn it into another. Each of the number types has a parse method to attempt to turn a string in a number. They will throw a NumberFormatException if they fail.

    Integer.parseInt
    Float.parseFloat
    Long.parseLong
    Double.parseDouble

    Consider:
    Code:
    int i1 = Integer.parseInt("1234"); //i1 now equals 1234
    int i2 = Integer.parseInt("1234.45"); //throw NumberFormatException because integers cannot contain decimals
    
    double d1 = Double.parseDouble("1234"); //d1 now equals 1234.0
    double d2 = Double.parseDouble("1234.45"); //d2 now equals 1234.45

  • #7
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    As a last note

    input.readLine() just takes the BufferedReader that we created through Standard In (typically the keyboard) and grabs a line from it.

    So in our case, it would be user input. The return is a string, thus why it needs to be parsed into it's appropriate function as exampled above this post by sobrien79.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sobrien79 View Post
    parse takes one thing and attempts to turn it into another. Each of the number types has a parse method to attempt to turn a string in a number. They will throw a NumberFormatException if they fail.

    Integer.parseInt
    Float.parseFloat
    Long.parseLong
    Double.parseDouble

    Consider:
    Code:
    int i1 = Integer.parseInt("1234"); //i1 now equals 1234
    int i2 = Integer.parseInt("1234.45"); //throw NumberFormatException because integers cannot contain decimals
    
    double d1 = Double.parseDouble("1234"); //d1 now equals 1234.0
    double d2 = Double.parseDouble("1234.45"); //d2 now equals 1234.45
    so (input.readLine()); is the command to store the 1234?

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    input.readLine() says to the user, give me something to work with. Then the user enters in something and the program goes on from there.


  •  

    Posting Permissions

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