我試圖從另一個單獨的類中呼叫類FileRead和方法Display和Contents,但是它在同一個包中。這是我的主要課程。它在 Eclipse 中作業。我可以輸入一個文本檔案并顯示它。該類還可以在命令提示符下編譯和運行。
public class FileRead
{
static String theFile;
Scanner myScanner = new Scanner (System.in);
static List<String> fileArray = new ArrayList<String>();
static StringBuffer stringBuff = new StringBuffer();
public FileRead()
{
System.out.println ("Enter your file: ");
theFile = myScanner.nextLine();
try
{
myScanner = new Scanner(new File(theFile));
FileReader myFileReader = new FileReader(theFile);
BufferedReader buffRead = new BufferedReader(myFileReader);
while ((theFile = buffRead.readLine()) != null)
{
fileArray.add(theFile);
}
buffRead.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public String Contents()
{
for (String str : fileArray)
{
stringBuff.append(str);
stringBuff.append("\n");
}
String str = stringBuff.toString();
return (str);
}
public void Display()
{
for (int i = 0; i < fileArray.size(); i )
{
System.out.println ("Line # " (i 1) " " fileArray.get(i));
}
}
public static void main (String[] args)
{
//FileRead textFile = new FileRead();
//textFile.Display();
//textFile.Contents();
}
}
我遇到的問題是這個類:
public class FileReadShow
{
public static void main (String[] args)
{
try
{
FileRead myTextFile = new FileRead();
myTextFile.Contents();
myTextFile.Display();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
當我嘗試在命令提示符下編譯它時,我得到了這個:
javac filereadshow.java
filereadshow.java:112: error: cannot find symbol
FileRead myTextFile = new FileRead();
^
symbol: class FileRead
location: class FileReadShow
filereadshow.java:112: error: cannot find symbol
FileRead myTextFile = new FileRead();
^
symbol: class FileRead
location: class FileReadShow
2 errors
我做了一些搜索,發現了這些錯誤的主要原因。我沒有發現任何錯別字。我還嘗試從其中一個類中洗掉一個主要方法,但這也沒有奏效。我仍然不確定的一件事是未宣告的變數,這是我的第一個問題。
1:我是建構式的新手,它看起來對我來說是正確的,但問題可能是我錯誤地呼叫了對FileRead建構式所在物件的參考嗎?
2:我已將錯誤范圍縮小到僅此陳述句,FileRead myTextFile = new FileRead();因為無論我如何處理它,這就是錯誤所在。有人可以幫我確定問題出在FileRead腳本本身還是物件參考上FileRead myTextFile = new FileRead();?
謝謝你的時間!
編輯: FileRead.class 在當前目錄中,因為我能夠編譯它。我在嘗試 FileReadShow 之前編譯了它。
編輯#2:我切換了 IDE 并找到了更多資訊,這些資訊給了我兩個編譯錯誤描述:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: -FileRead cannot be resolved to a type
-FileRead cannot be resolved to a type
到目前為止,似乎還沒有一個非常明確的答案。我目前正在處理對其他人有用的修復程式。比如重啟eclipse/PC,使用檔案下拉選單中的“clean”選項,創建一個新的類等等。到目前為止還沒有任何效果。我會繼續努力。有沒有人有更多我可以嘗試的建議?
編輯#3:我的兩個 .java 檔案都在包檔案夾中,其中包含 Eclipse 的所有內容(.CLASSPATH、.project、src 檔案夾、bin 檔案夾等)。通常,當我在命令提示符下更改目錄時,我會轉到這個包檔案夾并且可以輕松編譯。我的確切命令提示符輸入是:
C:\Users\D>cd eclipse-workspace\packageone\src\packageone
C:\Users\D\eclipse-workspace\PackageOne\src\PackageOne>javac fileread.java
//As you can see the first one compiled, so I call the second below
C:\Users\D\eclipse-workspace\PackageOne\src\PackageOne>javac filereadshow.java
filereadshow.java:90: error: cannot find symbol
FileRead myTextFile = new FileRead();
^
symbol: class FileRead
location: class FileReadShow
filereadshow.java:90: error: cannot find symbol
FileRead myTextFile = new FileRead();
^
symbol: class FileRead
location: class FileReadShow
2 errors
編譯的第一個 .java 檔案 (FileRead.java),第二個 (.FileReadShow.java) 仍然導致錯誤。我究竟做錯了什么?我已經用這種方式編譯了幾十個程式,還沒有遇到過這個......?
uj5u.com熱心網友回復:
您必須將.java檔案放在正確的目錄結構中,并考慮您的包。
因此,如果您有:
package x;
class Foo {}
和
package x;
class Bar {
Foo foo;
}
它們應該在路徑上x/Foo.java并且x/Bar.java相對于您運行的目錄javac。
所以如果你javac x/Foo.java跑javac x/Bar.java
你會得到:
.
└── x
├── Bar.class
├── Bar.java
├── Foo.class
└── Foo.java
javac Bar.java找的時候Foo.class就找x/Foo.class,因為Foo在包里x。因此,您的檔案所在的目錄結構必須反映您的類的包結構。
uj5u.com熱心網友回復:
源檔案名必須與類名完全匹配。所以它應該是FileRead.java,FileReadShow.java而不是fileread.java和filereadshow.java你一樣。此外,您對未命中excerpt變數宣告進行采樣。
PS 在 Java 中,通常camelCase用于變數和方法名稱。所以最好把名字改成contents()and display()。而且我們不會在打開花括號之前插入換行符。
uj5u.com熱心網友回復:
我不確定答案是什么。我能夠使用以下命令在命令提示符下編譯這兩個程式:
javac packageone\fileread.java packageone\filereadshow.java
但是,當我嘗試運行該FileReadShow程式時,它說存在編譯錯誤,并參考了相同的缺失符號。我只是將這兩個類嵌套在一起,就這樣結束了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520856.html
