-1

I am doing some programming for my Java school work. Yesterday I posted a question which is quite similar to this one regarding arrays and I managed to get it solved.

This time round, I am required to prompt the user to enter a series of words regarding of the number and from there the application will determine the longest word of all and print to console stating the longest word as well as the length of it.

Although, we weren't given any hint on how to go about it but I thought that by using vector could be the only solution. As for now, I only manage to print to console no matter how many words the user input but I have no idea how to be able to add each individual to the vector and compare them regarding their length.

I am a total beginner in programming.

import java.util.*;

class LongestWord2 {
    public static void main(String [] args) {               
        System.out.println("Please enter your words");
        Scanner userInput = new Scanner(System.in);
       
        Vector <String> v = new Vector <String>();
    
        while (userInput.hasNext()) {
            v.add(userInput.next());
            System.out.println(userInput.next());
            System.out.println(v.get(0));
        }   
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
alan_ogz83
  • 73
  • 1
  • 2
  • 10

4 Answers4

2

Here is the simplest way of doing this. No need to store the words in a vector.

 import java.util.*;

class LongestWord2 {
    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);

        String longestWord = "";
        String temp = "";
        int longestWordLength = 0;
        int numOfWords = 10; // num of words to ask from user
            System.out.println("Please enter " + numOfWords + " words");
        for (int i = 0; i<numOfWords ; i++) { // loop for taking words as input
            temp = userInput.next();
            if(temp.length() > longestWordLength){
                longestWordLength = temp.length();
                longestWord = temp;
            }
        }
        System.out.println("Longest Word = " + longestWord);
        System.out.println("Longest Word Length = " + longestWordLength);
        userInput.close();
    }
}

Sample Output:

Please enter 10 words
a
ab
abc
abcd
abcde
abcdef
abcdefg
abcdefgh
abcdefghi
abcdefghij
Longest Word = abcdefghij
Longest Word Length = 10
4aRk Kn1gh7
  • 4,259
  • 1
  • 30
  • 41
2

If you want to continue getting response from your scanner and keep checking for longest name then you can use the one that I implement in your last question with some changes.

sample:

System.out.println("Please enter your words");
    Scanner userInput = new Scanner(System.in);

    Vector<String> v = new Vector<String>();
    String longest = "";
    longest = userInput.nextLine(); //get the first array of words for checking
    v.add(longest);

    while (true) {

        for(String s : v) //iterate to all the array of words
        {
            if(longest.length() < s.length()) //check if the last longest word is greater than the current workd
                longest = s; //if the current word is longer then make it the longest word
        }
        System.out.println("Longest Word: " + longest + " lenght: " + longest.length());

        v.add(userInput.nextLine());
    }
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • hey thanks Rod! however, the compilation leaves me with.... Please enter your words one two three four thousand Longest Word: one length: 3 Longest Word: one length: 3 Longest Word: three length: 5 Longest Word: three length: 5 Longest Word: thousand length: 8 – alan_ogz83 Jun 26 '14 at 08:22
  • @alan_ogz83 updated above see edit – Rod_Algonquin Jun 26 '14 at 08:25
  • :) it actually calculates the total length of all the words lol. – alan_ogz83 Jun 26 '14 at 08:30
  • is there any way where we can calculate individual tokens? thanks! – alan_ogz83 Jun 26 '14 at 08:39
  • @alan_ogz83 you want to displace all the data?? – Rod_Algonquin Jun 26 '14 at 08:39
  • erm the requirements for this tutorial work is like the previous one but just that this time round the user can input any number of words and regardless of the number of words, the application will pinpoint the longest word between them and display the word itself as well as the length of it. – alan_ogz83 Jun 26 '14 at 08:43
  • @alan_ogz83 from what i understand you want to print each time the words that was saved? and also the longest word – Rod_Algonquin Jun 26 '14 at 08:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56328/discussion-between-alan-ogz83-and-rod-algonquin). – alan_ogz83 Jun 26 '14 at 08:46
  • hey hi mate, I have a new question relating to do while loop. if you are free, would you mind advising me on it? thanks again mate. http://stackoverflow.com/questions/24474905/java-se-do-while-loop-issues – alan_ogz83 Jun 29 '14 at 09:50
1

Your question is confusing, but I think this

while (userInput.hasNext())
{
  v.add(userInput.next());
  System.out.println(userInput.next());
  System.out.println(v.get(0));
}

Is intended to be more like,

while (userInput.hasNext())
{
  String line = userInput.next();
  line = (line != null) ? line.trim() : "";
  v.add(line);
  // System.out.println(userInput.next());
  // System.out.println(v.get(0));
  System.out.printf("'%s' is %d letters long%n", line, line.length());
}

And based on your question, that should be enough to get you started.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

If you just need to know the longest word, why not store the longest word so far from the user input in a variable? You do not need a vector.

jmac
  • 113
  • 1
  • 7