Java illegal Start of an Expression error - java

class FishingHour
{
public static void main(String args[])
{
public void fishing(){
int totalHoursFishing = 0;
int hoursAllowedFishing = 4;
for(int i=1;i<25;++i)
{
totalHoursFishing = ++totalHoursFishing;
if(hoursAllowedFishing>totalHoursFishing)
break;
System.out.println("Fishing for hours"+i+".");
}
}
}
}
hey guys....i'm just a starter in java language.....
my problem is, that this program is not compiling......& giving me "Illegal start of an Expression" error.......can any'one help me....??/

You have your method fishing() inside the main() method. Methods don't nest that way.

you can not write one method inside another another method.Java does not support nested methods.Move your fishing() outside the main method.
basic structure
class x
{
public static void main(String args[])
{
//codes
}
public void method()
{
//codes
}
}

There is a method inside a method.
you can't do this
fishing() is inside main(). you can't have nested method.

You can not have a method inside another one in Java. So you must put fishing() method outside main() method. For example put it above the main() method in your class.

Methods cannot be nested! main() is a special type of method from which the program starts. Separate fishing() method.

public class NewClassa {
public void fishing(){
int totalHoursFishing = 0;
int hoursAllowedFishing = 4;
for(int i=1;i<25;++i)
{
totalHoursFishing = ++totalHoursFishing;
if(hoursAllowedFishing>totalHoursFishing)
break;
System.out.println("Fishing for hours"+i+".");
}
}
public static void main(String args[])
{
NewClassa classa=new NewClassa();
classa.fishing();
}
}
main is a function and you have written a new function inside main function that was the error.
any way the code is note correct because the if condition is satisfied in the first loop itself and it control goes out of the loop.
break means stop looping
use continue then it will skip the current iteration and move to the next iteration

It is not the correct way to declare a method inside another instead go for method calling and declared outside the main() but inside your class "FishingHour"...

Related

Java program not sure where and how to execute main method whilst using static boolean

I've recently started using java and I am trying to make a program that checks to see if an array can be changed to ascending order by returning true/false.
That is the gist of it however I am having problems with the main class. The error I am getting is that the main method is not found in class.
public static boolean solution (int[] A){
int count = 0;
.......
.......
.......
for(int i=0; i<A.length; i++)
{
if(A[i] != B[i]) count++;
}
if(count > 2) return false;
return true;
}
}
Since I am doing this as my java summer homework I am abit confused as to where I should add the main method. I know it is supposed to be
public static void main (String args[])
However if I were to add that from the start of the code after the class I get errors. Is it because I cannot have
public static boolean and public static void main
in the same class?
Thanks.
You can have public static boolean and public static void main in the same class. A main method can be added anywhere within a class as long as you define it as its own standalone method, don't declare inside another method. For Example:
public class Example{
public void method1(){
....
}
public void method2(){
....
}
public static void main(String[] args){
....
}
}
oke if you are using an ide like intellij you must also configure which class has the main method in you project.
You CAN have a main method and other static methods in the same class. In fact you can have any type of method in the class. The reason your program cannot "find a main method" is because there either isn't one there, or your run configuration is off.
A java program starts, with some exceptions of the initializer blocks, at the main method. So you MUST have it in at least one class in your program.
From what it sounds like you want to test if the array is already in ascending order. Since all arrays that are not empty, can be sorted.
public class Example{
public static void main(String[] args){
int[] test = {1,2,3,4,5};
System.out.println(solution(test));
}
public static boolean solution (int[] a){
//returns true if in a
for (int i = 0; i < a.length-2; i++) {
if (a[i] > a[i + 1])
return false;
}
return true;
}
}
If your cannot find main method error continues. It is probably your run configuration
If you are getting an error, based on the way you phrased your question, it sounds like you have a method with the exact same method signature.
You can not have:
public static void main(String[] args)
{
}
public static boolean main(String[] args)
{
return false;
}
If this is what you have, you should probably rename the second method.

Recursive object access to private methods

Why does the following code print "YO"? Whose printYo() is being called? I would think that this code would not compile because printYo() is private to t.
public class Test {
private void printYo() {
System.out.println("YO");
}
public void doubleTrouble(Test t) {
t.printYo();
}
public static void main(String[] args) {
Test test = new Test();
test.doubleTrouble(new Test());
}
}
What can I do to make sure the outer object doesn't mutate the argument class?
printYo() is private to t
No. That method is private in regards to the class Test. Any piece of code within Test can use it.
What can I do to make sure the outer object doesn't mutate the argument class?
Java does not have any built in mechanism to refuse access to members on a per instance basis. (If that is what you meant.)
You are calling the method with in the class , which sound correct for the output . Even if you call the main method from different class it gives the same output.

Main method not found in class Television, please define the main method as:public static void main(String[] args)

