gpt4 book ai didi

java - 文本未读入变量

转载 作者:行者123 更新时间:2023-12-01 13:22:34 24 4
gpt4 key购买 nike

这是我的代码,我应该读取诸如AAABBBCCCDDD之类的数据并输出a3b3c3d3 .

我已经更新了代码,现在代码可以编译并运行,但是没有任何输出。我不知道这是我读取数据的方式还是代码不正确。

String text;
FileReader data = new FileReader("input.txt");
BufferedReader in = new BufferedReader(data);
text=in.readLine();
in.close();
//Counter looks at length of data
int counter=0;
//Counter2 looks at current letter or number to make see if its the same then iterates it
int counter2=0;
while (text.charAt(counter)<=text.length())
{
while (text.charAt(counter)==text.charAt(counter2+1))
{
counter2++;
}
System.out.println(text.charAt(counter) + counter2);
counter=counter2;
}

最佳答案

编译器提示的原因是因为在这个循环中:

    while (in.readLine()!=null)
{
text = in.readLine();
}

循环体可能永远不会被执行,这意味着 text 可能永远不会被设置为任何内容。当您使用可能尚未设置任何内容的变量时,Java 编译器不喜欢它。

但是整个循环无论如何都是错误的。您只使用一个输入字符串,那么为什么这是一个循环呢?在我们帮助解决此问题之前,我们需要知道您想要实现什么目标。如果您确实想要一个循环,那么像上面那样调用 in.readLine() 两次是错误的,因为这意味着每次都会读取行通过循环。

关于java - 文本未读入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21948917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com