Results 1 to 5 of 5
  1. #1
    Honijahu is offline Member
    Join Date
    Feb 2013
    Posts
    4
    Rep Power
    0

    Default Is there an order in which the Java codes need to be placed? viewed from top to botto

    I am studying a tutorial and there was an excercise concerning Loops. I found out that if I placed my code like this it works:

    package Userinput;
    import java.util.Scanner;
    public class Loops {

    public static void main (String arg []){

    int loopVal;
    int end =11;
    int answer=0;
    int calculation=0;
    Scanner read = new Scanner(System.in);
    System.out.println("which table do you want?");
    calculation = read.nextInt();

    for(loopVal=1;loopVal<end;loopVal++){

    answer = loopVal*calculation;
    System.out.println(loopVal +" times "+calculation+" = "+answer);
    }



    }
    }

    But if I placed it like this it doesn't work:

    package Userinput;
    import java.util.Scanner;
    public class Loops {

    public static void main (String arg []){

    int loopVal;
    int end =11;
    int answer=0;
    int calculation=0
    calculation = read.nextInt();
    Scanner read = new Scanner(System.in);
    System.out.println("which table do you want?");

    for(loopVal=1;loopVal<end;loopVal++){
    answer = loopVal*calculation;
    System.out.println(loopVal +" times "+calculation+" = "+answer);
    }



    }
    }

    So I was wondering is there an order in which the Java codes need to be placed? e.g. first give datatypes (int, double etc) and then Scanner codes etc. etc.
    Does anybody know?

    thanks Honijahu

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

    Default Re: Is there an order in which the Java codes need to be placed? viewed from top to b

    Of course it doesn't work because you forgot a semi-colon in your second version (so it doesn't even compile).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Is there an order in which the Java codes need to be placed? viewed from top to b

    Hi Honijahu, welcome.

    To answer your question, the order doesn't matter as long as the variable is declared before it is used and is in scope of where it it being used. By scope I'm refering to variables being declared in another method for example.

    int calculation = read.nextInt();

    The above is also a valid statement. I would not necessarily apply the same thinking to variables with loops as this would mean you are redeclaring the variable each time it is being called.

    Regards.

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

    Default Re: Is there an order in which the Java codes need to be placed? viewed from top to b

    Quote Originally Posted by Ronin View Post
    To answer your question, the order doesn't matter as long as the variable is declared before it is used and is in scope of where it it being used. By scope I'm refering to variables being declared in another method for example.
    See the following counter example:

    Java Code:
    public class T {
    
    	public static void main(String[] args) {
    		
    		System.out.println(x);
    	}
    	
    	private static int x= 42;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    335
    Rep Power
    4

    Default Re: Is there an order in which the Java codes need to be placed? viewed from top to b

    I see the confusion.
    The position in this case is irelevant as the variable x is declared and initialized prior to its use. I should have added that although it looks like it is declared after its use just by looking at the code, is not how the JVM understands it.

    For the OP I was looking at it from a beginners view.

    Java Code:
    System.out.println(x);
    int x = 11
    Java Code:
    int x = 11;
    System.out.println(x);
    The first section of code is invalid whist the second works.

    Regards.

Similar Threads

  1. Replies: 0
    Last Post: 02-22-2011, 10:55 AM
  2. where java codes to be run?
    By samira_fk222 in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 09:37 PM
  3. How load other java codes
    By pizzadude223 in forum Java Applets
    Replies: 35
    Last Post: 07-22-2010, 04:09 PM
  4. java codes
    By Balajee in forum AWT / Swing
    Replies: 1
    Last Post: 09-30-2008, 05:04 PM
  5. Replies: 3
    Last Post: 07-06-2008, 12:35 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
  •