0

コード:

public static void main(String[] args) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the BookName:");
        String bookName = br.readLine();
        System.out.println("Book Id is: " +bookInfo(bookName).getBookAvail().get(0).getBookID().toString());
        System.out.println("Book Name is: " +bookInfo(bookName).getBookAvail().get(0).getBookName().toString());
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
     try {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the StudentID:");
        String studentID = br.readLine();
        System.out.println("Student Information is: " +studentInfo(studentID));
    } catch (IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

private static ArrayOfBookAvail bookInfo(java.lang.String bookName) {
    org.tempuri.BookService service = new org.tempuri.BookService();
    org.tempuri.IBookService port = service.getBasicHttpBindingIBookService();
    return port.bookInfo(bookName);
}

private static ArrayOfStudInfo studentInfo(java.lang.String studentID) {
    org.tempuri.BookService service = new org.tempuri.BookService();
    org.tempuri.IBookService port = service.getBasicHttpBindingIBookService();
    return port.studentInfo(studentID);
}

}

そして、私が得ている出力は次のとおりです。

Enter the BookName:
Core Servlets and JavaServer Pages, Volume 2:Advanced Technologies
Book Id is: javax.xml.bind.JAXBElement@196da649
Enter the StudentID:
    13MCAL058
Student Information is: org.datacontract.schemas._2004._07.lms.ArrayOfStudInfo@2b08bc5a

適切な出力を得るのを手伝ってください。前もって感謝します。

4

1 に答える 1

0
Book Id is: javax.xml.bind.JAXBElement@196da649

bookInfo(bookName).getBookAvail().get(0).getBookID()を返すように見えますJAXBElementgetValue(),の代わりに呼び出すだけでtoString()、値を取得できます。を呼び出すときに、クラスが継承されたをtoString()オーバーライドしていない場合は、オブジェクトのメモリ ロケーションである が呼び出されます。toString()Object.classObject#toString()

bookInfo(bookName).getBookAvail().get(0).getBookID().getValue();

値が単なるプリミティブまたは文字列であると仮定すると、出力は問題ないはずです。


Student Information is: 
             org.datacontract.schemas._2004._07.lms.ArrayOfStudInfo@2b08bc5a

何科かわかりませんArrayOfStudInfo。しかしObject#toString()、文字列表現を取得するために が呼び出されているのと同じ問題のようです。クラスについて詳しく知らなくても、それが自分ArrayOfStudInfoのクラスであると仮定すると、そのクラスでオーバーライドして、文字列表現にしたいものを表示することができます。例えばtoString()

class ArrayOfStudInfo {
    StudentInfo[] studentInfoArray;

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (StudentInfo info : studentInfoArray) {
            builder.append(info.getFirstName())
                   .append(" ")
                   .append(info.getLastName())
                   .append("\n");
        }
        return builder.toString();
    }
}

今、私はクラスが何で構成されているのか分かりません.私はそこにアイデアを投げかけているだけです. クラスを変更できない場合は、返されたオブジェクトから値を抽出することで、いつでも文字列を作成できます。


ここからのポイントは、非プリミティブ オブジェクトを print ステートメントに入れようとする場合、toString()メソッドによって表される String 表現が必要であるということです。

于 2014-10-11T16:18:14.317 に答える