Java Program Error!

Status
Not open for further replies.

nitish_mythology

OSS Enthusiast!
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);
}
}
 

siriusb

Cyborg Agent
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.
 

sakumar79

Technomancer
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
 

sakumar79

Technomancer
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.
 

JGuru

Wise Old Owl
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);
}
System.out.println("Largest word = " + tmp);

}

public static void main(String []s)
{
new LargestWord();
}
}

:D :D
 

JGuru

Wise Old Owl
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!!
 

JGuru

Wise Old Owl
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.
*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!!
 
Status
Not open for further replies.
Top Bottom