Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Procedural programming veteran, Java OO newbie question, first time poster

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Procedural programming veteran, Java OO newbie question, first time poster

    The output of this program is:

    line 13
    line 18
    line 20
    ... and then it hangs. Why does it never get to 23? FYI, this code came from "Head First Java", chapter 3, Code Magnets puzzle (probably not actually intended to run)

    Kind of embarrassed that I'm not seeing anything wrong with this simple program...

    Thanks,
    Kevin

    class TestArrays {
     
    	public static void main(String [] args) {
     
    		int []  index = new int[4];
     
    		index[0] = 1;   // Figi
    		index[1] = 3;  // Cozumel
    		index[2] = 0;  // Bermuda
    		index[3] = 2;  // Azores  
     
    		String [] islands = new String[4];
    System.out.println("line 13");
    		islands[0] = "Bermuda"; // 3
    		islands[1] = "Fiji";    // 1
    		islands[2] = "Azores";  // 4
    		islands[3] = "Cozumel"; // 2 
    System.out.println("line 18");
    		int y = 0;
    System.out.println("line 20");
    		int ref;
    		while (y < 4); {
    System.out.println("line 23");
    			ref = index[y];
     
    			System.out.print("island = ");
    			System.out.println(islands[ref]);
    			y = y + 1;
    		}
    System.out.println("line 30");
    	}
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Procedural programming veteran, Java OO newbie question, first time poster

    Check your semi-colons on the while loop. You're short-circuiting your loop.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    4
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Procedural programming veteran, Java OO newbie question, first time poster

    Thanks! I am so not used to ending statements with a semi-colon, and here I did it to excess! You're not really a curmudgeon after all

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Procedural programming veteran, Java OO newbie question, first time poster

    You're welcome!

Similar Threads

  1. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2012, 11:41 PM
  2. New to programming...veteran fisherman
    By Sundancefisher in forum Member Introductions
    Replies: 1
    Last Post: June 17th, 2012, 07:34 AM
  3. Newbie Java Question
    By tinkersp in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 23rd, 2011, 01:41 PM
  4. Java and programming newbie needing help
    By speedmaster in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 8th, 2010, 06:57 AM
  5. Java Programming Help - thanks ahead of time
    By javanerd in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 28th, 2010, 04:14 PM