Ödevim, klasör nesneleri ve dosya nesneleri kullanarak bir dosya sistemi programlamak ve daha sonra aynı ağaçtaki iki nesneyi yönetmek için bir Dosya Sistemi nesnesini kullanmaktır. Benim sorunum, iki nesneyi nasıl aynı şekilde ele alacağımı anlayamıyorum.İki nesneyi bir ağaçta aynı şekilde işlemek için Java özeti
Ödev, "Dosya ve klasörün miras aldığı bir soyut sınıfa sahip olmanıza yardımcı olabilir, böylece bunları aynı şekilde ele alabilirsiniz." Ama hata yapmaya devam ediyorum.
import java.util.*;
public class FileSys {
private Node firstFolder;
public void newFolder(String loc) {
if (firstFolder == null) { // If there are no folders
Node folder = new Folder(loc);
firstFolder = folder;
}
else { // If there are folders
String s = loc;
String[] folders = s.split("\\\\"); // Each file/folder name is put into an array
boolean found; // Flag if found
Node current = firstFolder; //Sets the first folder to the current
int n = 0;
while (folders.length - 1 > n) { // To find the folder being added to
int i = 0;
found = false; // Resets flag
while (current.size > i) { // To search through the names of the folders
if (current.next[i].name.equalsIgnoreCase(folders[n])) {
current = current.next[i];
found = true; // Raises flag
break;
}
i++;
}
if (!found) // incomplete. Add Exception.
System.out.println("ERROR");
n++;
}
Node folder = new Folder(folders[folders.length - 1]);
current.next[current.size] = folder;
}
}
abstract class Node {
String name;
Node(String name) {
this.name = name;
}
}
private class File extends Node {
String data;
File(String nm, String data) {
super(nm);
this.data = data;
}
}
private class Folder extends Node {
private static final int ARRAYSIZE = 20; // default array size
private int size = 0;
private Node[] next = new Node[ARRAYSIZE];
public Folder(String nm) {
super(nm);
next[0] = null;
}
}
// Main method omitted
}
Doğru yönde herhangi bir yardımı takdir ediyorum! Son derece basit bir hata gibi hissediyorum, ama neyin yanlış olduğunu bilmek için nesneler ve özetler konusunda yeterli tecrübem yok. Yayınlamayı denedim, ancak çalışma zamanı sırasında daha fazla hataya neden oldu. Teşekkür ederim!
Düzenleme: İşte
FileSys.java:55: error: cannot find symbol
while(current.size > i)
^
symbol: variable size
location: variable current of type FileSys.Node
FileSys.java:57: error: cannot find symbol
if(current.next[i].name.equalsIgnoreCase(folders[n]))
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:59: error: cannot find symbol
current = current.next[i];
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
current.next[current.size] = folder;
^
symbol: variable next
location: variable current of type FileSys.Node
FileSys.java:76: error: cannot find symbol
current.next[current.size] = folder;
^
symbol: variable size
location: variable current of type FileSys.Node
5 errors
Tam olarak sorun nedir? – immibis
Ne tür hatalar alıyorsunuz? Sınıf kurulumunuz anlamlıdır. – ApproachingDarknessFish
İki nesneyi nasıl ele aldığımı bilmiyorum. Soyut bir Node sınıfı yaptım ama ne zaman Node klasörünü kullanıyorum = new Folder(); Klasör nesnesinde bulunan verilerden herhangi birini veya bir sonraki boyutu kullanamıyorum. – Bleu