no main classes not found - java

I have few classes in my project.the main class should me SMSMain.But when i run the project it gives no main class found error.I tried to set in in properties(Netbeans) but it doesn't find any.where am I going wrong?here is my code:
package
na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
public class sms {
class Student
{
int studentID;
String studentName,course;
public void setName(String studentName){
this.studentName = studentName;
}
public void setNewId(int studentID){
this.studentID = studentID;
}
public void setCourse(String course){
this.course = course;
}
public String toString(){
return studentID +" "+studentName.toString();
}
class lecturer{
int staffID;
String staffName,taughtCourses;
public void setName(String staffName){
this.staffName = staffName;
}
public void setNewId(int staffID){
this.staffID = staffID;
}
public String toString(){
return staffID +" "+staffName.toString();
}
class course{
String code,description;
double units;
course(String code, String CD){
this.code=code.toUpperCase();
description=CD.toUpperCase();
}
public String getCode(){
return code;
}
/**
* #param args the command line arguments
*/
public class SMSMain {
public void main(String[] args) {
// TODO code application logic here
Student a = new Student();
a.studentName = "Maria";
a.studentID = 1236;
System.out.println("Student Name:" + a.studentName);
System.out.println("Student Name:" + a.studentID);
}
}
}}}}

You need to use
public static void main(String[] args){/* ... */}
with the static keyword.
Also, put your class SMSMain in it's own file SMSMain.java. Then import the needed classes (ctrl-shift-o in Eclipse).
Try to always put a class in it's own file, unless you've got a really good reason not to.

You main class must be static.
Like this:
public static void main(String[] args)
{
// main goes here
}

Try replacing public void main(String[] args) { with public static void main(String[] args) {

Your coding style is confusing. Learn the Oracle/Sun Java coding standards.
The main method has to follow the exact signature and be associated with the public outer class, not one of the inner classes.
package na.edu.pon.oop210s.s12012.s211045888.sms;
/**
* Student Number: <211045888>
* Date: 3/11/12 7:47 pM
* Exercise: <Exercise 4>
* Created using: <netbeans>
*/
// bad naming. What's sms? student management system? sado-masochistic society?
public class sms {
public static void main(String [] args) {
}
}

Related

Open and close 2 distinct Scanners in java project

This is super simplified code of what I was doing, but the results are the same. I can comment out in.close() in class 1 and it will fix the error. But then I'm left with an open Scanner for the rest of the project. And changing the variable names are not a fix.
class1:
package scannerDebug;
import java.util.Scanner;
public class Class1 {
private String name_;
public Class1(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class1 ex1 = new Class1(in.next());
System.out.println("eex1" + ex1.name_);
in.close();
}
}
class2
package scannerDebug;
import java.util.Scanner;
public class Class2 {
private String name_;
public Class2(String name) {
name_ = name; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Your name: ");
Class2 ex2 = new Class2(in.next());
System.out.println("ex2" + ex2.name_);
in.close();
}
}
Driver class
package scannerDebug;
public class driver {
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
}
Closing a Scanner also closes the underlying stream. To solve this, simply use a single Scanner in your driver class and use it in both Class1 and Class2:
public class driver {
private static final SCANNER = new Scanner(System.in);
public static void main(String[] args) {
Class1.main(args);
Class2.main(args);
}
public static Scanner getScanner() {
return SCANNER;
}
}
public Class2(String name) {
name_ = name;
}
public static void main(String[] args) {
System.out.print("Your name: ");
Class2 ex2 = new Class2(driver.getScanner().next());
System.out.println("ex2" + ex2.name_);
}
I recommend you follow proper conventions and change your variable/class names.
You could also pass the Scanner to each class via their respective constructors, but I thought you might want to run Class1 or Class2 without running driver first.

Override toString() method java

I have this in my Main class in java
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
int aaa=0;
System.out.println(aaa);
}
}
I Want to Override that toString() Method that implicity called.
why output is 0 not "lol" ?
why output is 0 not "lol" ?
because you are printing an integer and not an instance of that Main class
you can do the following
public class Main {
#Override
public String toString(){
return "lol";
}
public static void main(String[] args) {
// int aaa=0;
Main myMain = new Main();
System.out.println(myMain);
}
}
note that you can do
System.out.println(myMain);
the same as
System.out.println(myMain.toString());

