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?
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 );
}
}
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)
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);
}
}
Re: I have no idea why "class or interface is expected" , or the main method is missi
Code :
public static void main(String[] args) {
}
is the main method why do you have 2 of them?
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
Re: What is wrong with this code?
There is a lot wrong with your code lol :-s 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.
Code java:
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());
}
}
Re: I have no idea why "class or interface is expected" , or the main method is missi
Quote:
Originally Posted by
dannyboi
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.
Code java:
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.