【问题标题】:Why is there no output when I run my code?为什么我运行代码时没有输出?
【发布时间】:2017-04-05 22:49:15
【问题描述】:

我正在尝试读取一个名为 ecoli.txt 的文件,其中包含 ecoli 的 DNA 序列,并将其内容存储到一个字符串中。我试图打印字符串来测试我的代码。但是,当我运行程序时,没有输出。我还是 java 新手,所以我确信我的代码中有错误,我只需要帮助找到它。

package codons;
import java.io.*;
public class codons 
{

    public static void main(String[] args) 
    {
        try 
        {
            FileReader codons = new FileReader("codons.txt");
            FileReader filereader = new FileReader("ecoli.txt");
            BufferedReader ecoli = new BufferedReader(filereader);
            StringBuilder dna_string = new StringBuilder();
            String line = ecoli.readLine();
            while(line != null);
            {
                dna_string.append(line);
                line = ecoli.readLine();
            }
            String string = new String(dna_string);
            System.out.println(string);
            ecoli.close();
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }


    }

}

编辑:

我仍然无法让程序按照我想要的方式运行,因此我尝试在程序中完成我想要的其余部分的编写,但我仍然没有得到任何输出。无论如何,这就是我现在的位置:

package codons;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;
public class codons 
{
    public static void main(String[] args) 
    {
        try 
        {
            FileReader filecodons = new FileReader("codons.txt");
            FileReader filereader = new FileReader("ecoli.txt");
            BufferedReader ecoli = new BufferedReader(filereader);
            StringBuilder dna_sb = new StringBuilder();
            String line = ecoli.readLine();
            while(line != null)
            {
                dna_sb.append(line);
                line = ecoli.readLine();
            }
            String dna_string = new String(dna_sb);
            ecoli.close();
            BufferedReader codons = new BufferedReader(filecodons);
            StringBuilder codon_sb = new StringBuilder();
            String codon = codons.readLine();
            while(codon != null)
            {
                codon_sb.append(codon);
                line = codons.readLine();
            }
            String codon_string = new String(codon_sb);
            codons.close();
            for(int x = 0; x <= codon_sb.length(); x++)
            {
                int count = 0;
                String codon_ss = new String(codon_string.substring(x, x+3));
                for(int i = 0; i <= dna_sb.length(); i++)
                {
                    String dna_ss = new String(dna_string.substring(i, i+3));
                    int result = codon_ss.compareTo(dna_ss);
                    if(result == 0)
                    {
                        count += 1;
                    }
                }
                System.out.print("The codon '");
                System.out.print(codon_ss);
                System.out.print("'is in the dna sequence");
                System.out.print(count);
                System.out.println("times.");
            }
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }


    }

}

【问题讨论】:

  • 调试时间——打印出while循环中的行以查看发生了什么,打印出文件的路径以确保它是正确的
  • 如果异常类型不是FileNotFoundExceptionIOException,您还应该为Exception 再添加一个catch 块。
  • 我也很困惑,为什么当 line 初始化为 String 时,它会迭代它。这真的没有意义 - 最好是 if 而不是 while
  • 投票结束是一个印刷/微不足道的错误
  • @DrewKennedy 我想从文件中读取多行到一个长字符串。我使用了一个while循环来追加初始化字符串'line'时读取的行,然后在再次追加之前读取下一行,直到没有更多行为止。

标签: java


【解决方案1】:

删除while(line != null)之后的;,会导致无限循环,而不是执行下一条指令。

原因在这里解释:Effect of semicolon after 'for' loop(问题是关于C语言,但在Java中是等价的)。

【讨论】:

  • 我明白为什么要删除分号,但删除后仍然没有输出。
  • @Nunzio 提供的带有测试文件的示例按预期工作,如果您仍然看不到输出,则可能是其他地方或您正在使用的输入文件存在问题
  • 我编辑了我的帖子,更新了我现在的位置。我仍然没有输出。任何反馈将不胜感激。
猜你喜欢
  • 2023-03-04
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
  • 1970-01-01
  • 2018-09-09
  • 1970-01-01
相关资源
最近更新 更多