How can I pass a pointer to some object as argument to a method?

For example, I have such code:
SomeClass item = new SomeClass();
OtherClass.someVoidMethod(item); //there was some changes with item
and then in class, where was called method someVoidMethod(item) item will not change, cause we use in this method copy of that item. In C/C++ there is pointers. I'm looking for something like that in Java.
there is better example:
There i cant edit string from method
public class Main {
public static void main(String[] args) {
String string = "String";
changeIt(string);
System.out.println(string);
}
public static void changeIt(String string){
string = string + " edited";
}
And what suntax I need, to do that? I tied to use *string and so on, but i do this wrong.
i find that this code wil not work the way i want
public class Main {
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
OtherClass.changeIt(someClass.getValue());
}
}
class SomeClass {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
class OtherClass {
public static void changeIt(String string){
string = "someStr";
}
}
so i need to use something like that:
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
someClass.setValue(OtherClass.changeIt(someClass.getValue()));
}
public static String changeIt(String string){
return "someStr";
}
and there is no other way?
To make a Long Story short: use Parameters as Inputs to a method, and use the return value as Output. This will make your code better readable and easier to maintain.
For example:
public class Main {
public static void main(String[] args) {
String string = "String";
String modifiedString = changeIt(string);
System.out.println(modifiedString);
}
public static String changeIt(String string) {
return string + " edited";
}
}
If you wrap your string into a mutable Container, than you can modify it in your method. However for your example code I would strongly recommend to not use this Approach!
class StringContainer {
String content;
}
public class Main {
public static void main(String[] args) {
StringContainer container = new StringContainer();
container.content = "String";
changeIt(container);
System.out.println(container.content);
}
public static void changeIt(StringContainer container) {
container.content += " edited";
}
}
The StringBuilder, as mentioned by #aioobe, would also be a good alternative to use.

Creating a Main method tester

I am trying to create a tester method to display a list.
How do I create a CongressStats object to exercise printPartyBreakdownInSenate method?
package congress;
import java.util.List;
public class CongressMain {
public static void main(String[] args){
}
}
public class CongressStats
{
private int congressNum;
/**
* Create a CongressStats object for the given congress number
*/
public CongressStats(int congressNum)
{
this.congressNum = congressNum;
}
/**
* Calculate and print the number of Democrats, Republicans, and Independents in this Senate
*/
public void printPartyBreakdownInSenate()
{
ok in your main function you would make an object of your class CongressStats.
public class CongressMain {
public static void main(String[] args){
CongressStats cs = new CongressStats(somenumhere);
cs.printPartyBreakdown(); //call your function
}
}
hopefully that helped.

Is this the case of method hiding?

i have following code in which the base class Employee have a static method meth1() which i am able to call from a child class (Pro) object . Is it a case of method hiding or what ? , i am not sure because i haven't implemented the meth1() method in Pro class, but still able to call Emplyee static method from Pro object.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
/*
* public void meth1()
{
System.out.println("inside encapsulation-meth1");
}
*/
public static void main(String as[])
{
Pro e = new Pro();
// e.s ="jay";
e.meth1();
}
}
Output:
inside emp-meth1
Thanks
Jayendra
What are you trying to hide?
Try the below code
emp.meth1() will call method based on reference not based on the object being referred.
class Employee
{
String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
protected static void meth1()
{
System.out.println("inside emp-meth1");
}
}
public class Pro extends Employee {
protected static void meth1()
{
System.out.println("inside encapsulation-meth1");
}
public static void main(String as[])
{
Pro e = new Pro();
Employee emp = new Pro();
emp.meth1(); //this is case of method hiding
e.meth1();
}
}

Categories