Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: What is wrong with this code?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is wrong with this code?

    package javaapplication11;



    public class HelloApp2 {
    {
    public static void main (String [] args)
    {
    Greeter myGreeterObject = new Greeter () ;
    myGreeterObject.sayHello();
    }



    }

    }

    I can't seem to find anything wrong , but the compiler says that it doesn't have a main method?


  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The compiler says the main method does not show up here, what is wrong?

    package javaapplication11;



    import javax.swing.JOptionPane;

    public class Greeter
    {

    public void sayHello()
    {
    (JOptionPAne.showMessageDialog(null
    "Hello,World", "Greeter",
    JOptionPane.INFORMATION_MESSAGE );
    }
    }

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is wrong with this code?

    Please read the forum rules. You have posted 2 topics that are basically the same. I have merged your two posts for you, but please avoid doing this again, and please use the code tags (instructions of which are again in the forum rules)

  4. #4
    Junior Member
    Join Date
    Jul 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I have no idea why "class or interface is expected" , or the main method is missing?

    public static void main (String [] args)
    {
    package javaapplication11;



    public class HelloApp
    {
    static String helloMessage;

    public static void main (String [] args)
    {

    helloMessage = "Hello, World !";
    System.out.println(helloMessage);
    }
    }

  5. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: I have no idea why "class or interface is expected" , or the main method is missi

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

    is the main method why do you have 2 of them?

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What is wrong with this code?

    @dannyboi, this is a warning. Please do not continue to force moderators to merge your threads - keep your questions regarding this topic - whatever variations of your code they may be - in this thread only. Otherwise, you will force the moderating team to take further action

  7. #7
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb Re: What is wrong with this code?

    There is a lot wrong with your code lol For one it won't do anything, because you are calling methods that don't exist. Here is a good example that accomplishes something similar to what I believe you were trying to accomplish.

    import java.util.Scanner;
     
     
    public class HelloWorldObject {
     
    	private void sayHello(String name)
    	{
    		String greeting = "\nHello "+ name +" welcome to JAVA!!!";
    		System.out.println(greeting); 
    	}
     
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("Please enter your name...");
    		new HelloWorldObject().sayHello(keyboard.next());
    	}
    }
    f34r th3 kut3 1z

  8. #8
    Junior Member
    Join Date
    Jul 2012
    Location
    Canada
    Posts
    6
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: I have no idea why "class or interface is expected" , or the main method is missi

    Quote Originally Posted by dannyboi View Post
    public static void main (String [] args)
    {
    package javaapplication11;



    public class HelloApp
    {
    static String helloMessage;

    public static void main (String [] args)
    {

    helloMessage = "Hello, World !";
    System.out.println(helloMessage);
    }
    }
    You have two main methods, then your class identifier, that's why your error is being thrown.

    public class Random {
     
    	public static void main(String[] args) {
    		//Here's out string instance which we use in the system.out object
    		String hello = "Hello!";
    		System.out.println(hello);
    	}
     
    }

    p.s: You don't need a static string for your helloMessage instance.

Similar Threads

  1. can someone tell me what's wrong with my code
    By Nate08 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 23rd, 2012, 05:07 PM
  2. what is wrong with my code?
    By bmb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 7th, 2011, 09:03 AM
  3. want wrong with my code
    By drum in forum Member Introductions
    Replies: 1
    Last Post: May 20th, 2011, 09:06 AM
  4. What's Wrong With My Code?
    By arunjib in forum What's Wrong With My Code?
    Replies: 18
    Last Post: May 7th, 2011, 08:47 PM
  5. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM

Tags for this Thread