mrsengineer mrsengineer - 28 days ago 23
Java Question

How reading from text file into stack and queues using doubly linked list in java

I explain my program. This program have two files and two files have student no, student name and surname.


  • Example:

  • queues.txt :

  • Student_No#Name#Surname

  • 1234#Jane#Weber#

  • 1235#Johnson#Roy

  • 1267#Henry#Morin

  • stack.txt:

  • Student_No#Name#Surname

  • 3456#Jane#lee

  • 7535#Johnson#Perez

  • 1967#Henry#Fortin



How do I read and throw stack and queue?
Program has Data class , Node class , Stack class , Queue Class and Main Class.
I did every class except main class because I cant read file after throw stack and queue.

public static void main(String[] args) throws IOException {

Stack stack = new Stack();
Queues queue = new Queues();

File stackfile = new File("stack.txt");


if (!stackfile.exists()) {
stackfile.createNewFile();
} else {
System.out.println("File is done");
}
FileReader r= new FileReader(stackfile);
BufferedReader reader = new BufferedReader(r);

String line = null;
Data data= new Data(); // this class have String name, surname and string number

int i=1;
while ((line=reader.readLine())!=null) {

{ if(line.trim().equals("#")){
stack.Push(data);
data=new Data();
i=1;
}
else{
if(i==1){
data.setNo(line);
}
else if(i==2){
data.setName(line);

}
else if(i==3){
data.setSurName(line);
}
i++;

}
}





}
stack.Push(data);
reader.close();

File queuefile = new File("queue.txt");

if (!queuefile.exists()) {
queuefile.createNewFile();
} else {
System.out.println("File is done");
}

BufferedReader read = null;
read = new BufferedReader(new FileReader(queuefile));
String lines = read.readLine();


while (lines != null) {
System.out.println("Read from queue: " + lines);


{ if(lines.trim().equals("#")){
queue.insert(data);
data=new Data();
i=1;
}
else{
if(i==1){
data.setNo(line);
}
else if(i==2){
data.setName(line);

}
else if(i==3){
data.setSurName(line);
}
i++;

}
}


}
queue.insert(data);
read.close();

}
}

Answer

Here is the corrected code. Your loop body did not do what you intended. If you want to split a text with a delimiter like #, use the split function of String. Here you can find out how to use split.

public static void main(String[] args) throws IOException {

    Stack stack = new Stack();
    Queues queue = new Queues();

    File stackfile = new File("stack.txt");

    if (!stackfile.exists()) {
        stackfile.createNewFile();
    } else {
        System.out.println("File is done");
    }
    FileReader r = new FileReader(stackfile);
    BufferedReader reader = new BufferedReader(r);

    String line = null;

    // Skip headline
    reader.readLine();

    while ((line = reader.readLine()) != null) {

        String[] splitLine = line.trim().split("#");

        if (splitLine.length == 3)
            stack.Push(new Data(splitLine[1], splitLine[2], splitLine[0]));
    }

    reader.close();

    File queuefile = new File("queue.txt");

    if (!queuefile.exists()) {
        queuefile.createNewFile();
    } else {
        System.out.println("File is done");
    }

    BufferedReader read = null;
    read = new BufferedReader(new FileReader(queuefile));

    // Skip headline
    read.readLine();

    while ((line = read.readLine()) != null) {

        String[] splitLine = line.trim().split("#");

        if (splitLine.length == 3)
            queue.insert(new Data(splitLine[1], splitLine[2], splitLine[0]));
    }

    read.close();

}