This program must print final parametres about a TV state in three (3) rows...
in first row...on which level loudness is...
in the second on which program is (1., 2., 56, etc...)
third row - is it turned on...
(after all operations inside programm)... this programm is pasted from manual for Java...
class Television {
int volumeTone = 0;
int channelNow = 1;
boolean turnedOn = false;
void turnOn(){
turnedOn = true;
}
void turnOff(){
turnedOn = false;
}
void increaseVolume(){
volumeTone = volumeTone + 1;
}
void decreaseVolume(){
volumeTone = volumeTone - 1;
}
void turnOffVolume(){
volumeTone = 0;
}
void changeChannelUp(){
channelNow = channelNow + 1;
}
void changeChannelDown(){
channelNow = channelNow - 1;
}
int returnChannelBefore(){
return channelNow;
}
int returnToneVolume(){
return volumeTone;
}
boolean isItTurnedOn(){
return turnedOn;
}
void writeParametres(){
System.out.println("Volume loudness now is "+volumeTone);
System.out.println("Channel now is "+channelNow);
System.out.println("Tv is turned on? "+turnedOn);
}
}
You need to have a main method in the class to run...
Your exception itself say that, please define the main method as:public static void main(String[] args)
You need main method in your class Television as shown below.
public static void main(String[] args)
{
//Your logic run this class
}
In Java, you need to have a method named main in at least one class.
Please refer this link about main method.
Without a main method, you will not be able to run anything. Create a new class with a main method like so:
public class myClass
{
public static void main(String [] args)
{
//Here you can create an instance of your television class
Television tel = new Television();
//You can then call methods on your newly created instance
tel.turnOn();
}
}
The main entrance to a java application is with a static main method
which is usually defined as;
public static void main(String []args)
{
}
If you want to run any class make sure it has this method.
You will need to Implement main method
From The Java Tutorials' Hello World Example: (emphasis mine)
In the Java programming language, every application must contain a main method whose signature is: public static void main(String[] args)
...
The main method is similar to the main function in C and C++; it's the entry point for your application and will subsequently invoke all the other methods required by your program.
In other words, when running a Java class, the JVM will look for a main() method in it. That method will be called automatically when running the class, and will be the entry point for the execution flow of the program.
Also, take a look at How does the main method work?.

How to jump to another method?

Is there any function in java that lets you jump to another method?
This is an example how it should work:
if (boolean expression(){
jump to public void something;
if (boolean expression) {
jump to public void something2;
} else{
jump to public void 3;
}
This is needed for a program that checks 3 numbers, and they have different methods linked to them.
If by jump, you mean calling a method:
if (boolean expression(){
something();
if (boolean expression(){
something2();
}
else{
something3();
}
Simply invoke it, in your example
if (boolean expression(){
something();
} else if (boolean expression(){
something2();
} else{
something3();
}
instead of jump to public void something just call something()
There is no syntax called "jump to" although you can easily call a method by firstly making a method.
public void myMethod(){
// do something
}
now when you have done this, you call it from another method by invoking it's name myMethod();. You might know that a method can either return a variable or return nothing (void) if you give it a return-type you MUST return a variable with the same type as your method.
For example:
public int oneplusone(){
int integer = 1 + 1;
return integer;
}
and a void would be like this:
public void oneplusone(){
int integer = 1 + 1;
System.out.println(integer);
}
When you've specified your method you need to call it by invoking its name from another place in your program. Final example:
public class Snippet(){
public static void main(String args[]){ // THIS IS THE MAIN METHOD OF YOUR PROGRAM!
Snippet snippet = new Snippet();
System.out.println(snippet.oneplusone());
}
public int oneplusone(){
int integer = 1 + 1;
return integer;
}
}
I suggest you should check out thenewboston java tutorials explaning java for beginners. Good luck!
if you have the mothod call in the class just call the method like
something(int i);
if your method is in another class make sure you have an object of that class if the method isn't static and call your method
Foo foo = new Foo();
foo.something(10);

How to execute a particular function before main() in C and JAVA ?

I want to execute one function before main function in C and JAVA language.
I know one way that is, by using #pragma directive in C language. Is there any other way to do that in both languages?
I can think of two simple(-ish) ways to do it in Java:
Method #1 - static initializers
For example:
public class Test {
static {
System.err.println("Static initializer first");
}
public static void main(String[] args) {
System.err.println("In main");
}
}
Method #2 - A proxy main.
public class ProxyMain {
public static void main(String[] args) {
String classname = args[0];
// Do some other stuff.
// Load the class
// Reflectively find the 'public static void main(String[])' method
// Reflectively invoke it with the rest of the args.
}
}
You then launch this as:
java <jvm-options> ... ProxyMain <real-Main> arg ...
There is also a 3rd method, but it requires some "extreme measures". Basically you have to create your own JVM launcher that uses a different scheme for starting the application. Have this do the extra stuff before loading the entry point class and calling its main method. (Or do something different entirely.)
You could even replace the default classloader; e.g. How to change default class loader in Java?
in java you can use static block
public class JavaApplication2 {
static {
System.out.println("in static ");
}
public static void main(String[] args) {
System.out.println("in main ");
}
}
As an extension to the C standard gcc provides the function attribute constructor which allows functions to be called before main().
For details please see here (scroll down). Also this SO question and its answers help on this.
Try combining a static block and a static method containing what you want executed before your main method.
package test;
public class Main {
static {
beforeMain();
}
public static void main(String[] args) {
System.out.println("after");
}
private static void beforeMain() {
System.out.println("before");
}
}
Output:
before
after
It could be the first thing you call from your main function. That way it will run before your "real" main function.
Use that method(what you want to run before main method) as your main method.
Then it is so simple
Or use static block before main()
In java, execution of any other method is possible before main() method execution. We need a static initializer block for that. It starts with a static keyword.
Like:
static {
// statements
}
Now try to understand with actual code implementation.
public class BefourMain {
static int sum;
static {
sum = add(4,5,6);
System.out.println("Calling add() method: " + sum);
System.out.println("\ncalling main");
main(null);
System.out.println("Main end\n");
System.out.println("Now JVM calling main()\n");
}
public static void main(String[] args) {
int sum = add(1,2,3);
System.out.println(sum);
}
static int add(int a, int b, int c) {
return a + b + c;
}
}
Now try to understand with actual code implementation.
When we start executing this code the static initializer block will start execution first, in the static initializer block we are calling the add() method and storing the value in the static variable, and printing it. Next, we are calling the main() method, which will execute. when the static initializer block execution will complete JVM will call the main() method again.
Output
Output of the above code

Categories

Resources