Online Hibernate Training just in Rs. 1000

One week online training in Hibernate ORM framework. Learn Hibernate Framework and enhance your skills. Best Online Hibernate training at 90% discount. Join Now!! Offer is valid Only for limited Period!!! Online Training Big Data and Hadoop Training Java Training  

read string - using loops in Java



Share on Google+Share on Google+

Maha abdulla
read string - using loops in Java
2 Answer(s)      6 years and 9 months ago
Posted in : Java Beginners

Write a program to read a string composed of an unknown number of words, then count the number of words in the string, and Display the longest and shortest words, with first letter Uppercase and rest in lowercase.??

View Answers

November 29, 2010 at 3:11 PM

Hi Friend,

Try the following code:

import java.util.*;
public class StringExample1
public static void main(String[]args){
Scanner input=new Scanner(;
System.out.println("Enter String: ");
String st=input.nextLine();
String[] str = st.split(" "); 
String longest = str[0];
String shortest = str[0];

for(String s : str)
if(s.length() > longest.length()){
longest = s;
if(s.length() < shortest.length()){
shortest = s;
String str1=longest.substring(0,1).toUpperCase();
String str2=longest.substring(1).toLowerCase();
System.out.println("Longest Word is: "+(str1+str2));

String str3=shortest.substring(0,1).toUpperCase();
String str4=shortest.substring(1).toLowerCase();
System.out.println("Shortest Word is: "+(str3+str4));


November 29, 2010 at 7:31 PM

thanks alot ..

but how did you use for loop without using ( initial expression & logical expression & update expression ) ?? and why you didn't use console ?? I think the code will be smaller if we use console !!

wating you .. And want to tell U that I am using JGrasp program ...


Related Tutorials/Questions & Answers:
How to read loops in Java
How to read loops in Java  Example that explains how to read loop in Java
Java loops
Java loops  What is the difference between a while statement and a do statement
java loops
java loops  to find the sum of alternate digits in a given
Java - Declaring variables in for loops
Java - Declaring variables in for loops  Java - Declaring variables in for loops
Read/Convert an inputStream to a String
in Java and convert it to a String. what is the best code for for this? How to Read/Convert an inputStream to a String? My object is of //and it will work String str = "Learn Java at http
java loops - Java Beginners
java loops  Q1 print the following pyramid * * * * * * * * * * * * * * * * * * * *  ...(String[] args) { System.out.println("*"); for(int i=1;i<=4;i
java loops - Java Beginners
java loops   Print the pyramid --------- * --------* * * * * * * * * * * * * * * * * * *  ... void main(String[] args){ int i; System.out.println(" *"); System.out.println
JAVA LOOPS - Java Beginners
JAVA LOOPS  Hi I need a Java program that can toss a coin over...) (Math.random() * 2); } public String toString(){ String faceName...; } public static void main (String[] args) { do{ flip
LOOPS !! - Java Interview Questions
LOOPS !!  i have string "Hello" i want to print like H He Hel...; String s="Hello"; System.out.println(s.length()); System.out.println...(String[] args) { String st="Hello"; for(int i=1;i<6;i++){ String
LOOPS - Java Interview Questions
LOOPS   how to find out unique number of elements in given array?{1,2,5,3,2,1,1,7,2,3,0,1,5} output is: 1-4 2-3 3-2 5-2 0-1 7-1 i want source code plz help me
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Arrays, loops and string handling
Arrays, loops and string handling  How would i go about in creating.... this is a console program that uses JOptionPane dialog boxes as well. Read a set of 10 correct answers using JOptionPane dialog box and store the entire answer as on string
Java - How to read a string line per line
Java - How to read a string line per line  Java - How to read... to read the big String line per line? Is there any API available in Java... a file using Scanner Class Java Read File Line by Line - Java Tutorial Thanks  
java read file
java read file  Hello i need some help... i want to read an MS Excel file in java so how to read that file
Read file in java
Read file in java  Hi, How to Read file in java? Thanks   Hi, Read complete tutorial with example at Read file in java. Thanks
Java string
Java string  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
Java read binary file
Java read binary file  I want Java read binary file example code that is easy to read and learn. Thanks   Hi, Please see the code at Reading binary file into byte array in Java. Thanks   Hi, There is many
Read from file java
Read from file java  How to Read from file java? What is the best method for a text file having a size of 10GB. Since i have to process the file one line at a time so tell me the very best method. Thank you
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... a java code is given below. import; import... org.w3c.dom.NodeList; class XMLReader { public static void main(String argv
Java char to string
Following program will show you how to convert a char into a string in Java. Though there are many ways to convert a char to a string we will use toString... of any white space. Example of converting Java char to string: package Roseindia
read an image in java
read an image in java  qns: how we can read an image tell me about its code
READ DATA - Java Beginners
= new BufferedReader(new InputStreamReader(in)); String strLine; //Read File Line By Line while ((strLine = br.readLine()) != null) { String a = strLine...READ DATA  i still don't understand, i have a data like
Java String
Java String  You are given two strings S1 and S2,Delete from S2 all those characters which occur in s1 also and create a clean S2 with the relevant characters deleted..write a pgm for this..pls send as soon as possible Thank you
Java file read write operation
Java file read write operation  how to read and write the data from..."); oos.writeInt(8000); oos.writeObject("Ankit"); oos.writeObject("Java... object ois = new ObjectInputStream(fis); String sh = (String
java loops - Java Beginners
a program to reverse a string using another array and without using another loops  Q1 print the following pyramid...? Q4- Write a program to sum the series- 2/9-5/13+8/17......... Q 5
java loops - Java Beginners
java loops  Q1-sum the series 2/9-5/13+8/17..... Q2 print first n odd numbers in descending order? Q3 program to input digits and write in words with out using any type of in built function and array program to print truth
string - Java Beginners
string in java interview questions  What is string in java? Interview questions
Java String - Java Beginners
Java String  How to seperate the string into characters.  there is a method called getChars() in String class,plz use that and separate into characters so plz verify the String api for related questions 
string - Java Beginners
string  hi, i need a source code for d following prgm; a java prgm which will read a string and rewrite it in the alphabetical order. for example... main(String [] args)throws IOException { String [] stringArray = new String[10
String and StringBuffer - Java Beginners
; Hi vijay Java provides the StringBuffer and String classes.... Simply stated, objects of type String are read only and immutable. The StringBuffer...String and StringBuffer  Dear Deepak Sir, What is String