Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Magazine Finding the longest word in a user given string

Have Programming Question? Ask it here!
 
 
 


Mayawati Yayawati
Finding the longest word in a user given string
2 Answer(s)      2 years and a month ago
Posted in : Java Magazine


In java, accept a string from the user and find the longest word in it.


Advertisement
View Answers

July 24, 2012 at 6:13 PM


The given code finds the longest word from the string.

import java.util.*;

class LongestWord
{
Scanner input=new Scanner(System.in);
System.out.println("Enter string: ");
String str=input.nextLine();
String stringArray[] = str.split("\\s");
public String compare(String st1, String st2){
if(st1.length()>st2.length()){
return st1;
}
else{
return st2;
}
}
LongestWord(){
String word = "";
for(int i=0;i<stringArray.length;i++){
if(i==0){
word = stringArray[0];
}
word = compare(word, stringArray[i]);
}
System.out.println("Longest word = " + word);
}
public static void main(String []args){
new LongestWord();
}
}


July 25, 2012 at 1:10 AM


import java.util.*;

class LongestWord {

    String str = "Ram is intelligent boy";
    String stringArray[] = str.split("\\s");

    public String compare(String st1, String st2) {
        if (st1.length() > st2.length()) {
            return st1;
        } else {
            return st2;
        }
    }

    LongestWord() {
        String word = "";
        for (int i = 0; i < stringArray.length; i++) {
            if (i == 0) {
                word = stringArray[0];
            }
            word = compare(word, stringArray[i]);
        }
        System.out.println("Longest word = " + word);
    }

    public static void main(String[] args) {
        new LongestWord();
    }
}
/**
 *Longest word = intelligent
 * 
 * */


You will get the more information 

Get the SCJP and SCWCD dumps 


