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 3 of 3

Thread: Array and FOR

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array and FOR

    Hello everyone, I have a problem with an exercice, in which I have to use arrays and forward loop. Here is the question.
    As part of an English assignment, Jonathan is supposed to use 10 words and then figure out which of them is the longest
    word. Help him by writing a program that accomplishes this. Items in red indicates user input.
    SAMPLE OUTPUT
    Enter 10 words, Jonathan :
    Singing
    Dancing
    Copenhagen
    Denmark
    Computers
    Apple
    Pineapple
    Music
    Director
    Cinema
    The longest word in the list is: Copenhagen
    So here is what i've got so far:
    class ArrayAssignement
    {
    public static void main (String []args)
    {
     
    String word[] =new String[10]; //what will store the 10 words
    String longest="";
     
    System.out.println("Enter 10 words: "); //prints out enter 10 words
     
     
    for (int position=0; position<word.length; position++) //position is 0, as long as position is smaller then the length of the biggest word, counter+1
    {
    	word[position]=TextIO.getWord();	//accepts the user input
    }
     
     
    for(int counter=0; word[counter].length()<word[counter+1].length(); counter++)
    {	
    	word[counter]=longest;
    }	
     
    System.out.println("the longest word is "+longest);
     
    }
    }
    It doesn't seem to print out longest, and also im not sure that the logic is correct in there.
    Thank you


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array and FOR

    It doesn't seem to print out longest,
    Can you post what it does print out and explain what is wrong with it?

    im not sure that the logic is correct
    Can you explain what your logic is? You'll need to have working logic BEFORE you try to write any code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Array and FOR

    yep ur logic is correct....
    you made a minor mistake...

    you should assign a value to variable/field...i.e., variable=value
    but in ur code....u wrote word[counter]=longest;
    it means your are giving "null" to word[counter]....

    correct line must be:: longest=word[counter];


    also one more thing....you didnt specify end point to the for loop,so it u'll get an arrayIndexOutOfBounds exception...
    so write code like this....

    longest=word[0];counter=0;
    while(counter <10)
    {
    if(word[counter].length()<word[counter+1].length()) {longest=word[counter];}
    counter++;
    }
    hope it helps

Similar Threads

  1. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  2. Replies: 6
    Last Post: October 7th, 2011, 07:50 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM
  5. 2d (4x4) array insdie a 1d array. (Block cipher)
    By fortune2k in forum Collections and Generics
    Replies: 13
    Last Post: November 23rd, 2010, 05:29 PM