簡體   English   中英

Java流程以及使用InputStream和OutputStream

[英]Java Process and using InputStream and OutputStream

我正在嘗試用Java為其他Java程序編寫一個自動評分程序。 我的Java程序使用ProcessBuilder創建一個進程(帶有重定向的錯誤輸出),然后執行學生的Java類。

問題是,當我嘗試從它們的輸出流中讀取內容時,我被阻止了。

現在作為測試,當我嘗試在終端中單獨運行其程序時,我嘗試一次完成所有輸入,但是由於某種原因,我得到了相同的阻塞行為。 他們的程序要求先輸入一個整數,然后再輸入一串字符串。 如果我先輸入整數,然后再輸入空格,然后輸入空格,則該程序將無法正確運行,並且會被阻塞。

這是學生的課程。 假設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]);
            }
        }



    }
}

當我在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(Kepler)中運行此代碼的完整代碼清單:

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();

  }
}

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM