简体   繁体   中英

Text file manipulation: Take data from text file and write a new text file with the data taken from the original file

I'm writing a program to manipulate some data in a text file. I'm supposed to take state ID, population, and school attendance, and poverty from a file that contains more information than these 4 and write them into a new text file. Then use this new text file to do some maths. This is the original text file structure:
这是原始文本文件结构 I ran into errors in my code and would appreciate some help. To be specific the errors are index out of bounds and non-static method cannot be reference from this context.

Class to define data for extraction:
定义要提取的数据的类 Class where the actual extraction is done:
完成实际提取的类 public PopulationData(String stateId, String totPopulation, String schoolGoingPopuplation, String povertPopulation) { this.stateId = stateId; this.totPopulation = totPopulation; this.schoolGoingPopuplation = schoolGoingPopuplation; this.povertPopulation = povertPopulation; }

  public String getState()
  {
     return stateId;
  }

  public String getPopulation()
  {
     return totPopulation;
  }

  public String getSchool()
  {
     return schoolGoingPopuplation;
  }

  public String getPoverty()
  {
     return povertPopulation;
  }

  public String toString()
  {
     return this.stateId + " " + this.totPopulation + " " + 
     this.schoolGoingPopuplation + " " + this.povertPopulation;
  }   

//this is the main class

         inFile = new InputStreamReader(new FileInputStream(inFileName));

        OutputStreamWriter outFile = new OutputStreamWriter(new FileOutputStream(outFileName));

        while(scanner.hasNext()) {

           String line = scanner.nextLine();

           String[] columns = line.split(" ");

           String sateId = columns[0];

           String totPopulation = columns[9]; 

           String schoolGoingPopuplation = columns[10]; //index out of bounds occured

           String povertPopulation = columns[11]; //index out of bounds occured

           PopulationData pd = new PopulationData(sateId, totPopulation, schoolGoingPopuplation, povertPopulation);
           data.add(pd);

           outFile.write(pd.toString()); 
        } 
        for(PopulationData pd : data){ 
           System.out.println(pd.toString()); 
        }       
     }
     catch(IOException except)
     {
        except.printStackTrace();
     }
  }

Line 28,30,32 : are the fields in the file really separated by the tab character, and exactly one tab character? Doesn't look like it. My guess is it's using spaces, and your split is producing an array of exactly one column, which is the entire line. Given that you have a scanner instance you were probably intended to use that to read the fields with the .nextInt() and .next() methods.

Line 37 : the .add() method is returning a boolean, which you are trying to write to the stream. You probably meant to write outfile.write(pd.toString()) ?

Line 40 : you probably mean System.out.println(pd.toString()) , what you have is effectively PopulationDataManipulation.toString() which doesn't work because it's an instance method and your main is static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM