2014-02-15 59 views
0

我想在java中爲其他java程序編寫自動評分程序。我的java程序使用ProcessBuilder創建一個進程(帶有重定向的錯誤輸出),然後執行學生的java類。Java進程和使用InputStream和OutputStream

問題是,當我嘗試從輸出流中讀取數據時,我遇到阻塞。

現在作爲測試,當我嘗試在終端中單獨運行他們的程序時,我嘗試立即執行所有輸入,但出於某種原因,我得到了相同的阻止行爲。他們的程序要求輸入一個整數,然後輸入一串字符串。如果我輸入整數,然後輸入一個空格鍵,那麼該程序將無法正常工作,並且會被阻止。

這是學生的課程。假設Mid對象只是一個整數和一些字符串。試運行這個程序,然後輸入實際上只是在像鍵入...

2 123456 matt smith 2 123455 jim bob 2 4 <- after the 4 hit enter 

,觀看默認的「阻塞」程序的行爲。

import java.util.*; 
public class HW3 
{ 
    public static Mid createMid() 
    { 
     Scanner in = new Scanner(System.in); 
     Mid myMid = new Mid(); 


     System.out.print("Alpha? "); 
     myMid.alpha = in.next(); 
     System.out.print("First name? "); 
     myMid.firstName = in.next(); 
     System.out.print("Last name? "); 
     myMid.lastName = in.next(); 
     System.out.print("Company? "); 
     myMid.company = in.nextInt(); 


     return myMid; 
    } 
    public static void printMid(Mid x) 
    { 
     System.out.println("" + x.alpha + " " + x.lastName + " " + x.firstName + " " + x.company); 

     return; 
    } 
    public static void main(String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.println("Welcome to my HW3"); 

     System.out.print("How many mids? "); 
     int numberMids = in.nextInt(); 
     System.out.println("Is this your number..."+numberMids); 
    Mid[] arrayMids = new Mid[numberMids]; 
     for(int i = 0; i < numberMids; i++) 
     { 
      arrayMids[i] = createMid(); 
     } 

     System.out.print("What company would you like to print out? "); 
     int printCompany = in.nextInt(); 
     for(int j = 0; j < numberMids; j++) 
     { 
      if(printCompany == arrayMids[j].company) 
      { 
       printMid(arrayMids[j]); 
      } 
     } 



    } 
} 

回答

0

當我在Eclipse中運行它(並創建一個Mid類)時,此程序適用於我。你需要做的一件事就是打印你實際進入的公司。如果你研究你建議輸入的輸入行,這些公司都是2,但你要求打印公司4

當我這樣做,沒有打印出(當然),但程序運行完成就好了。

當問及打印,如果我進入我實際上進入輸入公司,這裏是我所看到的:

Welcome to my HW3 
How many mids? 2 
Is this your number...2 
Alpha? 123456 
First name? matt 
Last name? smith 
Company? 2 
Alpha? 123455 
First name? jim 
Last name? bob 
Company? 2 
What company would you like to print out? 2 
123456 smith matt 2 
123455 bob jim 2 

哦,請務必關閉掃描儀。在完成使用資源時,清理資源只是一種很好的編程習慣。

完整代碼列表我曾經在Eclipse(開普勒)運行以下命令:

import java.util.Scanner; 

public class HW3 { 

    public static class Mid { 
    String alpha; 
    String lastName; 
    String firstName; 
    int company; 
    } 

    public static Mid createMid() { 
    Scanner in = new Scanner(System.in); 
    Mid myMid = new Mid(); 

    System.out.print("Alpha? "); 
    myMid.alpha = in.next(); 
    System.out.print("First name? "); 
     myMid.firstName = in.next(); 
    System.out.print("Last name? "); 
    myMid.lastName = in.next(); 
    System.out.print("Company? "); 
    myMid.company = in.nextInt(); 

    in.close(); 

    return myMid; 
    } 

public static void printMid(Mid x) { 
    System.out.println("" + x.alpha + " " + x.lastName + " " + x.firstName 
     + " " + x.company); 

    return; 
    } 

    public static void main(String[] args) { 
    Scanner in = new Scanner(System.in); 
    System.out.println("Welcome to my HW3"); 

    System.out.print("How many mids? "); 
    int numberMids = in.nextInt(); 
    System.out.println("Is this your number..." + numberMids); 
    Mid[] arrayMids = new Mid[numberMids]; 
    for (int i = 0; i < numberMids; i++) { 
     arrayMids[i] = createMid(); 
    } 

    System.out.print("What company would you like to print out? "); 
    int printCompany = in.nextInt(); 
    for (int j = 0; j < numberMids; j++) { 
     if (printCompany == arrayMids[j].company) { 
     printMid(arrayMids[j]); 
     } 
    } 

    in.close(); 

    } 
} 

HTH

+0

hmmmm ...由於某種原因,當我在一個標準的Ubuntu終端執行它,我仍然被阻塞.... – Matthew

+0

您使用的是哪個版本的JDK?我使用1.7。我運行的是Ubuntu 12.04,它也可以從命令行運行。 –

相關問題