首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BlueJ -从文本文件读取时BlueJ冻结

BlueJ -从文本文件读取时BlueJ冻结
EN

Stack Overflow用户
提问于 2017-12-07 14:37:59
回答 0查看 112关注 0票数 0

因此,对于我的计算机科学课,我们必须创建一个程序,它将从一个名为"compact.txt“的文本文件中读取数据,并将其中的整数存储到一个int[]中。在此之后,我们必须将数字打印到终端,在文本文件中包含0,然后在文本文件中不包含0。代码会编译并运行,但是当它运行时,它不会向终端打印任何内容,并且它会完全冻结所有BlueJ。在冻结之后,我甚至不能从main内部复制代码,而必须从任务管理器强制关闭它。FileInput是我的类用来读入文件的。下面是我的代码:

代码语言:javascript
复制
import chn.util.*;
public class compact
{
    public static void main(String [] args)
    {
        FileInput fI = new FileInput("compact.txt");
        int[] ar = new int[100];
        String line = fI.readLine();
        fI.close();
        System.out.println(line);
        int count = 0;
        int x = 0;
        while(x < line.length())
        {
            if(!line.substring(x, x+1).equals(" "))
            {
                if(!line.substring(x + 1, x + 2).equals(" ") && !
(line.length() - 1 == x))
                {
                    ar[count] = Integer.parseInt(line.substring(x + 1, x+2));
                }
                else
                {
                    ar[count] = Integer.parseInt(line.substring(x, x+1));
                }
                count++;
            }
            x++;
        }
        System.out.print("Before: " + ar[0]);
        for(int i = 1; i < count; i++)
        {
            System.out.print(", " + ar[i]);
        }
        System.out.print("\n");
        System.out.println("After: ");
        for(int i = 0; i < count; i++)
        {
            if(ar[i] == 0)
            {
                System.out.print("");
            }
            else
            {
                if(i == count - 1)
                {
                    System.out.print(ar[i]);
                }
                System.out.print(ar[i] + ", ");
            }
        }
    }
}

这是包含在"compact.txt“文件中的内容:

0 6 13 0 0 75 33 0 0 0 4 29 21 0 86 0 32 66 0 0

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47689032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档