【问题标题】:Having trouble with text file input in Java在 Java 中输入文本文件时遇到问题
【发布时间】:2015-05-23 07:47:20
【问题描述】:

我的教授真的蒙着眼睛把我们扔进了这个项目。我们没有深入探讨在 Java 中使用和插入文件。我收到大量错误,这很可能是由于我错误地插入了文件。我将文本文件保存在我的计算机上保存类文件的同一位置,假设这是必要的。我已经在我的电脑上的多个地方移动了它,试图让它工作。这是主程序。如果完全不正确,我很抱歉。

为了解释我们应该进一步做什么,这里是指向带有伪代码的提示的链接。我没有尝试执行所有列出的操作,因为我还没有正确插入文件。 http://jcsites.juniata.edu/faculty/rhodes/cs1/projects/program9Gen.html

编辑:这是整个程序的辉煌。作为我们对 Java 类的介绍,该类是在一个单独的项目中创建的。我们只是被告知再次使用它并将主程序插入底部只是为了便于评分。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GenSeq
{
   private String seq;
   private String species;
   private String startCodon;
   private String stopCodon;
   private String shape;
   private String chromosomeLocation;

   public GenSeq (String seq, String species, String startCodon, String stopCodon,
   String shape, String chromosomeLocation){
     this.seq = seq;
     this.species = species;
     this.startCodon = startCodon;
     this.stopCodon = stopCodon;
     this.shape = shape;
     this.chromosomeLocation = chromosomeLocation;
   }
   //Allowing the program to later set constructors

   //Creating all the appropriate getters and setters for the instance variables
   public void setSpecies(String newSpecies){
      species = newSpecies;
   }

   public String getSpecies(){
      return species;
   }

   public void setStartCodon(String newStartCodon){
      startCodon = newStartCodon;
   }

   public String getStartCodon(){
      return startCodon;
   }

   public void setStopCodon(String newStopCodon){
      stopCodon = newStopCodon;
   }

   public String getStopCodon(){
      return stopCodon;
   }

   public void setShape(String newShape){
      shape = newShape;
   }

   public String getShape(){
      return shape;
   }

   public void setChromosomeLocation(String newChromosomeLocation){
      chromosomeLocation = newChromosomeLocation;
   }

   public String getChromosomeLocation(){
      return chromosomeLocation;
   }

   public String toString(){
     return "Sequence length: " + seq.length() +
                       "\nSpecies: "+ species +
                       "\nStart Codon: "+ startCodon +
                       "\nStart Codon: "+ stopCodon+
                       "\nShape: "+ shape +
                       "\nChromosomal Location: " + chromosomeLocation;
     //Creating a toString method to hold all the class data
   }
}



public static void main (String args[ ])
{
  GenSeq seqA = null;
  //Setting constructor to default if not set
  //Opening the file
  Scanner inputStream = null;
  String seq;

  try
  {
    inputStream = new Scanner (new File ("W:\jcsites.junata.edu\students\morrian14\seq.txt"));
  }
  catch (FileNotFoundException e)
  {
    System.out.println ("Error opening the file ");
    System.exit (0);
  }
  do{
    inputStream = inputStream.trim();
    if ('>' == inputStream.charAt(0)){
      seq = inputStream.nextLine();
    }
  }
  while (inputStream.hasNextLine());
  inputStream.close();


}

这个错误是同一个不断重复的

文件:C:\LEXIPC\Users\Alexis\GenSeq.java [行:83] 错误:需要类、接口或枚举

【问题讨论】:

  • 欢迎来到 StackOverflow。该程序的预期效果是什么,它现在在做什么?如果您想增加获得有用答案的机会,请参阅How to Ask。另外,如果您有错误,请发布堆栈跟踪。
  • 这里完全不需要 {do while},虽然我不确定你为什么要问。
  • Scanner.charAt()Scanner.trim() 不存在。此代码无法编译。
  • 您是否导入了正确的参考文献?这就是你所有的代码吗?此外,您的 IDE 应该在此处发布错误描述。
  • 我编辑了原始帖子以获得完整的代码和程序的完整说明。很抱歉造成混乱。

标签: java file text inputstream insertion


【解决方案1】:

一个明显的问题,最后一行显然是写成 inputStream.close(); 而不是 input.Stream.close(); 你可能也需要一个 try .. catch ... 来关闭流

【讨论】:

    【解决方案2】:

    您的问题到底是什么?一些注释虽然... 摆脱 do{} while() 并执行以下操作:

    while(inputStream.hasNextLine(){
        if('>' == inputStream.charAt(0))
            seq = inputStream.nextLine();
    }
    inputStream.close();
    

    我有点困惑你为什么要回收seq 来读取文件,因为这就是你使用的文件名。更好的方法是使用 File 类作为文件名。考虑:File seq = new File(.../filename.txt)。 此外,如果您发现使用了太多的 try/catch 块,请考虑使用异常处理类来清理您的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2021-06-19
      • 2023-03-12
      • 1970-01-01
      • 2016-08-24
      相关资源
      最近更新 更多