This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'
I'm hoping this is just a simple error here, I've looked up numerous other instantces of people getting the same error message but none of their solutions really seem to apply for me. I was just wondering if you guys could help me find the error in my code. I'm not even sure if it's functional because I can't get it to run, so I suppose it could be a logic error.
When I try to run the following cold I am met with fatal exception error occured. Program will exit.
Eclipse also gives me:
java.lang.NoSuchMethodError: main
Exception in thread "main"
Thank you very much for any assistance you can offer!
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JoPuzzle
{
public static Integer main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of soliders");
int soldiers = input.nextInt();
System.out.println("Please enter the how many soldiers are skipped before the next death");
int count = input.nextInt();
List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
for (int i = 1; i <= count; i++) {
soldiersList.add(i);
}
int currentIndex = 0;
while(soldiersList.size() > 1) {
currentIndex = (currentIndex - 1 + count) % soldiersList.size();
soldiersList.remove(currentIndex);
}
return soldiersList.get(0);
} //end main
}//end class
We know that to execute any java Program we should have a main function. because this is a self callable by JVM.
And the signature of the function must be..
public static void main(String[] args){
}
but in your code it's seem like this...
public static Integer main(String[] args){
}
so its consider as a different function , so change your main return type..
The signature for main method is
public static void main(String[] args).
When you run your program the JVM will look for this method to execute. You need to have this method in your code
Your program does not contain the main method, change it to
public static void main(String[] args)
If you want to return an Integer object, then define a custom mehod and call that method inside the main method and handle that returned value.
Java main() function doesn't have a return-statement. This line
public static Integer main(String[] args)
should be
public static void main(String [] args)
Also since it has no return value, you should delete the return statement.
Java's main method should have below signature.
public static void main(String[] args){
..
..
}
Try to run this code and tell if it works.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class JoPuzzle
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number of soliders");
int soldiers = input.nextInt();
System.out.println("Please enter the how many soldiers are skipped before the next death");
int count = input.nextInt();
List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
for (int i = 1; i <= count; i++) {
soldiersList.add(i);
}
int currentIndex = 0;
while(soldiersList.size() > 1) {
currentIndex = (currentIndex - 1 + count) % soldiersList.size();
soldiersList.remove(currentIndex);
}
// return soldiersList.get(0);
System.out.println(soldiersList.get(0));
} //end main
}//end class
Related
How can I read-in an integer in this following program? It doesn't work. It doesn't compile at the moment.
/**
* Main class of the Java program.
*
*/
import java.util.Scanner;
//...
class Scanner{
Scanner in = new Scanner(System.in);
int num = in.nextInt();
}
public class Main {
public static void main(String[] args) {
// we print a heading and make it bigger using HTML formatting
System.out.println("<h4>-- Binaere Suche --</h4>");
int anzahl = 0; int zahl;
}
}
import java.util.Scanner;
// This will print what you want but will not make it look bigger as it
// will get printed in console
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
// we print a heading and make it bigger using HTML formatting
System.out.println("<h4>--"+num+" --</h4>");
}
}
In C i do this:
int main(){
int N = 0;
while(scanf("%d",&N) != EOF){ // <--- how to do this condition in java?
... Code ...
}
My input is some numbers: 10 20 5 9 7 6... and when i hit enter my output is some matrix.
Print with example
I know there is another topic, here, similar to this, but not solve my problem.
Any suggestion?
My problem is solved by Elliot, i just do this:
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
int N = 0;
#SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
N = (int) scanner.nextDouble();
... code ...
}
}
Working fine.
This question already has answers here:
Why can't I do assignment outside a method?
(7 answers)
Closed 7 years ago.
why cant i use System.out.println out side a method and when i try to use the method of my main class in other class it shows identifier not found
may be there are better way to do this . but i would like to learn this what is wrong here
package arraytest; //package declared
import java.util.Scanner; // for input
public class Arr { // this is my main class
void min(int arr[])
{
int min=arr[0];
for(int i=1;i<arr.length;i++)
{ if(min>arr[i])
min=arr[i];
}
System.out.println(min);
}
public static void main(String[] args)
{
Array a=new Array();
a.inputData();
a.display();
a.min();
}
}
class Array
{
int arr[]=new int[5];
Scanner sc= new Scanner(System.in);
// System.out.println(arr.length); will not work why?
void inputData()
{
for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
}
void display()
{
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
void min()
{
Arr a=new Arr();
a.min(int arr[]); // this shows error
}
}
You can't put code there. Your code has to be inside of a method. The 2 lines above that are declarations, and the System.out.println is code.
Code has to go inside a method.'
You could use a static block if your arr was static, but it's not.
Change you min method as below,
void min() {
Arr a = new Arr();
a.min(arr); // this shows error
}
This is how you pass a reference of an array to the method.
My aim here is to type a simple code to take in number input from the user and print a simple matrix. The code I typed seems to compile but doesn't work during run time! My code is something like this:
import java.util.Scanner;
class Arr
{
public static void main()
{Scanner in=new Scanner(System.in);
int a[ ][ ]=new int[2][3];
for(int i=0;i<2;i++)
{
for(int y=0;y<2;y++)
{
a[i][y]=in.nextInt();
}
}
for(int i=0;i<2;i++)
{
for(int y=0;y<2;y++)
{
System.out.print(a[i][y]);
}
}
}
}
At the same time could you suggest a solution if I were to transpose the matrix inputted by the user?
You have defined the main method incorrectly. The proper main method signature is
public static void main (String[] args)
That's why the compiler is not compiling your code.
String s contains a set of integers separated by white space (blanks, tabs, or newlines). Return the sum of the integers.
You can use a Scanner object to solve this problem. Create a new Scanner(s) and store it in a variable, say in. Then, use in.hasNextInt() to control a while loop. Each iteration of the while loop uses in.nextInt() to get the next integer from the String s. Accumulate this integer into a variable and return that variable when the loop exits.
You may use a main method to test your method by creating an instance of the Calculator class and calling sum(…) with several combinations of values using that instance.
For example, sum(“3 4 5 27 3”) is 42.
I have written this so far:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
}
public int sum(String s){
int i = 0;
Scanner in = new Scanner(s);
while (in.hasNextInt()) {
sum = sum + in.nextInt();
}
return sum;
}
}
To call the method from main and display the results make the sum method static and then call it and print the result:
public static void main(String[] args) {
System.out.println(sum("1 2 3"));
}
import java.util.Scanner;
public class Calculator{
public static void main(String[] args){
System.out.println(sum("1 2 3"));
}
public static int sum(String s){
int i = 0;
int sum = 0;
Scanner in = new Scanner(s);
while (in.hasNextInt()) {
sum = sum + in.nextInt();
}
return sum;
}
}