Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 30-12-2005, 01:24 PM   #1 (permalink)
OSS Enthusiast!
 
nitish_mythology's Avatar
 
Join Date: Sep 2005
Location: Hills of Kumaoun
Posts: 664
Default 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);
}
}
nitish_mythology is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 30-12-2005, 02:45 PM   #2 (permalink)
Wise Old Owl
 
siriusb's Avatar
 
Join Date: May 2005
Location: Chennai, India, Asia, the Earth, the Solar system, the Milky Way, the Local group, this Universe.
Posts: 1,171
Default

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
siriusb is offline  
Old 30-12-2005, 04:39 PM   #3 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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 is offline  
Old 30-12-2005, 07:16 PM   #4 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default

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.
sakumar79 is offline  
Old 31-12-2005, 01:08 PM   #5 (permalink)
Wise Old Owl
 
JGuru's Avatar
 
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
Default

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();
}
}

JGuru is offline  
Old 31-12-2005, 01:13 PM   #6 (permalink)
Wise Old Owl
 
JGuru's Avatar
 
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
Default

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 is offline  
Old 05-01-2006, 01:15 PM   #7 (permalink)
OSS Enthusiast!
 
nitish_mythology's Avatar
 
Join Date: Sep 2005
Location: Hills of Kumaoun
Posts: 664
Default Thanks!!!!!

Thanks JGuru for ur program..
Pls tell me the use of StringTokenizer in JAVA.
nitish_mythology is offline  
Old 06-01-2006, 04:37 PM   #8 (permalink)
Wise Old Owl
 
JGuru's Avatar
 
Join Date: Dec 2005
Location: Space-time continuum
Posts: 1,646
Default

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!!
JGuru is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by abhidev
- by Sujeet
- by Sarath
- by Krow

Advertisement




All times are GMT +5.5. The time now is 09:02 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2