Hello everyone, I have a problem with an exercice, in which I have to use arrays and forward loop. Here is the question.
So here is what i've got so far: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
It doesn't seem to print out longest, and also im not sure that the logic is correct in there.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); } }
Thank you