I need to write a program that takes in a string from a user and finds the longe
ID: 3644836 • Letter: I
Question
I need to write a program that takes in a string from a user and finds the longest word in the string.So far i have....
import java.util.Scanner;
public class Lab2_new {
public static void main(String[] args)
{
String sentence;
String word;
String longest = "";
System.out.print("Enter sentence: ");
sentence = inputLine.readLine();
Scanner keyboard = new Scanner(sentence);
while (keyboard.hasNext())
{
word = keyboard.next();
if (word.length() > longest.length())
{
longest = word;
}
}
System.out.println("The longest word is: " + longest );
}
}
Ive tried it using a bufferedreader, inputstreamreader, and IOexception which words but we technically haven't learned those yet so i need a code without but i can't seem to get it right
Thank you!