【问题标题】:Printing An External File's Contents打印外部文件的内容
【发布时间】:2014-12-14 04:12:05
【问题描述】:

下面是我的家庭作业代码,我需要读取外部文件的内容并确定其中的单词数、3 个字母单词的数量和总数的百分比。我已经把那部分写得很好,但我还必须在显示上述信息之前打印外部文件的内容。以下是我当前的代码:

public class Prog512h 
{
   public static void main( String[] args)
   {
    int countsOf3 = 0; 
    int countWords = 0; 

    DecimalFormat round = new DecimalFormat("##.00"); // will round final value to two decimal places

    Scanner poem = null;
    try 
    {
        poem = new Scanner (new File("prog512h.dat.txt"));
    }
    catch (FileNotFoundException e) 
    {
        System.out.println ("File not found!"); // returns error if file is not found
        System.exit (0);
    }

    while (poem.hasNext()) 
    { 
        String s = poem.nextLine(); 
        String[] words = s.split(" "); 
        countWords += words.length; 
        for (int i = 0; i < words.length; i++) 
        { 
            countsOf3 += words[i].length() == 3 ? 1 : 0; // checks for 3-letter words
        } 
    } 

    while(poem.hasNext()) 
    {       
        System.out.println(poem.nextLine());
    }
    System.out.println();
    System.out.println("Number of words: " + countWords); 
    System.out.println("Number of 3-letter words: " + countsOf3); 
    System.out.println("Percentage of total: " + round.format((double)((double)countsOf3 / (double)countWords) * 100.0)); // converts value to double and calculates percentage by dividing from total number of words
   } 

   }   

声明

while(poem.hasNext()) 
{       
    System.out.println(poem.nextLine());
}

应该打印外部文件的内容。然而,事实并非如此。当我尝试在之前的 while 循环之前移动它时,它会打印出来,但会弄乱我打印的单词数、3 个字母单词、百分比等值。我不太确定这里有什么问题。有人可以提供一些帮助吗?

提前谢谢你。

【问题讨论】:

  • 感谢您的及时建议!我只是试图在没有运气的情况下实现它。内容仍然不打印。 :(

标签: java external file-handling file-read


【解决方案1】:

您的扫描仪正在尝试重新读取文件,但它位于底部,因此没有更多行可读取。你有两个选择:

选项 1

为同一个文件创建一个新的Scanner 对象(从头开始),然后在该文件上调用while 循环(有效,但不是一个很好的设计)。

Scanner poem2 = null;
try 
{
    poem2 = new Scanner (new File("prog512h.dat.txt"));
}
catch (FileNotFoundException e) 
{
    System.out.println ("File not found!"); // returns error if file is not found
    System.exit (0);
}

while(poem2.hasNext()) 
{       
    System.out.println(poem2.nextLine());
}

选项 2

更好的选择是在您读入时显示每一行。这可以通过在已经存在的while 循环中添加额外的一行来实现:

while (poem.hasNext()) 
{ 
    String s = poem.nextLine();
    System.out.println(s); // <<< Display each line as you process it
    String[] words = s.split(" "); 
    countWords += words.length; 
    for (int i = 0; i < words.length; i++) 
    { 
        countsOf3 += words[i].length() == 3 ? 1 : 0; // checks for 3-letter words
    } 
}

这只需要一个Scanner 对象,并且只需要对文件进行一次通读,效率更高。

【讨论】:

  • 尽管我很想回去重做我的代码,但时间限制阻止了在作业到期之前这样做。我尝试了选项一,它成功了;希望我可以回去重做我的代码以供参考。非常感谢,朋友!
  • @Blackout621 第二个选项实际上可能比第一个更快实施。只是听起来更吓人。看看它,因为它的风格要好得多。
  • 哦...这比我想象的要简单得多。我会用那个代替!再次感谢。 :)
  • @Blackout621 没问题。对于未来的实践,不断寻找更好、更有效的解决方案(通常它们实际上更简单)。
猜你喜欢
  • 2011-08-03
  • 2012-06-12
  • 1970-01-01
  • 2021-03-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多