Related Tutorials/Questions & Answers:
Finding the longest word in a user given string
Finding the longest word in a user given string  In java, accept a string from the user and find the longest word in it.   The given code finds the longest word from the string. import java.util.*; class LongestWord
how to write a function to print for finding the longest palindrome in the given string
how to write a function to print for finding the longest palindrome in the given string  how to write a function to print for finding the longest palindrome in the given string
Advertisements
Find Longest Word from the string using Java
Find Longest Word from the string using Java Here we are going to find the longest word from the string. For this, we have specified the string which... and allow the longest word to display. Here is the code
finout longest word in sentence
]); } System.out.println("Longest word = " + word); } public static void main(String []args){ new...finout longest word in sentence  write a program , how to find out the longest word in a sentence using java   Hi Friend, Try
Finding a given word using regular expression
Finding a given word using regular expression  ... a given word from the String and also the no of times the word exists using regular... involved in program Find.java are described below:- String regex="\\b Tim\\b":-Here
Write a program in JAVA which accepts a sentence & displays the longest word in the sentence alongn with it length of the word.
Write a program in JAVA which accepts a sentence & displays the longest word in the sentence alongn with it length of the word.  **A program in JAVA which accepts a sentence & displays the longest word in the sentence along
servlet code for finding the table for given number
servlet code for finding the table for given number  servlet code for finding the table for given number?   Here is a an example that accepts a number from the user and display its multiplication table using servlet. 1
Finding word using regular expression
Finding word using regular expression       This Example describes the way to find the word from... the given regular expression into a pattern. String text = " JavaSun's regex
Write String in Word document
Write String in Word document  How to read and write strings in word document
Reverse word of string in alphabetical order
Reverse word of string in alphabetical order  wap a program that reverse word of string in alphabetical order. e.g. input by user The purpose of education is to replace an empty mind with an open one output
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
Compare String with given String arraylists.
Compare String with given String arraylists.  I have one String for example abcThirtyFour. i have two arraylists of string. one contains one, two.... i was thinking of comparing given string with both the arraylists. but, i
Finding start and end index of string using Regular expression
Finding start and end index of string using Regular expression... the way for finding start and end index of the string using regular expression... in program  Finding_index.java are described below:- String[] text
Java count frequency of words in the string
how to count the occurrence of each word in the given string. String... of words in a string. In the given example, we have accepted a sentence as a string from the user and convert it to String[] where each element of the array
Determining the Word Boundaries in a Unicode String
Determining the Word Boundaries in a Unicode String In this section, you will learn how to determine the word boundaries in a unicode string. Generally, we... to break the string into words. In the given example, we have invoked the factory
find the given input is integer or string
find the given input is integer or string  simple coding for to check the given input value is integer or no.If the given value is integer display... CheckValue { public boolean checkIfNumber(String in) { try
Help me on the given problem of String
Help me on the given problem of String  class Test { string s1="Hello",s2="lo"; System.out.println((s1=="Hel"+"lo")); System.out.println((s1=="Hel"+lo)); } The above program prints true
Collections Exercise 4 - Word Translator
, given a word (String), to its translations (a bunch of Strings). Problems...(String word), which returns an iterator over all definitions for the given word...: An online dictionary is needed. The user will enter a word and the program
Evaluating a math expression given in string form
Evaluating a math expression given in string form  Evaluating a math expression given in string form
count occourance no of time no between 0 to 9 given by user in java
count occourance no of time no between 0 to 9 given by user in java  import java.io.*; class count_no { public static void main(String args... example that accepts 9 integers from the user and count the occurrence of each
Example - Replace word
occurences a word in a string with another word. Assume the method signature... a new String. The word "replace" is probably misleading. Also, the method... woman". Ok, but what if we changed the orginal string to contain the word "human
write a java program for inserting a substring in to the given main string from a given position
write a java program for inserting a substring in to the given main string from a given position  write a java program for inserting a substring in to the given main string from a given position
In data structure in java how to parse the given string and counts the member of character that match the given data?
In data structure in java how to parse the given string and counts the member... the given string and counts the member of character that match the given data.using the STACK or QUEUE Example output ** GIVEN STRING: a b c d e f g INPUT
I want to put user guide in pdf and word in side WEB-INF and need to give link on the home page
I want to put user guide in pdf and word in side WEB-INF and need to give link on the home page  I want to put user guide in pdf and word formats in side WEB-INF (for security reasons, I want these documents visible after user
accept sentence from user and print all word starts with vowel and end with consonent
accept sentence from user and print all word starts with vowel and end with consonent  Write a program to accept a sentence. Print all the words that starts with vowel and end with a consonant. e.g. input by user "the purpose
Word Count
Word Count       This example counts the number of occurrences of  a specific word in a string. Here we are counting the occurrences of word "you" in a string
Convert a String to Hexa
;  In the example given below, you will learn how to convert String to hexadecimal  number. This example takes a user input string and convert in hexa numbers. Program asks the user to enter the string. User enters
Finding A Date
Finding A Date  My Frequency Start Date is 24-08-2012 My frequency end date - not defined Calculation Frequency is Weekly(Sunday) My calculation will be from 24-08-2012 to the following sunday My question is how to find
Write a method which will remove any given character from a string?
Write a method which will remove any given character from a string? In this example we will describe remove any given character from a string.... Example of remove any given character from a string
string
string  How to count a particular character in a given sentence and also displaying that contain word? Like I want to find "a" in the string "This is example of java Programme" and also display word that contain "a" character
finding the prime numbers
finding the prime numbers  Hi, I am a beginner to java and I have problem with the code in finding the prime numbers, can someone tell me about the problem. Thanks.   Want to know about how to find out the given
Repeat word as many times as it has characters
in it. To do this, we prompt the user to enter the word using the Scanner class. After finding the length of the word, we have created a for loop, it will display...Repeat word as many times as it has characters in Java This section
2. Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
2. Given a string and a number ?n?, find the ?n?th distinct repeating character.    Given a string and a number ?n?, find the ?n?th distinct repeating character. For example, Input: Abracadabra, 1 Abracadabra, 2 Abracadabra, 3
plz give me program to this: Given a string and a number ‘n’, find the ‘n’th distinct repeating character.
plz give me program to this: Given a string and a number ?n?, find the ?n?th distinct repeating character.    Given a string and a number ?n?, find the ?n?th distinct repeating character. For example, Input: Abracadabra, 1
String substring method example
a string. String class provide methods for finding substring from string. Method... .style1 { font-size: medium; } String substring() method example... index position of the sub string and second is end index position of the sub
Retrieving String after given Regular expression
Retrieving String after given Regular expression... below:-:-  String regex = "(?<=http://www.)\\S+":-Here we have given... the String after giving expression.For this we are going to make program named
finding java code on internet
finding java code on internet  Is there anybody to help me? i want to some java coding web sites.it is prerequest for me
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
finding divisors of a number
finding divisors of a number   write a c program to input a number and print all its divisors using while loop
Location finding error - JSP-Servlet
Location finding error  Location needs from drive name: My file uploading program has an error. It requires the location should be given from the directory name. Instead of this if only parent directory is given it shows
MicroSoft word
MicroSoft word  sir i want to develop an swing application like the microsoft word Document.can you help me pls
Finding Lost Frames - Java tutorial
Finding Lost Frames 2001-05-10 The Java Specialists' Newsletter [Issue 019] - Finding Lost Frames Author: Dr. Heinz M. Kabutz If you are reading... fact to the Politburo, but perhaps I've read too many spy stories. Finding
Reverse string in BlueJ
Reverse string in BlueJ  wap that user enter a string and one word. so i want that program search word in string entered by user if word exist in string than reverse only that word and give output. e.g This is a flower (string
Finding duplicates using array in for loop
Finding duplicates using array in for loop  how to find the duplicates in array using for loop
String Reverse in Java
String Reverse in Java       In this example we are going to reverse a given string. This example takes values from command line argument as string, buffers
String End with Example
; This section tells you, how to determine the given string ends with specified string. The following program helps you to check whether the given... 'true', it will display a message "The given string is end
String
String  write down the code of remove any character from a given string without using any string function   please give me the code of remove any given character from a given string without using function
 

 

 

DMCA.com