Closed Thread
Results 1 to 8 of 8

Thread: The use of "if" statements

  1. #1
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    The use of "if" statements

    I'm completely new to programming and don't have a complete understanding of the syntax of java though I am slowly grasping it. I was wondering how if, else, and else if statements need to be written and formatted in java ? If people could post some simple code to demonstrate as well. Also how to get input in java?

    Thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Generic is offline Newbie
    Join Date
    Feb 2010
    Posts
    26
    Blog Entries
    2
    Rep Power
    0

    Re: The use of "if" statements

    Code:
        public static void main(String[] args) { // Our code starts here.
            int i = 0; // Declare variable "i" as 0.
            if (i == 0) { // if "i" is equal to 0
                
            } else if (i == 1) { // if "i" is equal to 1
                
            } else { // if none of the other conditions are met
                
            }
        }
    is equivalent to
    Code:
        public static void main(String[] args) { // Our code starts here.
            int i = 0; // Declare variable "i" as 0.
            
            if (i == 0) { // if "i" is equal to 0
            } else {
                if (i == 1) { // if "i" is equal to 1
                    
                } else { // if none of the other conditions are met
                    
                }
            }
        }
    The first being far more legible making else if statements more advantageous.

  4. #3
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: The use of "if" statements

    input through command line programs is with for example the java.util.Scanner class, instanciated with System.in as parameter
    Code:
    java.util.Scanner scn = new java.util.Scanner(System.in);
    String myStringVar = scn.nextLine();
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  5. #4
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: The use of "if" statements

    Cool. Thanks. I figured the if statements out but I'm still working on getting the hang of the input statements. Thank you

  6. #5
    sourlemon's Avatar
    sourlemon is offline Programmer
    Join Date
    Nov 2008
    Posts
    101
    Rep Power
    0

    Re: The use of "if" statements

    There are two ways (that I use) to read input with Scanner, by reading one word at a time or one line at a time. The Scanner object help you do this.

    When you want to read an input, you first "declare" so:
    Code:
    java.util.Scanner scan = new java.util.Scanner(System.in);
    To read a line you use: scan.nextLine() which will return a string. A line is indicated by the character '\n' (aka the enter key).

    Code:
    String line = scan.nextLine();
    If you want to read a word, then you use scan.next() which will also return a string. Word can be separated by a space or new line character.

    Code:
    String word = scan.next();
    To read more than one word or more than one line, just repeat the code. Example:

    Code:
    String word1 = scan.next();
    String word2 = scan.next();
    String word3 = scan.next();
    Same with line. Hopefully that explained it a bit more. Good luck!

  7. #6
    agnl666's Avatar
    agnl666 is offline Programmer
    Join Date
    Feb 2010
    Location
    Halifax
    Posts
    163
    Blog Entries
    2
    Rep Power
    0

    Re: The use of "if" statements

    Ya, thanks. If I wanted to read an int as opposed to a string would I simply put "scan.nextInt();" ?

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: The use of "if" statements

    that's true. but, to make sure it's an int coming, you should check it with "scan.hasNextInt();" which returns a boolean if there is an in int scanned.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  9. #8
    G1TN3RD is offline Newbie
    Join Date
    Jan 2010
    Location
    College Station, TX.
    Posts
    8
    Rep Power
    0

    Re: The use of "if" statements

    Ima just do both, even though you have one already.
    Code:
    int number = 0;
    if(number==0)
    {
        System.out.println("The number is equal to zero");
    {
    else
    {
        System.out.println("The number is not equal to zero");
    }
    And the input is depressed to the to .in
    For ex. if you are reading a text file, and you need to capture some data from the text file and make it input for java...
    Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class insertnamehere
    {
        public static void main (String[] args) throws IOException
        {
            Scanner in = new Scanner (new File("whatever your file name is here"));
           in.NextLine();         <------ whatever was on the first line of that data file, has now been saved as data in java...
    Or you can declare something with the input statement
    Code:
    String line = in.nextLine();
    hope that helps =)

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MASM How To Use "Open" And "Save As" (Win32 API) ?
    By RhetoricalRuvim in forum Assembly
    Replies: 12
    Last Post: 08-13-2011, 04:10 PM
  2. "\r\n" are for some reason turned into "\r\n\r\n"; what's wrong?
    By RhetoricalRuvim in forum Ruby Programming
    Replies: 1
    Last Post: 08-06-2011, 05:55 PM
  3. Alex G. CSS "Sexy Buttons" - what about "submit on 'ENTER'" ?
    By shackrock in forum JavaScript and CSS
    Replies: 7
    Last Post: 12-05-2010, 04:57 PM
  4. Replies: 2
    Last Post: 11-01-2010, 11:52 PM
  5. Help - Including "if" statements into email $message
    By ghost2012 in forum PHP Development
    Replies: 5
    Last Post: 11-03-2009, 02:57 PM

Tags for this Thread

Bookmarks

Posting Permissions

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