请大家帮我看看下面这个代码是什么问题呢?
//Exception.java
import java.io.*;
import java.io.IOException.*;
class TestException
{
public static void main(String[] args)
{
int count;char ch='t';
byte buf[]=new byte[255];
System.out.println("请输入字符:");
try
{
count=System.in.read(buf);
String str=new String(buf,0,count-2);
System.out.println(str);
}
catch(IOException ioe)
{
if(this.in.reader()==ch)///在这个位置老是出错
System.out.println("不能输入字符't'");
}
}
}
在运行的时候编译器显示:
TestException.java:19: package this does not exist
if(this.in.reader()==ch)
^
1 error
请问下该怎么改正呢?
请知道的给我说一下.谢谢了
//Exception.java
import java.io.*;
import java.io.IOException.*;
class TestException
{
public static void main(String[] args)
{
int count;char ch='t';
byte buf[]=new byte[255];
System.out.println("请输入字符:");
try
{
count=System.in.read(buf);
String str=new String(buf,0,count-2);
System.out.println(str);
}
catch(IOException ioe)
{
if(this.in.reader()==ch)///在这个位置老是出错
System.out.println("不能输入字符't'");
}
}
}
在运行的时候编译器显示:
TestException.java:19: package this does not exist
if(this.in.reader()==ch)
^
1 error
请问下该怎么改正呢?
请知道的给我说一下.谢谢了
import java.io.IOException.*;
class TestException
{
public static void main(String[] args)
{
int count;char ch='t';
byte buf[]=new byte[255];
System.out.println("请输入字符:");
try
{
count=System.in.read(buf);
String str=new String(buf,0,count-2);
if(str.equals("t"))
System.out.println("不能输入字符't'");
else
{
System.out.println(str);
}
}
catch(IOException ioe)
{
}
}
}
呵呵
2 static修饰的方法中不能用this
import java.io.IOException.*;
class TestException
{
public static void main(String[] args)
{
String szLine;
System.out.println("请输入字符:");
try
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
szLine = br.readLine();
while(!szLine.equals("bye")){
// 检查是否包含字符't'
if(szLine.indexOf('t') == -1)
System.out.println(szLine);
else{
//System.out.println("不能输入字符't'");
// 或者改为抛出异常
throw new IOException("不能输入字符't'");
}
// 再次输入,直到输入'bye'
szLine = br.readLine();
}
br.close();
}
catch(IOException ioe)
{
// 打印出异常
ioe.printStackTrace();
}
}
}