0

back with new features in 'file.txt' are these lines:

   [noduri1]
   {x0;140;104;31;50;}
   {x1;361;132;55;50;}
   [arce1]
   {a0;160;143;386;173;0;1;}
   [noduri2]
   {x2;192;82;27;50;}
   {x3;174;263;27;50;}
   [arce2]
   {a0;200;119;196;300;0;1;}
   [end_graph] 

These lines represent two directed graph's draw, each one in two different JPanels, but the writefile function puts them together in 'file.txt'.

The problem begins in readfile class when I want to open any graph drawn, I receive this exception:

Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)

readfile code is:

          package pachet;
          import java.io.File;
          import java.io.FileReader;
          import java.io.BufferedReader;
          import java.io.IOException;
          import java.util.Vector;
          import java.util.StringTokenizer;
          public class FileOperationRead {
private File file;
private Vector lines;
private aArce1 arcele = new aArce1();
private aNoduri1 nodurile = new aNoduri1();
    private bArce2 arcul = new bArce2();
    private bNoduri2 nodul = new bNoduri2();
private FileOperationRead() {
    lines = new Vector();
}
public FileOperationRead(File file) {
    this();
    this.file = file;
    loadList();
    createListaNoduri1();
    createListaArce1();
            createListaNoduri2();
    createListaArce2();
}
public FileOperationRead(String pathAndFileName) {
    this();
    this.file = new File(pathAndFileName);
    loadList();
    createListaNoduri1();
    createListaArce1();
            createListaNoduri2();
    createListaArce2();
}
private void loadList() {
    String line;
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        line = br.readLine();
        while (line != null) {
            line = StringExt.blankEliminator(line);
            //add the lin to list
            lines.add(line);
            //read a new line from file
            line = br.readLine();
        }
    } catch (IOException e) {
    }
}
private void createListaNoduri1() {
    aNod nodet = new aNod();

    String line;
    StringTokenizerDebugDecorator st;
    int i = 1;

    //no nodes !!!
    if (lines.size() < 8) {
        return;
    }
    while (!((String) lines.get(i)).toUpperCase().equals("[ARCE1]")) {
        line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator (line, ";");
        //creating the node
        nodet = new aNod();
        nodet.setLabel(st.nextToken());
        nodet.setX(Integer.parseInt(st.nextToken()));
        nodet.setY(Integer.parseInt(st.nextToken()));
        nodet.setWidth(Integer.parseInt(st.nextToken()));
        nodet.setHeight(Integer.parseInt(st.nextToken()));
        nodet.setSelected(false);

        nodurile.add(nodet);

        //next node...
        i++;
    }
}
private void createListaArce1() {
    aArc arct = new aArc();

    String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
    while (!((String) lines.get(i)).toUpperCase().equals("[ARCE1]")) {
        i++;
    }
    i++;
    if (i == lines.size() - 1) {
        return;
    }
    while (i < lines.size() - 1) {
        line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
        //creating the arc
        arct = new aArc();
        arct.setLabel(st.nextToken());
        arct.setX1(Integer.parseInt(st.nextToken()));
        arct.setY1(Integer.parseInt(st.nextToken()));
        arct.setX2(Integer.parseInt(st.nextToken()));
        arct.setY2(Integer.parseInt(st.nextToken()));
        arct.setaNodSrc(nodurile.getaNod(Integer.parseInt(st.nextToken())));
        arct.setaNodDest(nodurile.getaNod(Integer.parseInt(st.nextToken())));
        //add the arc to the arcs list
        arcele.add(arct);

        //add the arc to the arcs list
        arcele.add(arct);

        //next arc...
        i++;
    }
}


   private void createListaNoduri2(){
        bNod nodel = new bNod();
        String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
        while (!((String) lines.get(i)).toLowerCase().equals("[end_graph]")) {
        i++;
    }
    i++;
            if (i == lines.size() - 1) {
        return;
    }
            while (i < lines.size() - 1) {
                line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
                    nodel = new bNod();
        nodel.setLabel(st.nextToken());
        nodel.setX(Integer.parseInt(st.nextToken()));
        nodel.setY(Integer.parseInt(st.nextToken()));
        nodel.setWidth(Integer.parseInt(st.nextToken()));
        nodel.setHeight(Integer.parseInt(st.nextToken()));
        nodel.setSelected(false);

        nodul.add(nodel);
                    i++;
            }

    }
   private void createListaArce2(){
       bArc arcl= new bArc();
       String line;
    StringTokenizerDebugDecorator st;
    int i = 1;
        while (!((String) lines.get(i)).toUpperCase().equals("[END_GRAPH]")) {
        i++;
    }
    i++;
            if (i == lines.size() - 1) {
        return;
    }
            while (i < lines.size() - 1) {
                line = (String) lines.get(i);
        //eliminate the '{}'
        line = line.substring(1, line.length() - 1);
        //creating the tokenizer
        st = new StringTokenizerDebugDecorator(line, ";");
                    arcl = new bArc();

        arcl.setLabel(st.nextToken());
        arcl.setX1(Integer.parseInt(st.nextToken()));
        arcl.setY1(Integer.parseInt(st.nextToken()));
        arcl.setX2(Integer.parseInt(st.nextToken()));
        arcl.setY2(Integer.parseInt(st.nextToken()));
        arcl.setbNodSrc(nodul.getbNod(Integer.parseInt(st.nextToken())));
        arcl.setbNodDest(nodul.getbNod(Integer.parseInt(st.nextToken())));
        arcul.add(arcl);
                    i++;

   }
   }
public aNoduri1 getaNoduri1() {
    return ((aNoduri1) nodurile.clone());
}
public aArce1 getaArce1() {
    return ((aArce1) arcele.clone());
}
    public bNoduri2 getbNoduri2(){
        return ((bNoduri2) nodul.clone());
    }
    public bArce2 getbArce2() {
    return ((bArce2) arcul.clone());
}

}

  • 2
    You need the whole stack trace. There should be a line with one of your classes mentioned. – Philip Whitehouse Jan 10 '13 at 18:53
  • You should see the API, it will help you a lot. Hint: You're trying to toknize with a character that doesn't exists in one of the lines. – Maroun Jan 10 '13 at 19:00
  • 2
    it should also be really easy to see what's going on by stepping this through a debugger, and watching the value of 'line' – GreyBeardedGeek Jan 10 '13 at 19:12
  • using StringTokenizerDebugDecorator i was able to see when nextToken stops, it stops after [noduri2](from file.txt) doesn't prints x2,x3, a0 – user1967726 Jan 21 '13 at 21:40

0 Answers0