今天在測試ant的時候,寫了以下代碼: java
package com.laolang.hello; import java.util.Scanner; import com.laolang.stu.Student; public class HelloWorld { public static void main(String[] args) { Scanner input = new Scanner( System.in ); Student stu = null; int id = 0; String name = null; int age = 0; String sex = null; System.out.print("輸入學號:"); id = Integer.parseInt( input.nextLine() ); System.out.print("輸入姓名:"); name = input.nextLine(); System.out.print("輸入年齡:"); age = Integer.parseInt( input.nextLine() ); System.out.print("輸入性別:"); sex = input.next(); stu = new Student( id, name, age, sex ); System.out.println("student:"); System.out.println(stu); System.out.println("Hello World!"); System.out.println("小代碼!"); input.close(); } }
package com.laolang.stu; public class Student { public Student() { super(); } public Student(int stuId, String stuName, int stuAge, String stuSex) { super(); this.stuId = stuId; this.stuName = stuName; this.stuAge = stuAge; this.stuSex = stuSex; } @Override public String toString() { return "Student [stuId=" + stuId + ", stuName=" + stuName + ", stuAge=" + stuAge + ", stuSex=" + stuSex + "]"; } public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public int getStuAge() { return stuAge; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public String getStuSex() { return stuSex; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } private int stuId; private String stuName; private int stuAge; private String stuSex; }
run: [java] 輸入學號: 1001 [java] 輸入姓名: [java] 輸入年齡: tian [java] Exception in thread "main" java.util.InputMismatchException [java] at java.util.Scanner.throwFor(Scanner.java:909) [java] at java.util.Scanner.next(Scanner.java:1530) [java] at java.util.Scanner.nextInt(Scanner.java:2160) [java] at java.util.Scanner.nextInt(Scanner.java:2119) [java] at com.laolang.hello.HelloWorld.main(HelloWorld.java:23) BUILD FAILED因而百度之,在CSDN上看到了能夠用BufferReader來讀取,通過測試,沒有問題:
package com.laolang.hello; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import com.laolang.stu.Student; public class HelloWorld { public static void main(String[] args) { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); Student stu = null; int id = 0; String name = null; int age = 0; String sex = null; try { System.out.print("輸入學號:"); id = Integer.parseInt(br.readLine()); System.out.print("輸入姓名:"); name = br.readLine(); System.out.print("輸入年齡:"); age = Integer.parseInt(br.readLine()); System.out.print("輸入性別:"); sex = br.readLine(); } catch (NumberFormatException | IOException e) { e.printStackTrace(); } stu = new Student(id, name, age, sex); System.out.println("student:"); System.out.println(stu); System.out.println("Hello World!"); System.out.println("小代碼!"); } }
run: 1001 小代碼 23 男 [java] 輸入學號:輸入姓名:輸入年齡:輸入性別:student: [java] Student [stuId=1001, stuName=小代碼, stuAge=23, stuSex=男] [java] Hello World! [java] 小代碼! BUILD SUCCESSFUL Total time: 12 seconds
laolang@laolang:~/code/ant/li/stu/classes$ java com.laolang.hello.HelloWorld 輸入學號:1001 輸入姓名:tianya 輸入年齡:23 輸入性別:nan student: Student [stuId=1001, stuName=tianya, stuAge=23, stuSex=nan] Hello World! 小代碼!
使用ant運行的時候雖然能夠正常輸入,可是沒有輸入,而後使用java命令就沒有問題,看來仍是應該在ant中執行shell下的 java.com.xx.xx.XX才能夠 shell