一个JAVA读取字符串的问题

betterhuch 2007-10-08 01:03:02
请大家看看下面这个程序:
import java.io.*;
class TinyEdit{
public static void main (String args[]) throws IOException{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop'to quit");
for (int i=0; i<100;i++)
{
str[i]=br.readLine();

if(str[i].trim().equals("stop"))
{ break;}
}
System.out.println("\nHere is your file:");
for(int i=0;i<100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}

当我写了几行之后再写如STOP就发现程序不会终止了.然后我只要在str[i]=br.readLine();后写一句System.out.println(str[i]);程序运行就会变正常了,我不知道是什么原因啊,也许是那个"回车"符号的问题吧,请各位帮帮忙!
...全文
1221 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
betterhuch 2007-10-11
  • 打赏
  • 举报
回复
晕,按回车是肯定知道的拉,可能是我的机子的问题吧,你们都说可以.不管怎么样还是谢谢各位了.
sh_royan 2007-10-10
  • 打赏
  • 举报
回复
我这边一切正常嘛

package test;

import java.io.*;

public class Test {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = new String[100];
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop 'to quit");
for (int i = 0; i < 100; i++) {
str[i] = br.readLine();
//System.out.println("-----------");
if (str[i].trim().equals("stop")) {
break;
}
}
System.out.println("\nHere is your file:");
for (int i = 0; i < str.length; i++) {
if (str[i].trim().equals("stop"))//加个trim() ?
break;
System.out.println(str[i]);
}
}
}
bindian 2007-10-10
  • 打赏
  • 举报
回复
你的程序完全正常得嘛,当你输入了"stop"后要按回车哦
betterhuch 2007-10-09
  • 打赏
  • 举报
回复
大家觉得有没有可能是readLine()这个函数出的问题.也有可能是缓冲区没有刷新的缘故.
lhf165446471 2007-10-09
  • 打赏
  • 举报
回复


在System.out.println("\nHere is you file:");
前加上if(!("stop".equals(str[0].trim())))

即:if(!("stop".equals(str[0].trim())))
{
System.out.println("\nHere is your file:");

}


我在我机子里调试过了,你调试下看下还有什么问题!!
betterhuch 2007-10-08
  • 打赏
  • 举报
回复
上面的程序调试成功了吗?
lhf165446471 2007-10-08
  • 打赏
  • 举报
回复
代码修改下:用两个循环不是嵌套循环。

import java.io.*;

class TinyEdit
{
public static void main(String[] args) throws IOException
{
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop' to quit");

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str[] = new String[100];

for (int i =0;i<100 ;i++ )
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{
break;
}
//System.out.println("\nHere is your file:");
}
System.out.println("\nHere is your file:");
for (int i = 0;i<100 ; i++)
{
if (str[i].equals("stop"))break;
System.out.println(str[i]);
}


}
}


lhf165446471 2007-10-08
  • 打赏
  • 举报
回复
LZ看你的代码好象有些问题呀!
第二个循环是一个嵌套循环呀,i在外循环已经定义了;
内循环的作用是什么?
第二次判断起什么作用?你的意思是不是输入一个字符串,最后的是STOP是退出?

betterhuch 2007-10-08
  • 打赏
  • 举报
回复
你是第一行就写STOP吗?先写其他东西多写几行,然后再写STOP会终止程序吗?
dodober 2007-10-08
  • 打赏
  • 举报
回复
我是太菜了吧,都不知道你们的问题是什么意思,我这里运行正常啊!
betterhuch 2007-10-08
  • 打赏
  • 举报
回复
那样写也是不行的,你去试试就知道了.
gzhiceberg 2007-10-08
  • 打赏
  • 举报
回复
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
问题出在顺序上,当上面一行在输出前面时,实际上输出已经被输入阻塞。
gzhiceberg 2007-10-08
  • 打赏
  • 举报
回复
class TinyEdit{
public static void main (String args[]) throws IOException{
System.out.print("Please enter lines of text.");
System.out.println("Enter 'stop 'to quit");

BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
String str[]=new String[100];

for (int i=0; i <100;i++)
{
str[i]=br.readLine();
if(str[i].trim().equals("stop"))
{ break;}
}
System.out.println("\nHere is your file:");
for(int i=0;i <100;i++)
{
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}

betterhuch 2007-10-08
  • 打赏
  • 举报
回复
你第一行别输STOP试一下,先打其它的内容.
伍子V5 2007-10-08
  • 打赏
  • 举报
回复
你输入stop后要按回车

我这里运行后就结束了输入

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