FileReader is already defined in this compilation unit error Java - java

So I'm working on reading in a ".txt" file to use it to implement Dijkstra's algorithm, but every time I try to compile it gives me a "FileReader is already defined in this compilation unit" error while highlighting where I imported it in the beginning. If I take this out, however, it throws a constructor error when I'm trying to read in the file that it's of the wrong type. What am I missing here??
Here is my code:
import java.io.BufferedReader;
import java.io.File;
//import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileReader
{
public ArrayList main1()
{
System.out.println("got here");
try
{
BufferedReader in = new BufferedReader(new FileReader(new File("input1.txt")));
I can provide more if needed, but this is where all of the errors crop up.

Your class is named the same as FileReader in the java.io package (you have commented out above). Rename your class to something else like TextFileReader or InputFileReader or use the fully qualified class name for java.io.FileReader.

Just rename your class "FileReader" to different toxicity, in order not to be confused.

Related

Where am I going wrong in trying to import a class?

Here's the code for the source file:
package moa4;
public class Book {
....
}
And for the destination file:
import moa4.Book;
public class Library {
...
}
The source and the destination are both saved in the same directory with the address:
C:\Users\\java\M\moa4
I'm getting the following error: package moa4 does not exist
You asked Library to import a package moa4.Book but you defined no such package. Instead, you defined a type Book inside package moa4, and that is not consistent with your import directive.
You could either import the package, or make that an import static of the class, but since Book and Library are both in the same package you don't need the import directive at all.
As mentioned, C:/Users/java/M needs to be in your classpath ("-cp" option).

eclipse: import (classname) cannot be resolved

I am new in using eclipse java using multiple .java files. My eclipse java project consist of one project file two package files, each with one .java class
My 2nd java class import the 1st java class/package, like so
VerifyLogin.java
package VerifyLogin;
import ArgumentCountException;
ArgumentCountException.java
// ...
The problem is VerifyLogin.java is getting an error
Import ArgumentCountException cannot be resolved
Or any reference I have to ArgumentCountException cannot be resolved to a type.
In java if you need to import a class then you need to use the full qualified name for that class, as the following:
import packageName.YourClass;
For Example, if your need to use Scanner class, then you need to import it as:
import java.util.Scanner;
But if the class was withing the same package, you don't need to import it.
When importing your class, it should be done as below:
//Current package name for the VerifyLogin Class (All package names should be lowercase by convention)
package packageforcurrentclass;
//Import statements: import thedependencyclasspackage.thedependencyclassname
import exceptionpackage.ArgumentCountException;
public class VerifyLogin
{
...
}

Getting Error for package name in netbeans

I am using one .java file and i have given a package name as com.onlinmebank but netbeans displaying error at this package declaration line as Incorrect Package.
Following is the package declaration code.
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons{
//All Code Here
}
Can Anybody tell me why i am getting this error
All Java keywords are lower-case!
So, this should work:
package com.onlinebank;
import java.sql.*;
import java.util.*;
public class BankCommons {
//All Code Here
}
And keep in mind, that the file BankCommons.java must be placed in the directory com/onlinebank.
In package, p should be small not Package(no captial P).
Also in public class p should be small. Similarily for Import also.
Important note all java keywords are in lower cases

Parsing Xml file using Java throws Exception. Why?

While I was trying to get the attribute of the root element Company I found the following problems, also some exceptions.
But I imported everything I need; then also eclipse says that remove unused import.
I want to know that why it is happening even after i have imported everything,
please give me some idea to remove the bug.
Also is it the way to do xml-parsing? is there any alternative and easy way to
do the same?
import java.io.EOFException;
import java.io.File;
import javax.lang.model.element.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import javax.lang.model.element.Element;
public class DomTest1 {
private static final String file = "test1.xml";
public static void main(String[] args) {
if (args.length>0) {
file = args[0];
}
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File(file));
Element root=document.getDocumentElement();
System.out.println(root.getTagName());
System.out.printf("name: %s%n", root.getAttribute("name"));
System.out.printf("sHortname: %s%n", root.getAttribute("sHortname"));
System.out.printf("mission : %s%n", root.getAttribute("mission"));
} catch(EOFException e) {
e.printStackTrace();
}
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Type mismatch: cannot convert from org.w3c.dom.Element to javax.lang.model.element.Element
The method getTagName() is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
The method getAttribute(String) is undefined for the type Element
at DomTest1.main(DomTest1.java:23)
You have
import javax.lang.model.element.Element;
but you want
import org.w3c.dom.Element;
instead.
Also, regarding this:
also is it the way to do xml-parsing? is there any alternative and
easy way to do the same?
You are using java out of the box-provided ways to do xml parsing. There are several, better alternatives using 3rd part libraries. I'd recommend testing jdom, it is a lot simpler. See example here.
problem is because of this
import javax.lang.model.element.Element;
use this import statement
import org.w3c.dom.Element

Jruby embedded modules and classes

I have a ruby file as follows:
module Example
class Myclass
def t_st
"Hello World!"
end
end
end
now if this was just a class I would be able to use the following java code:
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval(new BufferedReader(new FileReader("example.rb")));
Object example = jruby.eval("Myclass.new");
However, this class rests inside a module. Calling the same code as above produces the error:
Exception in thread "main" org.jruby.embed.EvalFailedException: uninitialized constant myclass
In addition, calling:
Object example = jruby.eval("Example");
The module returns no error. So one would assume this follows the format for Ruby.
Object example = jruby.eval("Example::myclass.new");
Again however, I get the same error as before.
Can anyone help? As there is little documentation on JRuby?
Thanks
Make sure that you do not have syntax errors. Usually I get those errors when I'm not paying attention to what I write...
Secondly, you cannot write the following:
Object example = jruby.eval("Myclass.new");
The reason being that your class is in a module. Instead, use the this:
Object example = jruby.eval("Example::Myclass.new");
Other than that, I don't know what the problem could be. For myself, I was able to run the following code under Java 1.6 and with jruby-engine.jar and jruby-complete-1.4.0.jar under my classpath.
package test;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class MyJavaClass {
public static void main(String arg[]) throws ScriptException,
FileNotFoundException {
ScriptEngine jruby = new ScriptEngineManager().getEngineByName("jruby");
jruby.eval(new BufferedReader(new FileReader("example.rb")));
Object example = jruby.eval("Example::Myclass.new");
jruby.put("a", example);
System.out.println(jruby.eval("$a.t_st"));
}
}

Categories