java - how to iterate over a text file to perform different tasks (including creating an unknown number of objects) depending on which line I'm reading -


hello i'm low level comp sci student that's struggling/unfamiliar file i/o.

i'm attempting read in text file using buffered reader. understand how use while loop continue scanning until end of file reached how can instruct reader read 1 line , until end of 1 line reached, read next line , until line ends, etc?

basically input text file going repeat every 3 lines. text file represents nodes in weighted directed graph.

the input text file supposedly following:

each node represented 2 lines of text. example on top line, first 's' name of node, second 's' indicates it's start node, third 'n' indicates it's regular node, not goal node, indicated 'g'.

on second line 2 nodes connected 's' first being 'b' weighted distance of 1, , second being 'e' weighted distance of 2.

the third line supposed blank, , pattern repeated.

s s n                      b 1 e 2              b n n c 2 f 3  c n n d 2 ga 4  d n n ga 1  e n n b 1 f 3 h 6  f n n 3 ga 3 c 1  ga n g  h n n 2 gb 2 f 1  n n ga 2 gb 2  gb n g  

my code follows:

public void actionperformed(actionevent e)  {      if(e.getsource() == openbutton)     {         returnval = filechooser.showopendialog(null);          if(returnval == jfilechooser.approve_option)         {             selected_file = filechooser.getselectedfile();              string file_name = filechooser.getselectedfile().getname();             file_name = file_name.substring(0, file_name.indexof('.'));              try             {                 bufferedwriter buff_writer = null;                 file newfile = new file("."+file_name+"_sorted.txt");                             boolean verify_creation = newfile.createnewfile();                 //if (verify_creation)                 //  system.out.println("file created successfully");                 //else                 //  system.out.println("file present in specified location");                  file_reader1 = new bufferedreader(new filereader(selected_file));                 file_reader2 = new bufferedreader(new filereader(selected_file));                  filewriter file_writer = new filewriter(newfile.getabsolutefile());                 buff_writer = new bufferedwriter(file_writer);                  //find number of nodes in file                 while( (currentline = file_reader1.readline()) != null)                 {                     k++;                     //system.out.println("value of k: " + k);                 }                   nodearray = new node[k];                  while( (currentline = file_reader2.readline()) != null)                  {                        //system.out.print(currentline);                           string[] var = currentline.split(" ");                           nodearray[x] = new node(var[0]);                           if (var[1].equals('s') || var[1].equals('s'))                             nodearray[x].settype(nodetype.start);                         else if (var[2].equals('g') || var[2].equals('g'))                             nodearray[x].settype(nodetype.goal);                         else                             nodearray[x].settype(nodetype.normal);                          x++;                  }              buff_writer.close();             file_writer.close();              }                catch (exception e1)             {                 e1.printstacktrace();             }                }     } 

my node class follows:

import java.util.*;   enum nodetype  {     start, goal, normal; }  public class node  {  private string name; private nodetype typeofnode; private final map<node, integer> neighbors = new hashmap<>();   public node(string name) {     this.name = name; }  public void settype(nodetype type) {     typeofnode = type; }  public void addadjacentnode(node node, int distance) {     neighbors.put(node, distance); }     public string tostring() {     string output = "";      output += "node name: " + name + ",\n";      return output; } 

}

my other major problem how handle second line in repeating 3 line sequence. second line gives adjacent nodes , weighted distances node described on first line. problem is, don't know how many adjacent nodes exist given node. technically there none, or maybe large number.

a kind programmer here suggested use hash map record adjacent nodes i'm not sure how structure line of code account indeterminate number of such adjacencies

note: question in reference earlier question asked: how create adjacency matrix, using input text file, represent directed weighted graph [java]?

if point me in right direction eternally grateful

as far input problem goes, while loop treating every line reads identically. have add variable keep track of line in 3-line sequence you're dealing with.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -