2

I am new to Java and I need to solve an exercise that requires the following:

For a string that the user inputs, return the largest word (so far so good here)

But, it should take into account that numbers and symbols are not words.

For example: Hello, my name is Jonathan and my phone is 0123456789 should return "Jonathan" as longest word.

Example 2: H3llo W0rld, Java 1s fun!, should return "Java" as longest word.

In my pseudo-code I thought about the following:
A) Request input from user.
B) While input invalid, loop.
C) Scan the input
D) Separate the words by the spaces, or tokens.
E) If Word contains just letters A-Z && a-z do the following code:
e1) Check for longest word, with a for.
e2) Print result
F) else if: move to the next word.

Here is the code that I have so far. As you see, many parts are taken from here and adapted to the code. The "Find the longest in array" is a popular subject, yet I am not able to do the checking for the alphabetical characters. Any help will be welcomed.

The problem is that I don't know how to implement the part that checks for letters.

Code:

package longestword; import java.util.Scanner; public class LongestWord { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a String: "); //looping for non empty strings int largestLength = 0; String largestWord = ""; String userinput = sc.nextLine(); while (userinput.equals("")) { System.out.println("Please insert a String: "); userinput = sc.nextLine(); } for (String b : userinput.split(" ")) { if (largestWord.length() == 0) { largestLength = b.length(); largestWord = b; } else if (b.length() >= largestLength) { largestLength = b.length(); largestWord = b; } } sc.close(); System.out.println("Longest Word: " + largestWord); } public boolean isAlpha(String userinput) { char[] chars = userinput.toCharArray(); for (char c : chars) { if (!Character.isLetter(c)) { return false; } } return true; } } /* char c = '*'; if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) System.out.println(c + " is an alphabet."); else System.out.println(c + " is not an alphabet."); } } */ 
2
  • 1
    Why don'y you use your isAlpha method? Commented Mar 25, 2019 at 7:57
  • I am trying to, just didn't know how to implement it Commented Mar 25, 2019 at 8:21

2 Answers 2

1

Just change your isAlpha method to static and use it as follows:

for (String b : userinput.split(" ")) { if (isAlpha(b)) { if (largestWord.length() == 0) { largestLength = b.length(); largestWord = b; } else if (b.length() >= largestLength) { largestLength = b.length(); largestWord = b; } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I just need to look for how to convert it to Static, but that shouldn't be a problem. Thanks Eran for your help! Really appreciate it :).
@jonakru that's simple - public static boolean isAlpha(String userinput) {}...
Thanks Eran for all. What I was needing was the implementation of the IsAlpha. Right now the code doesn't work properly, for a string :"asdasd asd4a65sd4a6s5d4a6sd a6s5d4a6s5d4a ssddaasssddddddddd I got 3 lines of Longest Word: asdasd and then the correct answer. It is on me to correct it. Thanks for the help provided!
@jonakru strange - I tried that input and got just "Longest Word: ssddaasssddddddddd"
Found the mistake, I had misplaced a "}", that was including the System.out.println. So, for every word check it was printing the longest word so far. Thanks!!!!!!!!!!!!!!
1

A possibly easier approach could be to split the string by any non-letter character, and let Java's streams do the heavy lifting for you:

String longestWorld = Arrays.stream(userInput.split("[^a-zA-z]")) .max(Comparator.comparing(String::length).reversed()) .orElse(null); 

1 Comment

Thanks for your reply. You are probably right about that. It happens that right now I am at the beginning of the course, so it is expected from us to use For, While, Loops, more basic stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.