Timmy
Timmy

Reputation: 67

Why is this returning blank?

This pulls from a word bank our teacher gave us, and I'm supposed to return the longest word that includes only characters from the top row of the keyboard. Currently it returns blank. Please help.

//What's the longest word only using the top row of the keyboard?      
   public static void Question6() {  

      String longestWordSoFar = " ";
      System.out.println("Question 6:");
      for(int i = 1; i < WordList.numWords(); i++) // check every word in wordlist
      {
          if(topRow(WordList.word(i))) { // if the length is greater than the previous word, replace it
          {  
              if(WordList.word(i).length() > longestWordSoFar.length())            
                  longestWordSoFar=WordList.word(i);
              }      
          }

      }
      System.out.println("longest word including top row: " + longestWordSoFar);
      System.out.println();
      return;
   }

public static boolean topRow(String word) {

       for(int i = 0; i < word.length(); i++) {
       //return true if the word has all of the letters in the top row of the keyboard
               if (word.charAt(i) != 'q') {
               return false;
               }
               if (word.charAt(i) != 'w') {
               return false;
               }
               if (word.charAt(i) != 'e') {
               return false;
               }
               if (word.charAt(i) != 'r') {
               return false;
               }
               if (word.charAt(i) != 't') {
               return false;
               }
               if (word.charAt(i) != 'y') {
               return false;
               }
               if (word.charAt(i) != 'u') {
               return false;
               }
               if (word.charAt(i) != 'i') {
               return false;
               }
               if (word.charAt(i) != 'o') {
               return false;
               }
               if (word.charAt(i) != 'p') {
               return false;
               }   
        }
        return true;
   }

Upvotes: 0

Views: 111

Answers (5)

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

You function topRow does not do what you want. It will return false if any of the characters in the word is not q,w,e,r,t,y,u,i,o and p at the same time. This will never be true.

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234715

Use a regular expression instead. If s is a String type then use

s.matches("[qwertyuiop]+")

which matches one or more letters on the top keyboard row. I'll leave case insensitivity to you.

p.s. bet the answer is "typewriter".

Upvotes: 5

ilmirons
ilmirons

Reputation: 664

Am I wrong or your function always return at first char?

Upvotes: 1

grexter89
grexter89

Reputation: 1102

Your topRow() method would always return false, because every character of your word must be every character of the top row at the same time (That's not possible) in order to return true. Try using regex.

Upvotes: 1

florent
florent

Reputation: 801

Let's take this lines

if (word.charAt(i) != 'q') {
   return false;
}
if (word.charAt(i) != 'w') {
   return false;
}

Now, what happen if the current character ( word.charAt(i) ) is 'q'? what happen if it's 'w'? what happens in any other case?

Upvotes: 1

Related Questions