Errors when trying to use import java.util.Scanner - java

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
}
}
}
}

Related

Do I kneed to import libraries for different files?

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();
}
}

Access Class methods without prefixing with class

In a program like:
package testing;
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Mouse.pressMouse(1);
}
}
Is there a way to call Mouse.pressMouse() without prefixing using Mouse?
You can import static methods:
import static com.company.Mouse.pressMouse;
public static void main(String[] args) {
pressMouse(1); // No need to prefix with "Mouse."
}
If the method pressMouse is static, then you could Static Import your method(s),
import MarcoLib.Mouse;
import MarcoLib.Timings;
import static MarcoLib.Mouse.pressMouse;
public class Testing {
public static void main(String[] args) {
pressMouse(1);
}
}
and per the link,
So when should you use static import? Very sparingly!
If the method pressMouse isn't static, then you could extend Mouse,
import MarcoLib.Mouse;
import MarcoLib.Timings;
public class Testing extends Mouse {
public static void main(String[] args) {
new Testing().pressMouse(1);
}
}

Why won't Eclipse find my main class

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

Java import class System

I have a question on class imports, It seems you can call a method with a reduced line if you have imported the class. I don't understand what is the name of this operation, and how is it possible...
For instance :
Why this code
public class test
{
public static void main (String args[])
{
System.out.print("Test");
}
}
Can be replaced by
import static java.lang.System.out;
public class test
{
public static void main (String args[])
{
out.print("Test");
}
}
What happens if you have also an object named "out" ?
Thanks in advance
What happens is that out from external class must be referenced by full name:
String out = "Hello World";
java.lang.System.out.println(out);
The variable out will shadow the static import and you will have to use the full name in order to use the function print.
import static java.lang.System.out;
public class Tester5 {
public static void main (String args[]) {
int out=0;
out.print("Test");
}
}
yields "cannot invoked print(String) on primitive type int. The same error is shown if out is an object.

why am I getting this error

Why Am I getting an error. In eclipse it says constructor call should be the first line. It is the first line. or you can not extend Main?
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
public Main(){
super("title bar");
}
}
}
Your Main constructor should sit outside of the main method. Like so:
public class Main extends JFrame {
public Main() {
super("title bar");
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//JLabel testLabel1 = new JLabel();
}
}
The constructor should be outside public static void main(String[] args) {
It's a function and you can't have a constructor inside a function .
You're trying to define a constructor (public Main) within a static method. That's not valid in Java.
You probably meant something more like this:
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Main extends JFrame{
/**
* #param args
*/
public static void main(String[] args) {
}
// The constructor isn't *inside* `main` anymore:
public Main(){
super("title bar");
}
}
There are multiple errors.
The first error is that you're defining the constructor of the class in a method. This is illegal which results in the compiler complaining that it expected the new keyword instead of public.
Secondly, super class methods have to be called in the first line. But since, the compiler is now confused due to the previous error, it has reported so.
You might also want to improve on the class naming convention. It is very easy to get confused between the main(String args[] method, which is the entry point into your code, the Main class, and its constructor Main() (which would generated by the compiler).

Categories