My Code has a problem, either if my first void is static or not i get a error that i cant seem to fix.


This is the error i get if the void is static

Java Compiler By Noj
All underpreciated java will soon apear
All errors within java files will soon apear

RPGmain.java:29: non-static method mainmenu(java.lang.String) cannot be referenc
ed from a static context
                mainmenu(playername);
                ^
1 error
Press any key to continue . . .

if its not static it compiles fine, but it wont run i get this error when i attempt to run the code.

Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue . . .

This is my code.

import java.util.Scanner;

public class RPGmain {
	
    
    		public int hp = 30;
		public int str = 25;
		public int def = 20;
		public int intel = 20;
		public int bronzecoins = 50;
		public int silvercoins = 25;
		public int goldcoin = 1;
		public int level = 1;
		public int exp = '1';
		public int makerestart = 1;
		
		
                	
	public void Openning (String args[]) {
		
		System.out.println("Beware this game is not saveable");
		System.out.println("What is your name?");
		Scanner scan = new Scanner(System.in);
		String playername = scan.next();
		System.out.println("hello " + playername);
		System.out.println("Welcome to Text RPG");
		
		mainmenu(playername);
		
	}
	
	
	public void mainmenu (String playername) {
		
		while (makerestart <= 200000);
			{
		
		makerestart += 2;
		
		
		System.out.println("What would you like to do " + playername + "?");
		System.out.println("Type in help if your new");
		Scanner whatsnext = new Scanner(System.in);
		String nextaction = whatsnext.next();
		
		if(nextaction == "stats"); {
			System.out.println("hitpoints: " + hp);
			System.out.println("Strength: " + str);
			System.out.println("Defence: " + def);
			System.out.println("intelligeance: " + intel);
			System.out.println("Bronze: " + bronzecoins);
			System.out.println("Silver: " + silvercoins);
			System.out.println("Gold: " + goldcoin);
			System.out.println("Combat Level: " + level);
			}
		}
	}
}

only part that really matter about the error is the top area so i removed a bit from the bottom (the rest was all more if(nextaction == "text") anyway)

i was told making the first void static fixes the
Exception in thread "main" java.lang.NoSuchMethodError: main
problem, only way i had that work and the code compile is when i added in a empty void
something like

public static void empty (String args[]) {

	}

you need an actual method named main when starting the application and it does need to be static

It looks like you need to change Opening method to be main

i got it working, i made all the voids and variables static.
what does "static" do? making all the voids/variables static will effect the coding how?


and also
how would i clear text from

System.out.println

it makes text in the window (the game window so far is just the cmd dos window that batch coding uses also) i tryed System.out.println("cls") since "cls" i batch clears the text, but it didint work, obviously lol.

Here is an example for your problem followed by a link from this forum:

Static:

class Test {
public static printMyName() {
  System.out.println("My name IS  NEO");
}

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

   //or

   Test.printMyName();

   //both will work
}
}

Non Static:

class Test {
public printMyName() {
  System.out.println("My name IS  NEO");
}

public static void main(String [] args) {
   Test test = new Test();
   test.printMyName();
}
}

http://www.daniweb.com/forums/thread143361.html

Static is meant to be thought of as that it doesn't apply to a specific instance of the object or class. As java addict's example, you can printMyName at any time without creating an instance.

Here is an example for your problem followed by a link from this forum:

Static:

class Test {
public static printMyName() {
  System.out.println("My name IS  NEO");
}

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

   //or

   Test.printMyName();

   //both will work
}
}

Non Static:

class Test {
public printMyName() {
  System.out.println("My name IS  NEO");
}

public static void main(String [] args) {
   Test test = new Test();
   test.printMyName();
}
}

http://www.daniweb.com/forums/thread143361.html

wont let me do anything like that, first methode has to be
public static void main(String [] args);
it wont allow me to have anything but variables above it, no methods nothing.
wont even allow me to have a methode without a 'type' such as
public static unnamed();

but thats beside the point.

i got the code to work but when it gets to here

public static void main(String args[]) {
		
		System.out.println("What is your name?");
		Scanner scan = new Scanner(System.in);
		String playername = scan.next();
		System.out.println("hello " + playername);
		System.out.println("Welcome to Text RPG");
               [B] mainmenu(playername);[/B]
               
                
	}
	
	
	public static void mainmenu(String playername) {
		
		while (makere....

it doesent bring me to mainmenu(); and because it doesent do that im not sure if it even sends the value of (playername) to the methode either. should work with static just like you said too :/

Create mainmenu as a separate class NON STATIC

from main call this

Mainmenu mainmenu = new Mainmenu(playername);

mainmenu.run();


// in the mainmenu class
private String playerName;
public void mainmenu (String playername) {
this.playerName = playername;
}

public void run(){
while (makerestart <= 200000);
//... continue on with your original mainmenu code
}

wont let me do anything like that, first methode has to be
public static void main(String [] args);
it wont allow me to have anything but variables above it, no methods nothing.
wont even allow me to have a methode without a 'type' such as
public static unnamed();

/

That is because i wrote this:

public printMyName() {

}

When I should have written this:

public void printMyName() {

}

It was a simple mistake and you should have been able to correct it and not say that it cannot be done


Add a System.out.println("") at the begining of the mainmenu() method and see if it is called:

public static void mainmenu(String playername) {
   Sytem.out.println("mainmenu method called with: "+playername);
}

Does the playername printed? :

System.out.println("hello " + playername)

Here is an example for your problem followed by a link from this forum:

Static:

class Test {
public static printMyName() {
  System.out.println("My name IS  NEO");
}

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

   //or

   Test.printMyName();

   //both will work
}
}

Non Static:

class Test {
public printMyName() {
  System.out.println("My name IS  NEO");
}

public static void main(String [] args) {
   Test test = new Test();
   test.printMyName();
}
}

http://www.daniweb.com/forums/thread143361.html

That is because i wrote this:

public printMyName() {

}

When I should have written this:

public void printMyName() {

}

It was a simple mistake and you should have been able to correct it and not say that it cannot be done


Add a System.out.println("") at the begining of the mainmenu() method and see if it is called:

public static void mainmenu(String playername) {
   Sytem.out.println("mainmenu method called with: "+playername);
}

Does the playername printed? :

System.out.println("hello " + playername)

ya the problem is with the variable 'makerestart' most likely, thanks for the help all problems are fixed for know, im new to java, just a couple days in, just catching in quick because i know a few other programming languages.


Does the playername printed? :

System.out.println("hello " + playername)

yes

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.