Maha abdulla
read string - using loops in Java
2 Answer(s)      4 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.??

Advertisement
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.in);
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));
}
}

Thanks



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
Advertisements
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
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
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
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
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
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 array...java loops  Q1 print the following pyramid...? Q4- Write a program to sum the series- 2/9-5/13+8/17......... Q 5
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... a solution.my java code is given below. import java.io.File; import... org.w3c.dom.NodeList; class XMLReader { public static void main(String argv
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 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
loops
loops  I need the program output 55555 4444 333 22 1 like this by using for loop and if condition only
Loops
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
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