Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Not able to compile and it is showing ; expected at the end of public static statement.
//Recursive program
class Recur
{
public static void main(String args[])
void witty(String n, int p)
{
if(p<0)
System.out.println("");
else
{
System.out.println(n.charAt(p)+".");
witty(n, p-1);
System.out.print(n.charAt(p));
}
}
}
It seems that you forgot the body of the main method:
public static void main(String args[]) {
}
Note that the body of a method is delimited by { and }.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is the program I'm trying to run
public class PracticeTwo {
public static void main(int[] args){
int[] lower = {-4,-3,-7};
for(int i=0; i<0; i++){
int[] greater = {5,2,6};
for(int j=0; j>0; j++){
if(greater.length>0);
System.out.println(Integer.toString(j) + " is greater than 0");
if(lower.length<0);
System.out.println(Integer.toString(i) + " is lower than 0");
}
}
}
}
There is no compiling issues but when i try to run it returns this:
Error: Main method not found in class PracticeTwo, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
The problem is in the declaration of your main method, as the error description reads, it should be:
public static void main(String[] args)
Pay close attention to the errors and warnings, they exist for a reason.
This is how it should be listed:
public static void main(String[] args){
System.out.println("Hello World!");
}
Anything inside this method will run when you run the program.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Can anyone please tell me what is this coding language? It has similar structure to Java but I cannot understand.
class Sample {
String student;
#Override
public String toString() {
return "${student}";
}
public static void main(String[] args) {
Sample s = new Sample();
s.student = "Joe"
System.out.println(s.toString())
}
}
This piece of code is valid in groovy... and print "Joe" on standard output.
It is Java, no discussion on this.
But 2 errors :
class Sample {
String student;
#Override
public String toString() {
return student; //<------------------ and not "${student}"
}
public static void main(String[] args) {
s.student = "Joe"; //<---------------- don't forget ';'
}
}
First error is intuitive, I mean you can return "${student}; it will print it, but it's not what the user wanted to do, quite sure
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to execute this below code sample to understand why call to " this " must be first statement in constructor ?? I had read lot of About it and I understand Why this is so !!
so I write the below simple program But Still showing me the same Error even I use 'this' as a First Statement in my Program .
import java.io.*;
import java.lang.*;
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
class ThisDemo
{
public static void main(String []args)
{
Demo obj = new Demo();
}
}
To specifically answer your question, this or super needs to be the first call to ensure the base class has been setup correctly.
https://stackoverflow.com/a/1168356/154186
To solve your error above, remove the void type from the function call. e.g.:
Demo(int x) {
this.x = x;
}
Demo() {
this(50);
}
Remove void from Demo constructors
class Demo
{
int x=23;
Demo()
{
this(55);
}
Demo(int x)
{
this.x=x;
System.out.println("Inside Parameterise Constructor 2"+"\n Value of x:"+x);
}
}
You should remove void.Constructor must have no explicit return type.
Then it will work fine.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am studying Java currently, and I am wondering why this code throws a NullPointerException at the line indicated below. The Question object is a user-defined class that takes two strings as parameters when initializing the object.
public class QuizTime
{
public static void main (String[] args)
{
Quiz qz = new Quiz();
// Throws a NullPointerException
qz.add (new Question ("How may US states are there?", "50"));
}
}
Below is the supporting class. The NullPointerException also indicates a problem with the line "quiz[count] = q;"
import java.util.Scanner;
public class Quiz
{
private Question[] quiz;
private int count;
private final int MAX_QUESTIONS = 25;
public void Quiz ()
{
quiz = new Question[MAX_QUESTIONS];
count = 0;
}
public void add (Question q)
{
if (count < MAX_QUESTIONS)
{
// Throws a NullPointerException
quiz[count] = q;
count++;
}
}
public void Quiz ()
should be
public Quiz()
in order to be considered a constructor and correctly initialize your object. Otherwise, it is a method which you haven't invoked.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I was just figuring out how instance variables are working, but it lead me to something strange.
public class DrumKit {
boolean topHat = true;
boolean snare = true;
void playTopHat() {
System.out.println("ding ding da-ding");
}
void playSnare(){
System.out.println("bang bang ba-bang");
}
}
public class DrumKitTestDrive {
public static void main(String [] args) {
DrumKit d = new DrumKit();
d.playSnare();
d.snare = false;
d.playTopHat();
if (d.snare == true);{
d.playSnare();
}
}
}
How is it possible that it outputs:
"bang bang ba-bang
ding ding da-ding
bang bang ba-bang"
And not: "bang bang ba-bang
ding ding da-ding"
Because what I thought was that the snare only would play once because I'm declaring it after d.playSnare();
to d.snare = false;
Remove the semicolon in this line, which is acting as the body for your if block.
if (d.snare == true);{
Change it to
if (d.snare == true){
Also, d.snare is already a boolean, so you can simplify the conditional expression to:
if (d.snare){
Here's the bang
if (d.snare == true);
Remove ;