read string - using loops in Java

read string - using loops in Java

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.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
read complete file as string java
read complete file as string java  Hi, How I can read read complete file as string java? Thanks   Hi, You can do like this in one line: String fileContent = new String(Files.readAllBytes(Paths.get(fileName
Advertisements
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 java.io.InputStream... //and it will work String str = "Learn Java at http
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
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  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
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
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 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.ADS_TO_REPLACE_1 Example of converting Java char to string
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 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
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
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 an image in java
read an image in java  qns: how we can read an image tell me about its code
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
string - Java Beginners
string in java interview questions  What is string in java? Interview questions
capitalizing string in java
capitalizing string in java  How to capitalizing string in Java?   String s = "some string"; String capitol = Character.toString(s.charAt(0)).toUpperCase(); String newString = capitol + s.substring(1,x
String immutable - Java Beginners
String immutable  Why is String immutable?  See here Why String is immutable in Java
How to split string in Java?
How to split string in Java?  Hi, I have a string seperated by ',' comma. How to split the string? Thanks
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
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 string buffer
Java string buffer  what is stringBuffer and StringBuilder
How to Append String In Java?
How to Append String In Java?  How to write program in in Java for appending to a String? How to Append String In Java? What is the best way to do... the StringBuilder class. Check the example at Appending String in java. You can
converting string to double in java
converting string to double in java  Please post an example to converting string to double in java. Thanks!   Convert String to Double Tutorial
Java String Datatype
Java String Datatype  Weather String in java is class or Datatype,if it's clss then How can i assign String str="hello";   String is a class found in the package java.lang.*. When we represent string in double quotes
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
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
Java string buffer
Java string buffer  What is the basic difference between string and stringbuffer object
loops
loops  how to use for loop and while loop in java?/ how to write a code using for or while loop
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
Loops
Loops
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
loops  I need the program output 55555 4444 333 22 1 like this by using for loop and if condition only
loops
loops  how to get the sum of 10 number using a loop
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 
Appending String efficiently in Java
for Appending String efficiently in Java. Thanks   Hi, StringBuilder class of Java API is used by developers of Java for appending string... example at How To Append String In Java? Thanks

Ads