30-12-2005, 01:24 PM
|
#1 (permalink)
|
|
OSS Enthusiast!
Join Date: Sep 2005
Location: Hills of Kumaoun
Posts: 664
|
Java Program Error!
I tried to make a prg to find the largest word in a string but was unable.
I ended up with this.Can anyone tell my mistakes and write the correct prg!!
import java.io.*;
class lar_str
{
public static void main(String args[]) throws IOException
{
String str;
char arr[]=new char[50];
int bp[]=new int[50];
int lp[]=new int[50];
int len[]=new int[50];
int c,l;
int bep=0;
InputStreamReader rd=new InputStreamReader(System.in);
BufferedReader inp=new BufferedReader(rd);
System.out.println("Enter the String");
str=inp.readLine();
l=str.length();
str.getChars(1,l,arr,0);
for(c=0;c<l;c++)
{
bp[c]=bep;
while(arr[bep]!=' ')
{
bep++;
}
len[c]=bep-bp[c];
lp[c]=bep;
}
int search_add=len[0];
for(c=0;c<len.length;c++)
{
if(len[c]>search_add)
search_add=c;
}
char fin[]=new char[50];
int k=0;
for(c=bp[search_add];c<=lp[search_add];c++)
{
fin[k]=arr[c];
k++;
}
System.out.println(fin);
}
}
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
30-12-2005, 02:45 PM
|
#2 (permalink)
|
|
Wise Old Owl
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
|
If the means is unnecessary, then why don't you try split() function?
Code:
Pattern p = Pattern.compile("[,\\s]+");
String[] arrWords= p.split(strText);
int chip=0;
for (int i=0; i<arrWords.length; i++)
{
if (arrWords[i].length>arrWords[chip].length)
chip = i;
}
System.out.println(arrWords[chip]);
The code may not work as i haven't run it, but that's the logic. Check the java docs for Regexes.
__________________
http://myxp.blogspot.com
-----------------------
Winchester 3200+ @2,500MHz
LeadTek 7900GT VOLT MODDED @ 680 core, 1800 mem
2x1GB Transcend DDR400 @ DDR454 2.5,3,3,5,1T
|
|
|
30-12-2005, 04:39 PM
|
#3 (permalink)
|
|
Human Spambot
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
|
You can make use of indexOf command for Strings... Search for " ". The first output gives location x. Using this, you know length of first word. Then, repeat search for this for the first character after the current search (indexOf has both parameter list possible). Now, repeat this until you get index as -1... Now, you know length of each word (new position - previous position - 1)... Run through the list to see which is largest...
Also, when you submit a program for error checking, please provide sample run...
Arun
|
|
|
30-12-2005, 07:16 PM
|
#4 (permalink)
|
|
Human Spambot
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
|
Another option is to use the StringTokenizer and keep track of the largest token as you parse through the list...
BTW, to use the split command, you dont need to create a pattern...
//Use this after getting str
String[] words = str.split (" ");
After that, you can follow siriusb's code...
Arun
PS: Is it enough if you search for blank space in the text to split words? What about commas, hyphens, full stops, tabs, etc? If that is the case, you should use Siriusb's method... It splits based on any/all of the whitespace characters.
|
|
|
31-12-2005, 01:08 PM
|
#5 (permalink)
|
|
Wise Old Owl
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
|
Here is my program, It works !!. Forget yours!!
The logic :
1)Get the zeroth element of the array first , store it in a String
2) Compare this String with the rest of the array elements. Whichever is bigger store it and again compare it with the array elements.
Here is the complete source code!!
------------------------------------------------------------
// Find the Largest word in a String
/**
*
* Author JGuru - dated 31-12-2005
*/
import java.util.*;
class LargestWord
{
String str = "Hey Java is great programming language oops!!";
StringTokenizer st = new StringTokenizer(str);
String strArr[] = str.split("\\s");
// Compare the String Length of two Strings
public String compare(String str1, String str2)
{
if(str1.length()>str2.length())
{
return str1;
}
else
{
return str2;
}
}
LargestWord()
{
String tmp = "";
for(int i=0;i<strArr.length;i++)
{
//Initialize variable tmp with the //element '0' in the array
if(i==0)
{
tmp = strArr[0];
}
//The greater of the two Strings store it in tmp variable, and compare it with the rest of the array elements
tmp = compare(tmp, strArr[i]);
}
System.out.println("Largest word = " + tmp);
}
public static void main(String []s)
{
new LargestWord();
}
}
|
|
|
31-12-2005, 01:13 PM
|
#6 (permalink)
|
|
Wise Old Owl
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
|
Update :
StringTokenizer is not needed in the source code, neither is the 'import java.util.*' !! These are redundant!!
I forgot to delete these lines!!
Use Java 1.5 or higher .
Format my source code!!
|
|
|
05-01-2006, 01:15 PM
|
#7 (permalink)
|
|
OSS Enthusiast!
Join Date: Sep 2005
Location: Hills of Kumaoun
Posts: 664
|
Thanks!!!!!
Thanks JGuru for ur program..
Pls tell me the use of StringTokenizer in JAVA.
|
|
|
06-01-2006, 04:37 PM
|
#8 (permalink)
|
|
Wise Old Owl
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
|
StringTokenizer is used to split the String into tokens
(ie., words).Using StringTokenizer you can extract the
words from a String. See the Java API Documentation
for more details.
Also browse thro the following link for more details
on Java technology.
You can see a lots of stuff(Java) being dicsussed in detail here.
http://www.jguru.com/faq/index.jsp
Use a good IDE . Use JCreator Pro ( www.jcreator.com)
Although it's trial version , it's excellent for beginners
like you. If you have worked with big IDEs you
can use NetBeans ( www.netbeans.org) or Eclipse
( www.eclipse.org)
Good luck!!
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|