Why won't my program find my main class? I don't think you need the rest of the parse() function to understand what is wrong... let me know
package help;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class help {
ArrayList<Character> StringList = new ArrayList<Character>();
static char[] data;
String val;
public void main(String[] args){
InputStreamReader ISR = new InputStreamReader (System.in);
BufferedReader BR = new BufferedReader(ISR);
try{
int sCurrentChar;
while ((sCurrentChar = BR.read()) != -1) {
parse((char) sCurrentChar);
}
} catch(IOException e){
e.printStackTrace();
}
}
public void parse(char x){
boolean done =false;
int state =0;
The main() method needs to be static:
public static void main(String[] args) {
...
}
For further information, read Why is the Java main method static?.
Also, I would recommend you to follow Java naming conventions. Member names of the form someMember, and class names of the form SomeClass.
The correct way to declare main method is :
public static void main(String args[]){
........
}
Yes because signature of the main method requires static.
public static void main(String args[])
Only at this point the JVM will recognize the main method as the entry point of the program and will execute.
You will need to have parse method to be static if you want in the same class.
Or else you can use a separate class for parsing..
You need to have a
public static void main(String [] args){
rather than
public void main(String [] args){
You don't have it as static
Related
Say I have two files:
// File Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Scanner stuff here...
Food.fries();
}
}
// File Food.java
public class Food {
public static void fries() {
System.out.println("Some fries...")
// Can I do Scanner stuff here without importing?
}
}
I know that if classes are in the same file, I don't need to import for each class. Now, if I wanted to do operations with Scanner in class Food (separate file), do I need to import Scanner again?
You can import the Scanner class again to be able to use it inside Food class.
But, what I recommend it's you make sc variable as static and global (Scanner sc = new Scanner(System.in);)
So, in your main class you will have to create the variable like:
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Scanner stuff here...
Food.fries();
}
}
And inside your Food class:
public static void fries() {
System.out.println("Some fries...Give me a number:");
Main.sc.nextInt();
}
Those examples work if you have the files in the same package.
Now, you have to be careful here, Because if you have your Main class in another java package (or folder), then, you will have to import the Main class or you can use a static import. You can use that to avoid the use of the syntax Classname.your_static_method(). For example, your Main class:
import java.util.Scanner;
import static yourpackagename.Food.fries;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Scanner stuff here...
fries();
}
}
As you can see now, I used a static import to use the fries() method which you have in the another class. So inside the main, you can call the method with the simple name, and not using the syntax Classname.static_method()
And inside Food class, you can do the same with Scanner object (sc):
import static yourpackagename.Main.sc;
public class Food {
public static void fries() {
System.out.println("Some fries...Give me a number:")
sc.nextInt();
}
}
Those examples work for files in the same package or in separate ones. You only have to do the import correctly.
Now, if you don't want to use a static import and the files or classes are in separate packages, you will have to import the class as always and use the syntax: Classname.static_methodorvariable()
import java.util.Scanner;
import thepackgename.Food;
public class Main {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// Scanner stuff here...
Food.fries();
}
}
Food class:
import thepackagename.Main;
public class Food {
public static void fries() {
System.out.println("Some fries...Give me a number:")
Main.sc.nextInt();
}
}
I'm new to coding and I'm going through an online course (MOOC). I'm on a section about reading user input and I copy-pasted the code from the website into Netbeans but I'm getting errors like illegal start of expression and not a statement. Here's my code:
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
import java.util.Scanner;
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}
I get illegal start of expression for the import and public class lines. I also get a not a statement error for the import line. Lastly I get illegal static declaration for the public static void line.
Any help would be appreciated. Thanks!
The import goes here:
import java.util.Scanner;
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
It is not a property of the class but a reference the compiler needs to be able to tell what "Scanner" (in this context) refers to. In this case it states that Scanner is defined in java.util which is part of the Java Runtime Environment (JRE).
Furthermore as you go on coding you should structure your files into folders.
As soon as you do so you will have to add this type of line in the first line of the file: package folderName.folderName0.
If you want to use one of your classes from another one you will have to add an import like: import folderName.folderName0.JavaApplication2
You've put an entire java source file into a main function. Simply do this instead:
import java.util.Scanner;
public class JavaApplication2 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
}
}
You cannot put import and package statements inside a class. They should be done before class definitions.
Try this code:-
import java.util.Scanner;
// import before class definition.
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}
Type this in the header of the page : import java.util.Scanner;
you missing import important : import java.util.Scanner;
Exmple :
import java.util.Scanner;
public class JavaApplication2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
import java.util.Scanner;
public class ProgramName {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// code here
}
}
}
}
My codes are like the following.
public class readfile {
public static void readfile() {
int i = 0;
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
And it works well if I do not refer to the variable i.
(That means it can print out hello.)
So how can I refer i in the main method?
public class readfile {
static int i;
public static void readfile() {
i = 0;
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
As UUIIUI says in comment, if you declare i inside of readfile(), it is valid only inside of the method.
As Murli says in comment, you need a member variable in the class field. And also it must be static one.
You are writing java code in a bad way :
1.First, the class name char in Java is Uppercase so your class need to be named ReadFile.
You cannot use the i variable in your main method, because it's just a local variable of your readFile method, and you have compilation errors, because of the use of i in the main method, and a warning in the readFile method, because you don't use it in the local block code.
Java appear new for you? and you need to learn a litle bit more. There's a full of book or documentation on the web.
Your sample corrected, compiled well and run well :
package stackWeb;
public class ReadFile {
static int i = 0;
public static void readfile() {
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
You could try this
class readfile {
static int i =0;
public static void readfile() {
System.out.println("hello");
}
public static void main(String[] args) {
readfile();
System.out.println(i);
}
}
Here is my code :
import java.util.Scanner;
public class newCode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String stored = scan.nextLine();
print();
}
public static void print() {
System.out.println(stored);
}
}
I am using eclipse and I am wondering why I cannot do this, I understand it is out of scope or whatever but how can I get this to work without putting my sysout statement in my main function.
You could always pass a value to your print method instead:
// inside of main:
print(stored);
public static void print(String stored) {
System.out.println(stored);
}
The reason you have to is as you describe; stored inside of main is out of scope. Without informing another method of the value you want to use, it has no way to access it.
You can declare your stored at class level :
public class newCode {
static String stored; // You have to declare it static to access directly it in static method or you can also use newCode object if it is not static
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
stored = scan.nextLine();
print();
}
This question already has answers here:
Writing a function inside the main method - Java
(7 answers)
Closed 6 years ago.
Hello I am a want create method into main?
So this is code where i want crete method:
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}}
Java does not allow to create methods within a method. This is a general rule and NOT specific to main method.
Perhaps what you are looking for is to create additional static methods in your class:
public final class MyCommand {
private MyCommand() {
//
}
private static void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
releaseTheCatsOfWar();
}
}
Of course, it would be better to do:
public final class MyCommand {
private MyCommand() {
//
}
private void releaseTheCatsOfWar() {
System.out.println("Meow!");
}
public static void main(String[] args) {
MyCommand that = new MyCommand();
that.releaseTheCatsOfWar();
}
}
you cannot create methods within methods in Java.
You can have it as an additional method of your class and call it form main.
public static void main is not a class, it's a method within class. And your class is input (by the way, you should start class name with an uppercase letter, like Input), in which you define method main, from which your program starts working.
Although it is entirely pointless, the closest way you would get to achieving your results is with the following:
public class Main {
private static interface MethodMaker{
void randomise();
}
public static void main(String[] args) {
MethodMaker methodMaker = new MethodMaker(){
#Override
public void randomise() {
// DO SOMETHING
}
};
methodMaker.randomise();
}
}
You have to put method outside main ,, and just call it in main ,, like this :-
import java.io.*;
import java.util.Random;
class input{
public static void main (String args[]){
Randomises();
}
public void Randomises() {
int writabledata;
Random a=new Random();
writabledata=a.nextInt();
}
}