473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

how to print the highest length of word in a string ?

Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.    static String s = "Hello i am fine" ;
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.        StringTokenizer st=new StringTokenizer(s, "' ';");
  8.        int a [] =new int[30];
  9.        String str="";
  10.        while(st.hasMoreTokens())
  11.        {
  12.            //int a [] =new int[30];
  13.            int i=0;
  14.             str=st.nextToken();
  15.  
  16.            System.out.println(str);
  17.  
  18.            a[i]=str.length();
  19.            System.out.println(a[i]);
  20.  
  21.        }
  22.             int maxvalue,j;
  23.              maxvalue=a[0];
  24.              for(j=0;j<str.length();++j)
  25.  
  26.           if(a[j]>maxvalue)
  27.           maxvalue=a[j];
  28.          System.out.println("The maximum length of word is : " +maxvalue);
  29.   }
  30.  
  31.        }
  32. /* The above code prints the length of last word in a string but i have to print the highest length of word in a string */
Jul 12 '10 #1
1 1739
Dheeraj Joshi
1,123 Expert 1GB
There are multiple issues.

Expand|Select|Wrap|Line Numbers
  1. while(st.hasMoreTokens())
  2.        {
  3.            //int a [] =new int[30];
  4.            int i=0;
  5.             str=st.nextToken();
  6.  
  7.            System.out.println(str);
  8.  
  9.            a[i]=str.length();
  10.            System.out.println(a[i]);
  11.  
  12.        }
  13.  
Change it to

Expand|Select|Wrap|Line Numbers
  1. int i=0;
  2. while(st.hasMoreTokens())
  3.        {
  4.            //int a [] =new int[30];
  5.            str=st.nextToken();
  6.  
  7.            System.out.println(str);
  8.  
  9.            a[i]=str.length();
  10.            i++;
  11.  
  12.        }
  13.  
Next

Expand|Select|Wrap|Line Numbers
  1. if(a[j]>maxvalue)
  2. maxvalue=a[j];
  3.  
You need to wrap it within the for loop.

Regards
Dheeraj Joshi
Jul 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jon Perez | last post by:
There are objects whose repr() is not the same as what gets printed out when you apply the print statement to them. Usually these are complex objects like exceptions. Example: >>> import...
5
by: Mitchua | last post by:
Simplified a bit, I'm parsing HTML documents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah @sentences = split(/\./, $html)); then I'm trying to determine the...
11
by: cc | last post by:
Hi, From a C# application am I writing some text in a Word.Table, in a Cell of the Table to be more precise. The cell has a certain width and any string written in the cell will cover one or...
5
by: Daniel | last post by:
c# string size limit? length of string limit?
17
by: anurag | last post by:
hey can anyone help me in writing a code in c (function) that prints all permutations of a string.please help
1
by: mhsasono | last post by:
Hi all, Has anybody experienced with CrystalReport? My question is: Does CrystalReport have capability to print the Microsoft Word document? Reason: I want to know CrystalReport capability...
1
by: vanand | last post by:
hai, I have wanted to print the data in word document from vb. Now i can able to print the data from database to word without format layout. I need to format the data in word. Kindly send your...
4
by: krndhi1983 | last post by:
Hi to All, I need a query to Find out the max.length of string in a particular Column. For ex. In a table,One of the column name is EmpName, In this column I want...
5
by: prakashturkar | last post by:
Hi, I am Prakash.... I have tried to print an MS Word file using the basic print utilities provided in JAVA.But while asking for printing through my own code i am getting proble for example..."The...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.