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

 
 


Maha abdulla
read string - using loops in Java
2 Answer(s)      2 years and 6 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.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 Pages:
How to read loops in Java
How to read loops in Java  Example that explains how to read loop in Java
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
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
JavaScript Loops Function
JavaScript Loops Types: In Java programming language a loop is a language..., and foreach loop. Following examples will help you to learn loops: JavaScript Loop...; </body> </html> Output: 1 3 5 7 9 Java While
read a file
read a file  read a file byte by byte   import java.io.File... static void main(String[] args) { File file = new File("D://Try.txt"); try...); String strFileContent = new String(fileContent); System.out.println("File
Read Write
Read Write  Hi; How can I read certain line of say 10 text files and write to one text file   Java Read Multiple Files and store the data... java.util.jar.*; class OnlyExt implements FilenameFilter{ String ext; public OnlyExt
read image
read image  java code to read an image in the form of an array.   import java.io.*; import javax.imageio.*; import java.awt.image.*; class...(); } return pixels; } public static void main(String[] args) throws
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 , 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
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
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
Use javascript loops..
Use javascript loops..  Write a JavaScript code to find a number of unique letters in string. (Eg. if keyword is Tajmahal, Tajmahal count will be '5... string: "); var arr=st.split(""); var resulted = new Array
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
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
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 prgm
string prgm  write a java program which read a sentence and print occurence of each vowels in it seperatly
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
string
string  read a sentence and replacing character with immediate preeceding character ex: input:Be careful output:Ad bzqdetk
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
string
string   difference detween "public static void main (String[] args) " and "public static void main (String args[])" in java but it executes both ,in java which one is legal   The only difference is style, where
string
string  java prgm to find total occurence of a given string pattern in a sentence
string
string  java prgm to find total occurence of a given string pattern in a sentence
Java read file in memory
Java read file in memory In this section, you will learn how to read a file... main(String[] args) throws Exception { File f = new File("C:/hello.txt... and then read the file
string
string  how do i extract words from strings in java???   Hi... ExtractWords { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter String
string
string  a java program to input a string and display the string...*; import java.io.*; public class FirstLetter{ public static String capitalizeFirstLetter( String str ) { final StringTokenizer st = new StringTokenizer( str
string
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.   Here is a java example that accepts a sentence from the user
string
string  program for String reverse and replace in c,c++,java e.g.Input:10,20,hello output:hello;20;10   Hi Friend, Try the following java...(String[] args) { String array[]=new String[5]; Scanner input
string
string  a java program to replace all matching substrings
Java read multiple files
Java read multiple files In this section you will learn how to read the data... class Files { public static void main(String[] args) throws Exception..."); String sch = input.nextLine(); String fname[] = { "s1.txt", "s2.txt
Read PDF file
Read PDF file Java provides itext api to perform read and write operations with pdf file. Here we are going to read a pdf file. For this, we have used... of StringBuffer,it will again converted into string and display the data on the command
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
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
iPhone Loops in objective c
programing then ..you must read the given definition of loops. What is loop...iPhone Loops in objective c If you have started learning Objective c, this small tutorial on "Loops in Objective c" would be helpful. In this series
program that uses while loops to perform the following steps :
program that uses while loops to perform the following steps :  Write a program that uses while loops to perform the following steps : a. Prompt... inclusive f. Output all the uppercase letters. (java coding: pls help me
How to read file in java
How to read file in java Java provides IO package to perform reading and writing operations with a file. In this section you will learn how to read a text file line by line  in java. FileInputStream- This class reads bytes from
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
a little problem in read data - Java Beginners
a little problem in read data  ok Vinod Kumar and friend, i already... ReadDataTxtFileAndInsertIntoDatabase { public static void main(String[] args) { try... = new BufferedReader(new InputStreamReader(in)); String strLine
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <..._COMMENT> //nothing ... in this row and my some java codes : private String getResults(TopDocs docs) { StringBuilder htmlFormat = new
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <..._COMMENT> //nothing ... in this row and my some java codes : private String getResults(TopDocs docs) { StringBuilder htmlFormat = new
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <..._COMMENT> //nothing ... in this row and my some java codes : private String getResults(TopDocs docs) { StringBuilder htmlFormat = new
can't read my xml file in java
can't read my xml file in java  i've a xml file like this : <..._COMMENT> //nothing ... in this row and my some java codes : private String getResults(TopDocs docs) { StringBuilder htmlFormat = new

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.