This question already has answers here:
Can't use Scanner class, constructor is undefined, method is undefined
(5 answers)
Closed 4 years ago.
package scanner;
import java.util.Scanner;
public class Scanner {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String line = input.nextLine();
}
}
Why am I getting an error message saying 'The import java.util.Scanner' conflicts with a file defined in the same file?
Your own class is named Scanner and you are importing another class named Scanner. This means the compiler does not know which Scanner class you mean when you create a variable of type Scanner.
Try to rename your class to something else.
Alternatively you could use java.util.Scanner this way without renaming your own class:
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
...
}
You should not name your class Scanner because Scanner already exists as a class in Java.
Rename your class.
Rename your class to something else, for example :
package scanner;
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String line = input.nextLine();
}
}
You can't have two Scanners, rename your class to and .java file MyScanner and you're golden.
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();
}
}
Facing error In this java code.I want to print hello world and var1 value.
Input:
import java.util.*;
class Helloworld{
Scanner Scannerobj =new Scanner(System.in);
int var1;
public static void main(String[] args){
System.out.println("hello world");
var1 = Scannerobj.nextInt();
System.out.println("Enter the value of var1");
System.out.println(var1);
}
}
Error:
PS C:\Users\SOUVIK\Desktop> & 'c:\Users\SOUVIK\.vscode\extensions\vscjava.vscode-java-debug-0.31.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-14.0.2\bin\java.exe'
'-XX:+ShowCodeDetailsInExceptionMessages''-Dfile.encoding=UTF-8' '-cp' 'C:\Users\SOUVIK\AppData\Roaming\Code\User\workspaceStorage\1fca3cee0a29c97a3fcadb36b4ef44b2\redhat.java\jdt_ws\jdt.ls-java-project\bin' 'Helloworld'
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot make a static reference to the non-static field var1
Cannot make a static reference to the non-static field Scannerobj
Cannot make a static reference to the non-static field var1
You cannot use non static variables from static context as main is static method.So you either make variables static static Scanner Scannerobj =new Scanner(System.in);
static int var1; or define these variables under main function
Define your scanner object and var1 inside main method as below
import java.util.*;
class Helloworld{
public static void main(String[] args){
int var1;
Scanner Scannerobj =new Scanner(System.in);
System.out.println("hello world");
System.out.println("Enter the value of var1");
var1 = Scannerobj.nextInt();
System.out.println(var1);
}
}
or
Add static keyword while defining scanner object and var1 if you want to define scanner object and var1 outside the main method
import java.util.*;
class Helloworld{
static Scanner Scannerobj =new Scanner(System.in);
static int var1;
public static void main(String[] args){
System.out.println("hello world");
System.out.println("Enter the value of var1");
var1 = Scannerobj.nextInt();
System.out.println(var1);
}
}
For example:
static void lod(){
Scanner j = new Scanner(System.in);
String m = j.next();
}
public static void main(String[] args) {
Scanner j = new Scanner(System.in);
String val = j.next();
}
}
Like, why should I declare the Scanner class object again in the method lod before accessing the scanner class in the method Los.
... why we declare a scanner class object the second time if we want
to use it in a method outside the main method?
You do not have to do it and it is also not a clean way to do it. I would use one of the following ways:
Declare the Scanner object as an instance variable. However, you won't be able to use it directly inside a static method (including main) as you can't access a non-static member inside a static method directly. In a static method, you will be able to access an instance variable only through the instance of the class e.g.
import java.util.Scanner;
public class MyClass {
Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
MyClass obj = new MyClass();
System.out.print("Enter a word: ");
System.out.println(obj.scanner.nextLine());
}
static void lod() {
MyClass obj = new MyClass();
String m = obj.scanner.next();
}
}
Declare the Scanner object as a class variable (i.e. static variable at class level) e.g.
import java.util.Scanner;
public class MyClass {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter a word: ");
System.out.println(scanner.nextLine());
}
static void lod() {
String m = scanner.next();
}
}
Pass the Scanner object to the methods being called from main e.g.
import java.util.Scanner;
public class MyClass {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word: ");
System.out.println(scanner.nextLine());
}
static void lod(Scanner scanner) {
String m = scanner.next();
}
}
You don't need to!
You can declare Scanner object at the class level like this,
class Test {
static Scanner j = new Scanner(System.in);
....
}
In Java the objects can be created inside methodsor outside and so they are only for that method or for the whole class but I don't recomend you to do that becouse every time the class is used the method is created even though you're accesing a method that don't uses it. Instead, you can declarate it outside the method and it will be accessible for all the methods from the class, but it will have to be created separately inside every method and they will be destroyed in the end of the method. Example:
class example{
Canstructor name;
public void method1(){
name = new Constructor();
}
public void method2(){
name = new Constructor();
}
}
When i want import scanner class in my project eclipse show me some errore :
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Scanner(InputStream) is undefined
The method nextLine() is undefined for the type Scanner
and this is my code :
import java.util.Scanner;
public class Scanner {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println(myScanner.nextLine());
}
}
The problem is that you're also declaring a class called Scanner. That means that when you then declare a variable of type Scanner and try to call the constructor, the compiler thinks you're talking about your class. Just change your own class to something else (e.g. Test):
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println(myScanner.nextLine());
}
}
Alternatively you could just fully-qualify the name when you mean java.util.Scanner - but this would be a bad idea in terms of readability.
// Please don't do this - but it would work.
public class Scanner {
public static void main(String[] args) {
java.util.Scanner myScanner = new java.util.Scanner(System.in);
System.out.println(myScanner.nextLine());
}
}
Try
java.util.Scanner myScanner = new java.util.Scanner(System.in);
instead. Else the compiler tries to instantiate your class which is also called Scanner. Or just rename your own Scanner class to something else.
Try to change your class name eg:
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println(myScanner.nextLine());
}
}
Please change the name of your class:
public class Scanner {
to some other name. The compiler is unable to see Scanner as java.util.Scanner because it sees it as your class (which doesn't have such constructor nor method so it gives you errors informing about it).
You should give class name as different then API classes of Java.So just change class name from Scanner to ScannerProgram.
import java.util.Scanner;
public class ScannerProgram {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println(myScanner.nextLine());
}
}
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();
}