This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'
i am new in java.i want to write a program to swap 2 nos.
i have written 2 programs on it.one is running and other is not.
i cant understand the fault of the not running program.pls help me to understand my fault.
here i giving you both the programs along with the output.
the running program:
public class SwapElementsExample {
public static void main(String[] args) {
int num1 = 10;
int num2 = 20;
System.out.println("Before Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
swap(num1, num2);
}
private static void swap(int num1, int num2) {
int temp = num1;
num1 = num2;
num2 = temp;
System.out.println("After Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
}
}
the output is:
before swapping
value of num1 is : 10
value of num2 is : 20
after swapping
value of num1 is : 20
value of num2 is : 10
in the above mentioned program i have not any problem.
but in the next program what is the fault i cannot find.
pls help me to find the error.
class Swap
{
public static void main(int a, int b)
{
int c=0;
c=b;
b=a;
a=c;
c=0;
System.out.println(a);
System.out.println(b);
}
}
in the execution there is no error msg.
but in runtime there is a error msg and that is:
exception in thread "main" java.nosuchmethoderror:main
pls let me know the problem of this program.
public static void main(int a, int b) is not correct.
It must be:
public static void main(String[] args). This is by definition.
If you want to get the first and second argument:
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
problem lies here
public static void main(int a, int b)
Java always starts executing program from the main method you declared in the first sample code.
When you start your java application, the java interpreter tries to find a method
public static void main(String[] args)
to run the application.
In one class you can declare several methods with the same name but with different parameters they get. Like that:
public static void main(String[] args) {
}
public static void main(int a, int b) {
}
public static void main(float a, float b) {
}
And all these methods will be accepted by compiler. Because every method is not identifying by its name only, but by its name and signature. Signature is based on the parameters you pass to a method. Type of every parameter and parameters sequence are the signature body.
So, when you start your app without public static void main(String[] args) method inside, then interpreter is unable to find the main method with expected signature String[] args, and throws the exception.
Related
this is my first question on Stack Overflow, do comment on how I can improve my question-asking.
So basically I have random errors appearing in my Java code, for the semicolon, curly bracket, and sometimes for other random characters. I have no idea why they keep popping up now and then - they only occur sometimes. I've made sure that all the brackets are paired. Here's an example of when the errors appear :
public static *void* main(String[*]* args) *{*
public class TestClass {
int num1 = 1;
int num2 = 1;
int num3*;*
for(int i = 0; i < 2; i++) {
num3 = num1 + num2;
num1 = num2;
num2 = num3;
num3 = num1 + num2;
System.out.println(num3);
}
}
}
All the character between asterisks have errors in my code. All of them say : "Syntax error on token :...."
And if rewrite the exact same code somewhere else, the errors disappear
What's going on?
Oh, and the asterisks aren't actually in my code, they stand for the squiggly red underlinings in the original code.
You declared class inside of a main method - this is incorrect, you should declare method inside of your class instead - this is an example how you could improve your program to make it work correctly:
public class TestClass {
public static void main(String[] args) {
int num1 = 1;
int num2 = 1;
int num3;
for(int i = 0; i < 2; i++) {
num3 = num1 + num2;
num1 = num2;
num2 = num3;
num3 = num1 + num2;
System.out.println(num3);
}
}
}
As mentioned in the other replies you have your class inside your method, instead of your method inside of your class.
When thinking about object oriented programming, your classes should be the concept you are trying to represent and the methods should be what they do.
For example:
public class Dog{
public void bark(){
//foo
}
}
Is like saying there's a dog and it can bark.
However:
public class Bark{
public void dog(){
//foo
}
}
Is like saying there's a bark and it can dog. It doesn't make much sense.
And what you have now is even a bit stranger:
public void bark(){
public class Dog{
//foo
}
}
Which doesn't have any particular meaning in programming. But the best that I could reason would be that something can bark, but also contains a dog. It doesn't make any sense really.
You got the class and main method backwards. The main method goes inside the class:
public class TestClass{
public static void main(String[] args){
// code here
}
}
There are several problems.
1. You are trying to define the method "main" outside of a class. You can't do that.
2. You are trying to define a class inside the main method. That is wrong.
3. You are writing executable code (the loop) inside the class definition but not inside a method of the class.
All of your code needs to be inside
public class Main{
}
Program 1:
public class ValAndRefTypes {
public static void main(String[] args) {
int x=5;
addOneTo(x);
System.out.println(x);
}
static int addOneTo(int num){
num=num++;
return(num);//Outputs 5
}
Program 2:
public class ValAndRefType1 {
public static void main(String[] args) {
int a=2,b=3;
System.out.println("The sum is: " + Add(a,b));
}
static int Add(int num,int num1){
num++;
num1++;
return(num+num1); //Outputs 7
}
Why does the first program not output the incremented value of the variable 'x', but the second program outputs the incremented value of variables 'a' and 'b'?
I also would like to ask whether this has any relation with Value types and Reference types.
TIA for the answer!
Two reasons:
First:
In the first program, the addOneTo function is adding the value and returning the new value (well, attempting to, but we'll get to that below), but that returned value is ignored:
addOneTo(x);
You don't assign the new value to anything. Assign it back to the variable:
x = addOneTo(x);
Whereas in the second program this works because you are using the returned value. You're including it as part of the output:
System.out.println("The sum is: " + Add(a,b));
Second:
This line is very misleading, and is confusing the logic being implemented:
num=num++;
num++ increments num, but evaluates to the previous value of num. So the assignment results in assigning back that previous value. This could work instead:
num = ++num;
Though that's still clouding the logic for no reason. Just increment directly:
num++;
or if you prefer being more explicit:
num = num + 1;
In the second example, that's how you increment:
num++;
num1++;
In your first program, you are printing "x" which has value 5. Only calling a function does not change the value of "x". If you want to print incremented value the program looks like this:
public class ValAndRefTypes {
public static void main(String[] args) {
int x=5;
System.out.println(addOneTo(x));
}
static int addOneTo(int num){
num=num++;
return(num);//It returns 6 not 5
}
Let me make a small change to program 1. You can't mutate a primitive.
public static void main(String[] args) {
int x=5;
int y = addOneTo(x);
System.out.println(y);
}
static int addOneTo(int num){
num=num++;
return(num);
}
I'm trying to show the results from GCD and LCM using the display method. I try accessing the numbers object in the display method and it cannot resolve the symbol. Everything works with the code I'm just not sure how else I can access the numbers object inside the display method. Any help is greatly appreciated! Thanks
public static void main(String[] args) {
TwoNumbers numbers = getNumbers();
System.out.println(numbers.getNum1());
System.out.println(+numbers.getNum2());
GCD(numbers.getNum1(), numbers.getNum2());
System.out.println(GCD(numbers.getNum1(), numbers.getNum2()));
LCM(numbers.getNum1(), numbers.getNum2());
System.out.println(LCM(numbers.getNum1(), numbers.getNum2()));
}
public static TwoNumbers getNumbers(){
int num1;
int num2;
Scanner input = new Scanner(System.in);
System.out.println("Enter your first number: ");
num1 = input.nextInt();
System.out.println("Enter your second number");
num2 = input.nextInt();
return new TwoNumbers(num1, num2);
}
public static int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
public static long LCM(int a, int b) {
return a * (b / GCD(a, b));
}
public static void display(){
}
TwoNumbers numbers = getNumbers();
If you are trying to access numbers of main method. Then you can not do that, scope of numbers is only inside main method. You can not access it directly from other method.
Either you pass the numbers as parameter to display method or you can declare numbers as class level static variable.
public static void display(TwoNumbers numbers){
//Now you have numbers inside display
}
Moreover, you do not need to call the GCD and LCM method again from the display. You can simply pass result of both methods to display method from main.
TwoNumbers gcdNumbers = GCD(numbers.getNum1(), numbers.getNum2());
display(gcdNumbers);
TwoNumbers lcmNumbers = LCM(numbers.getNum1(), numbers.getNum2());
display(lcmNumbers);
I am getting this error that to me looks like I am not calling the method correctly. I have reviewed the past answers here but none have specifically addressed my problem as far as I can see. This is for a class project. I realize my math in the method is most likely not correct yet but I need to get the rest working then deal with an incorrect out put. Thanks a lot!
Here is my code:
import java.util.*;
public class PrintOutNumbersInReverse {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Gather Number
System.out.print("Enter a number between 2 and 10 digits long ");
int num = console.nextInt();
System.out.println("your number is: " + num);
// call method
System.out.println("Your number in reverse is: " + reverse);
}
public static int reverse(int num, int rNum) {
rNum = 0;
while (num != 0) {
rNum = rNum + num % 10;
num = num / 10;
}
}
}
And My error Message:
PrintOutNumbersInReverse.java:28: error: cannot find symbol
System.out.println ("Your number in reverse is: " +reverse);
^ symbol: variable reverse location: class PrintOutNumbersInReverse 1 error
Change method implementation to:
public static int reverse (int num)
{
int rNum = 0;
...
return rNum;
}
and place, that is calling this method to:
System.out.println ("Your number in reverse is: " +reverse(num));
Then should be fine
When copy pasting this into eclipse, i noticed 2 things:
1.) your reverse() method doesn't return an int, but it should because the signature of the method says so: public static int reverse(int num, int rNum). Maybe return rNum, or whatever the logic behind it might be?
2.) second, you have not declared any reverse variable in the main method. Maybe you wanted a parameterized call of reverse()?
Also it looks like, you want in the reverse() method rNum to be an output parameter. In java you can't pass primitives by reference, so whatever you do with rNum inside the method, the changes will only be present in the scope of the method. So you might want to calculate something and actually return the results of your calculations.
You need to use reverse as a method, and not a variable. Also, you are passing in a variable that is not used: rNum. You see in reverse(int num, int rNum); right after you start, it sets your rNum to 0. So why pass a number in that will get set to zero?
I did this from my phone, but this should be working code:
import java.util.*;
public class PrintOutNumbersInReverse {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Gather Number
System.out.print("Enter a number between 2 and 10 digits long ");
int num = console.nextInt();
System.out.println("your number is: " + num);
// call method
System.out.println("Your number in reverse is: " + reverse(num)); //<-- notice how this is a method cause it has "()"
}
public static int reverse(int num) { //<-- this has "int num" in the "()". This is a parameter.
int rNum = 0;
while (num != 0) {
rNum = rNum + num % 10;
num = num / 10;
}
}
}
(Rookie mistake, I'm sure.)
I'm a first year computer science student, and attempting to write a program for an assignment, with the code;
import java.util.Scanner;
public class Lab10Ex1 {
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
int n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}
But upon compiling, I get the error;
Lab10Ex1.java:10: error: cannot find symbol System.out.print(n +
1);
^
symbol: variable n
location: class Lab10Ex1
If someone could explain to me what I've done wrong, or how to fix it, I would greatly appreciate it.
The n variable was declared in the main method and thus is visible only in the main method, nowhere else, and certainly not inside of the calcNumFactors method. To solve this, give your calcNumFactors method an int parameter which would allow calling methods to pass an int, such as n into the method.
public static void calcNumFactors(int number) {
// work with number in here
}
and call it like so:
int n = keyboard.nextInt();
calcNumFactors(n);
You must declare the variable n in public static void calcNumFactors()
In your code, you have to pass the value of n as an argument to the function calcNumFactors() as Hovercraft Full Of Eels said.
import java.util.Scanner;
public class Lab10Ex1 {
private static int n;
public static void main(String[] arg) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type a number: ");
n = keyboard.nextInt();
calcNumFactors();
}
public static void calcNumFactors(){
System.out.print(n + 1);
}
}
In my case, I copied an Enum file from a Grails (.groovy) project and forgot to change the extension to .java