import java.io.*;
import java.lang.*;
public class Test {
public String b[] = {"aaaa","bbbb","cccc","dddd","eeee"};
public BufferedReader in;
public Test() {
in = new BufferedReader(new InputStreamReader(System.in));
} public boolean xunhuan(String s) {
for(int i=0;i<b.length;i++){
if(s.equals(b[i]) ) {
System.out.println("yep");
return true;
}
}
System.out.println("wrong");
return false;
} public static void main(String[] args) throws IOException{
Test temp=new Test();
System.out.println("Enter a word let me correct it:"); String str = temp.in.readLine();
temp.xunhuan(str);
}
}
import java.lang.*;
public class Test {
public String b[] = {"aaaa","bbbb","cccc","dddd","eeee"};
public BufferedReader in;
public Test() {
in = new BufferedReader(new InputStreamReader(System.in));
} public boolean xunhuan(String s) {
for(int i=0;i<b.length;i++){
if(s.equals(b[i]) ) {
System.out.println("yep");
return true;
}
}
System.out.println("wrong");
return false;
} public static void main(String[] args) throws IOException{
Test temp=new Test();
System.out.println("Enter a word let me correct it:"); String str = temp.in.readLine();
temp.xunhuan(str);
}
}
不过从控制台读入字符串,可以
public class Test
{
public static void main(String args[])
{
String str1=args[0];
String str2=args[1];
System.out.println(str1);
} System.out.println(str2);
}例如在命令行下
c:\java Test hello world!
hello就是args[0],world!就是args[1]
在程序中就可以引用args数组
import java.io.*;
import java.lang.*;
import java.sql.*;
import java.util.*;public class test {
public String[] b;
// public String b[]={"aaaa","bbbb","cccc","dddd","eeee"};//storge the data
public BufferedReader in;
public test() {
in = new BufferedReader(new InputStreamReader(System.in));
}
public void DataAccess() throws ClassNotFoundException, SQLException{
ResultSet rs=null;
Connection con=null;
Statement stmt=null;
ArrayList m = new ArrayList();
String query = "select word from words";
String url = "jdbc:odbc:dsn";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con =DriverManager.getConnection(url,"", "");
stmt = con.createStatement(); rs = stmt.executeQuery(query);
while (rs.next()) {
m.add(rs.getString(1));
}
stmt.close();
con.close();
b = new String[m.size()];
for(int i =0 ;i<m.size();i++)
b[i] = (String )m.get(i);
}
public boolean xunhuan(String s) {//check word correst or not
for(int i=0;i<b.length;i++){
if(s.equals(b[i]) ){
System.out.println("yep");
return true;
}
}
System.out.println("wrong");
return false;
}
public static void main(String[] args) throws IOException{
test temp=new test();
System.out.println("Enter a word let me correct it:");//enter word
String str = temp.in.readLine();//read a word from console
temp.xunhuan(str);//test it from xunhuan method
}
}
这是我的全部程序,可以编译,当我输入任何单词的时候他都说
Enter a word let me correct it:
men
java.lang.NullPointerException
at test.xunhuan(test.java:43)
at test.main(test.java:59)
Exception in thread "main"
怎么回事?
这样盲目的做程序没有用。