• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This program is throwing back error

 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever i try to run this program even though after successful compilation it is throwing back this error
Exception in thread "main" java.lang.NoSuchMethodError: main

And this is he program

/* This class uses API introduced in 1.1. */

import java.util.Locale;

public class FirstClass {
public String getString() {
return Locale.getDefault().getDisplayName();
}
}
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class file that you use as the entry-point to your software needs to have a method named main() with the following signature:

public static void main(String[] args) {
}


Kaydell
 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried to input the main argument but i think it just go to reflect that i'm a complete novice.i tried adding the main argument and it just wouldn't compile,this is how the code looks like and it throws up error
/* This class uses API introduced in 1.1. */

import java.util.Locale;

public class FirstClass {
public static void main (string[] args)
{
public String getString() {
return Locale.getDefault().getDisplayName();
}
}
}

this is the error message,please do not get pissed with me,i really need help and if anybody could help me in terms of materials i will really appreciate it
C:\java\java src\FirstClass.java:8: illegal start of expression
public String getString() {
^
C:\java\java src\FirstClass.java:11: ';' expected
}
^
2 errors
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's OK. Generally, people here at the JavaRanch like to help each other.

Asking Questions on the Java Ranch

Suggestions:

1. If you use the CODE tags (see the CODE button below), your code will be more readable and you'll get better help.
2. String is the name of a class so it should be capitalized when you want a class. Variables and method names start with a lower-case letter by conention.
3. The getString() method should be decalared within the class -- but not within another method.
4. To call getString() from main, getString() should be static since you're calling it from main which is static.

I have given you some hints. Try to solve the problem and if you need more help, come back and add another question to this thread. This way, you'll learn better than if I just gave you the solution in code.

Kaydell
[ June 07, 2007: Message edited by: Kaydell Leavitt ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abbey,

I'm new here but I am going to try to lend a hand where I can.

First your code, prettied up just a bit (it helps me to see the levels if you will...)
-------


--------------

From this I ascertain that you are wanting to use the Locale class to determine your default location.

Locale is a class defined in the java.util package and you have accounted for that by importing the Locale class explicitly.

You are wanting to use two methods from Locale, getDefault() and getDisplayName().

getDefault is a static method that returns a reference to your default Locale. That says two things 1) it can be called without an instance, and 2) it's return value needs to be placed in a reference variable of type Locale.

getDisplayName() is an instance method that returns a String value representing the name of that instance's Locale.

----
Now, as to the structure of your code...

You had method getString inside the main method and I don't think that is what you wanted. main() and getString() are both instance methods of FirstClass and should be on the same level.

i.e. public static void main(String [] args){
// code
}
public String getString() {
// Code
}

One of the things you have to think about is..... getString() is an instance method of your FirstClass class and THAT means that it must be instantiated. So....main has a thing to do and that is... instantiate it. Once that is done, yuo can now call getString to get the string returned to you.

NOw let's look at getString itself.

This is where the "meat" of the functionality is and needs to be seen as the two steps it is.

1) get the reference to the default Locale, then
2) using that Locale as a reference, get it's Locale name.

as you think of these two look back at the top of my drivel... at what each of the two calls does....

okay, here is my code, with a few comments added to tie in to the above drivel!!!

I hope that this helps you out and perhaps gives a little insight into using these classes and methods.

My best to you,
Bob


------------------------------------------

[ June 07, 2007: Message edited by: Bob Ruth ]
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops.....sorry Kaydell, didn't mean to step on you.

Yes I did furnish a solution but not without making Abbey suffer through reading all of that nonsense. And I tried to make the explanation sort of a seed, to perhaps show how to think about these things so that it will still be a learning experience.

Give someone a fish, they can eat,
Teach them to fish....
[ June 07, 2007: Message edited by: Bob Ruth ]
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Whoops.....sorry Kaydell, didn't mean to step on you.


That's OK. Sometimes, I answer in words, sometimes, I answer in code. Maybe, I should answer both in words and in code and let the person that I'm helping handle their own study habits.

Kaydell
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please remember that our goal here is not to just give someone the code... we're trying to help them learn, not hand out answers.

I'm not saying you did that here, i'm just throwing out a reminder for the future.
 
Abbey Samuel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys i'm just trying to give myself some time to digest this and may be from this,i could start thinking in the java language.But if anybody have a book/material or links to where i could get to understand this code and programming of a thing from the scratch.It would be surprising that this is the first time i'm laying my hand on anything programming and i know next to nothing but in my intended school when we will be resuming this session we will be having a course that requires a knowledge of Java or C++ as a pre-requisite,so you could see the reason i have to learn by force.
I really appreciate you guys for your contribution,i am ready to learn.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic