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

Home Answers Viewqa Java-Beginners read string - using loops in Java

Have Programming Question? Ask it here!

Maha abdulla
read string - using loops in Java
2 Answer(s)      3 years and 8 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  to find the sum of alternate digits in a given
Java loops
Java loops  What is the difference between a while statement and a do statement
Java - Declaring variables in for loops
Java - Declaring variables in for loops  Java - Declaring variables in for loops
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
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   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 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 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
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
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  Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "Hello World". How
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
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 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 read lines from file
Java read lines from file  Any code example related to Java read... of reading file line by line in Java. Can any one share me the code for reading java file line by line. My file is very big around 6.5GB so it's not possible
loops  I need the program output 55555 4444 333 22 1 like this by using for loop and if condition only
Loops  ??Using for loops, Write a program to simulate a clock countdown. The program prompts the user to enter the number of seconds, displays a message at every second, and terminates when the time expires. Use method
Loops  by using drjava q1.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 q2.Write code
loops  how to use for loop and while loop in java?/ how to write a code using for or while loop
Read data again - Java Beginners
Read data again  sir, i still hav a problem,first your code...); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine... = DriverManager.getConnection("jdbc:odbc:hyt","root","suprax"); //Read File Line By Line
Read data again - Java Beginners
Read data again  OK my problem some like my first question.First i... BufferedReader(new InputStreamReader(in)); String strLine; Class.forName...","root","suprax"); //Read File Line By Line while ((strLine = br.readLine
read excel file from Java - Java Beginners
read excel file from Java  How we read excel file data with the help of java?  Hi friend, For read more information on Java POI visit to : Thanks
Read file into String
Read file into String In this section, you will learn how to read a file into the string.  Description of code: You can use any input stream... data from the file using it's read() method. This data is then stored
Read the value from XML in java
Read the value from XML in java  Hi, i have an XML with the following code. I need to get the path("D... the permissions on that file.So how can i read that value. This is little urgent
How To Read File In Java
How To Read File In Java In this section we will discuss about about how data of a file can be read in Java. A file can contain data as bytes, characters, binary data etc. To read a file in Java we can use following of the classes
loops , control structures
loops , control structures  a simple java program to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
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 - Java Beginners
string in java interview questions  What is string in java? Interview questions
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
use of loops and screenshots
use of loops and screenshots to use loops in netbeans? to draw a correct screenshot
Java string buffer
Java string buffer  what is stringBuffer and StringBuilder
Java read file contents into byte array
Java read file contents into byte array  Hello share the code of java read file contents into byte array with me. It's urgent. Thanks   Hi, This is simple process if you use the InputStream class. Read example
How to convert into to String in Java?
How to convert into to String in Java?  Hi, How to convert into to String in Java? Thanks