AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

to break a string into words stored in array

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Anup Bishnoi
Guest





PostPosted: Wed Feb 22, 2006 8:12 pm    Post subject: to break a string into words stored in array Reply with quote



can someone please tell me why this code is giving a runtime error (no
compilation error)

Error :
Quote:
"J:\Programs\Java\jdk1.5.0\bin\java.exe" StringSort
java.lang.NoSuchMethodError: main

Exception in thread "main"

----------------------------------------------------
The program is this file StringSort.java :

public class StringSort {
public static void main() {
String texT = "To be or not to be, that is the question;"
+"Whether `tis nobler in the mind to suffer"
+" the slings and arrows of outrageous fortune,"
+" or to take arms against a sea of troubles,"
+" and by opposing end them?";

String[] words;
int howManyWords = texT.length();
words = new String[howManyWords];

int a=0;
int k=0;
for (int i=0 ; i<texT.length() ; i++) {
if (texT.charAt(i)==' ') {
words[k++]=texT.substring(a,i);
System.out.println(words[k-1]);
while(texT.charAt(i)==' ') {
a=i++;
}
}
}
}
}


plz help
Back to top
Anup Bishnoi
Guest





PostPosted: Wed Feb 22, 2006 8:12 pm    Post subject: Re: to break a string into words stored in array Reply with quote



Anup Bishnoi wrote:
Quote:
can someone please tell me why this code is giving a runtime error (no
compilation error)

Error :
"J:\Programs\Java\jdk1.5.0\bin\java.exe" StringSort
java.lang.NoSuchMethodError: main
Exception in thread "main"

----------------------------------------------------
The program is this file StringSort.java :

public class StringSort {
public static void main() {
String texT = "To be or not to be, that is the question;"
+"Whether `tis nobler in the mind to suffer"
+" the slings and arrows of outrageous fortune,"
+" or to take arms against a sea of troubles,"
+" and by opposing end them?";

String[] words;
int howManyWords = texT.length();
words = new String[howManyWords];

int a=0;
int k=0;
for (int i=0 ; i<texT.length() ; i++) {
if (texT.charAt(i)==' ') {
words[k++]=texT.substring(a,i);
System.out.println(words[k-1]);
while(texT.charAt(i)==' ') {
a=i++;
}
}
}
}
}


plz help


inserting (String args[]) as argument in main() solved the problem but
please tell me what difference did it make?
Back to top
Paul Hamaker
Guest





PostPosted: Wed Feb 22, 2006 9:12 pm    Post subject: Re: to break a string into words stored in array Reply with quote



main() and main( String[] )
are two different methods. A method is identified by its name plus
parameters. Method overloading.
---------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com
Back to top
Rob Skedgell
Guest





PostPosted: Wed Feb 22, 2006 9:12 pm    Post subject: Re: to break a string into words stored in array Reply with quote

Anup Bishnoi wrote:

Quote:

Anup Bishnoi wrote:
can someone please tell me why this code is giving a runtime error
(no compilation error)

Error :
"J:\Programs\Java\jdk1.5.0\bin\java.exe" StringSort
java.lang.NoSuchMethodError: main
Exception in thread "main"


----------------------------------------------------
The program is this file StringSort.java :

public class StringSort {
      public static void main() {

[...]

Quote:

inserting (String args[]) as argument in main() solved the problem but
please tell me what difference did it make?

In order to run the class, the JVM invokes a main method which accepts a
single array of strings, and can be declared as:
public static void main(String[] args)
or
public static void main(String...)
(Java Language Specification v3.0 s12.1.14)

If you declare it as "public static void main()" it can't accept an
array of strings, or anything else as parameter(s).

--
Rob Skedgell <rob+news (AT) nephelococcygia (DOT) demon.co.uk>
GnuPG/PGP: 7DA3 1579 C0DD 8748 C05A B984 E2A2 3234 D14B 6DD7
Back to top
Rajah
Guest





PostPosted: Wed Feb 22, 2006 9:12 pm    Post subject: Re: to break a string into words stored in array Reply with quote

Anup,
There's a nifty java function to split up a string into words. You may
replace everything after the texT definition with:
String [] words = texT.split("\\s");

for (int i=0; i<words.length; i++) {
System.out.println(words[i]);
}

And you'll get the final "them?" as well. The split("\\s") has the
effect of dividing the string up at spaces and placing them into the
words array.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.