my output of my java application is running null values for the output of Q4 and 5, tried fixing it but had no luck swapping out the null values for the actual string question needed to be shown just before the user input
MultipleChoiceQuestions.java
import java.util.Arrays;
import java.util.Scanner;
public class MultipleChoiceQuestion extends Question{
private int count = 0 ;
private String cho1, cho2, cho3, cho4 ;
private boolean bool1, bool2, bool3, bool4 ;
private final String[] newchoice = new String[4];
public MultipleChoiceQuestion(String text, String cho1, boolean bool1,
String cho2, boolean bool2,
String cho3, boolean bool3,
String cho4, boolean bool4) {
super(text);
this.cho1 = cho1; this.cho2 = cho2;
this.cho3 = cho3; this.cho4 = cho4;
this.bool1 = bool1; this.bool2 = bool2;
this.bool3 = bool3; this.bool4 = bool4;
}
public void addChoice(String Choices, boolean isCorrect){
this.newchoice[count]= Choices;
if(isCorrect) {
super.setCorrectResponse(Choices);
}
count++ ;
}
public void initQuestion(){
addChoices(cho1, bool1);
addChoices(cho2, bool2);
addChoices(cho3, bool3);
addChoices(cho4, bool4);
}
#Override
public String toString(){
StringBuilder sbf = new StringBuilder(super.toString());
sbf.append(Arrays.toString(newchoice));
return sbf.toString();
}
private void addChoices(String cho1, boolean bool1) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}
FillInTheBlankQuestion.java
import java.util.Scanner;
public class FillInTheBlankQuestion extends Question{
public FillInTheBlankQuestion(String text){
super(text);
}
public void extractQA(){
Scanner scan = new Scanner(super.getQuestionText());
scan.useDelimiter("_");
super.setQuestionText(scan.next());
super.setCorrectResponse(scan.next());
}
public String toString(){
return super.toString() + "______________";
}
}
Question.java
import java.util.Scanner;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
/**
*
* #author --
*/
public class Question {
private String questionText;
private String correctResponse;
//Creates a question with an empty question and answer
public Question(){
this.setQuestionText("");
this.setCorrectResponse("");
}
public Question(String text){
this.setQuestionText(text);
}
public Question(String text, String answer){
this.setQuestionText(text);
this.setCorrectResponse(answer);
}
//sets the text of this question
public void setQuestionText(String text){
this.questionText = text;
}
//sets the answer for this question
public void setCorrectResponse(String answer){
this.correctResponse = answer;
}
public String getQuestionText(){
return this.questionText;
}
public String getCorrectResponse(){
return this.correctResponse;
}
/*
checks the response/answer given for correctness
#param givenResponse The response to check
return true if the response is correct, false otherwise
*/
public boolean verifyAnswer(String givenResponse){
//it does not take into account upper/lower case characters.
return givenResponse.equalsIgnoreCase(this.getCorrectResponse());
}
//allows user to type the answer
public void inputAnswer(Scanner scan){
System.out.print("Type your answer:");
System.out.println(this.verifyAnswer(scan.nextLine()));
}
//display the question
#Override
public String toString(){
return this.getQuestionText();
}
}
QuestionTester.java
import java.util.Scanner;
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
/**
*
* #author ---
*/
public class QuizTester {
public QuizTester(){
Scanner in = new Scanner(System.in);
//declare an array quiz that can hold a mixture of Question and Fill in blank type
Question[] quiz = new Question[6];
quiz[0] = new Question("Which class is used to get user input?", "Scanner");
quiz[1] = new Question("How many primitive data types are there in Java? Enter in words.", "eight");
quiz[2] = new FillInTheBlankQuestion("The inventor of Java was _James Gosling_");
quiz[3] = new FillInTheBlankQuestion("Every class in Java inherits from _Object_");
quiz[4] = new MultipleChoiceQuestion("What represents the collection of related data?",
"String", false,"Array", true, "Integer", false,
"Iterator", false);
quiz[5] = new MultipleChoiceQuestion("Which method does not belong to Scanner class?",
"nextInt()", false,
"next()", false,
"nextboolean()", false,
"nextChar()", true);
for(int i = 0; i < quiz.length; i++){
if(quiz[i] instanceof FillInTheBlankQuestion){
((FillInTheBlankQuestion)quiz[i]).extractQA();
}
System.out.println("Q" + (i + 1) + ": " + quiz[i].toString());
quiz[i].inputAnswer(in);
}
}
public static void main(String[] args) {
new QuizTester();
}
}
I thank you so much in advance
So I have a test which is to test the addNewCustomer method which does so by reading in from a text file
#Test
public void testAddNewCustomer() {
System.out.println("addNewCustomer");
try {
File nFile = new File("ProductData.txt");
File file = new File("CustomerData.txt");
Scanner scan = new Scanner(file);
ElectronicsEquipmentSupplier ees = new ElectronicsEquipmentSupplier(1, 1, InputFileData.readProductDataFile(nFile));
ees.addNewCustomer(InputFileData.readCustomerData(scan));
CustomerDetailsList expResult = ees.getDetails();
CustomerDetailsList result = ees.getDetails();
assertEquals(expResult, result);
} catch (IllegalCustomerIDException | IOException | IllegalProductCodeException e) {
fail(e.getMessage());
}
}
The problem that I'm having is to what to have as the expected result? I tried putting a string with the values that I thought would be entered but it then said I can't compare type string with type CustomerDetailsList. Any ideas?
public class CustomerDetailsList {
private final ArrayList<CustomerDetails> customerCollection;
public CustomerDetailsList() {
customerCollection = new ArrayList<>();
}
public void addCustomer(CustomerDetails newCustomer) {
customerCollection.add(newCustomer);
}
public int numberOfCustomers() {
return customerCollection.size();
}
public void clearArray() {
this.customerCollection.clear();
}
/**
*
* #param givenID the ID of a customer
* #return the customer’s details if found, exception thrown otherwise.
* #throws supplierproject.CustomerNotFoundException
*/
public CustomerDetails findCustomer(String givenID) throws CustomerNotFoundException {
CustomerNotFoundException notFoundMessage
= new CustomerNotFoundException("Customer was not found");
int size = customerCollection.size();
int i = 0;
boolean customerFound = false;
while (!customerFound && i < size) {
customerFound = customerCollection.get(i).getCustomerID().equals(givenID);
i++;
}
if (customerFound) {
return customerCollection.get(i - 1);
} else {
throw notFoundMessage;
}
}
#Override
public String toString() {
StringBuilder customerDets = new StringBuilder();
for (int i = 0; i < numberOfCustomers(); i++) {
customerDets.append(customerCollection.get(i).toString()).append("\n");
}
return customerDets.toString();
}
}
The list itself
Generally, you should test if the new customer is in the list. However, the expResult and result from your test are just the same, because at that point the ees already contains the new customer. Therefore the assertion does not make sense.
However, you can test if the Customer List contains the customer with given email (or some unique property of that customer).
I am trying to remove errors in the following code.
package in.citydoor.imports.catalog.tools;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Main {
/**
* #param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String file_name="C:/aman/textfile.txt";
try {
CatFeedWriterToMemory obj = new CatFeedWriterToMemory(file_name);
String[] arryLines = obj.ReadFile();
/*
* int i;
* for(i=0;i<arryLines.length;i++){
* System.out.println(arryLines[i]);
* }
*/
} catch(IOException e) {
System.out.println(e.getMessage());
System.out.println("Keep file on specified path");
}
}
}
package in.citydoor.imports.catalog.tools;
import java.util.ArrayList;
public class CatFeedBean {
ArrayList<ProductVo> parsedList = new ArrayList<ProductVo>();
ArrayList<PriceVo> priceList = new ArrayList<PriceVo>();
ArrayList<SkuVo> SkuList = new ArrayList<SkuVo>();
**String[] columns = arryLines.split("/");**
String productid = columns[0];
String productname = columns[1];
String skuid = columns[2];
String price = columns[3];
ProductVo productObj = new ProductVo(productid,productname);
**parsedList.add(productObj);**
SkuVo skuObj = new SkuVo(skuid);
// SkuList.add(skuObj);
PriceVo priceObj = new PriceVo(price);
// priceList.add(priceObj);
}
package in.citydoor.imports.catalog.tools;
public class ProductVo {
private String product_id;
private String product_name;
public ProductVo(String i, String n) {
product_id = i;
product_name = n;
}
public String getProductId() {
return this.product_id;
}
public void setProductId(String product_id) {
this.product_id = product_id;
}
public String getProductName() {
return this.product_name;
}
public void setProductname(String product_name){
this.product_name = product_name;
}
}
1) For the line String[] columns = arryLines.split("/"); in main class,
I am getting error "arryLines cannot be resolved".
2). For line parsedList.add(productObj);" in CatFeedBean class,
I am getting error "Syntax error on token"productObj",VariableDeclaratorId expected after this token".
Yes,
The arryLines is a local variable in your Main class.
The code is not in a method, it is in the declaration part of the
class.
You should call the add-method inside of a method or constructor.
like:
public CatFeedBean() {
parsedList.add(productObj);
}
see stackoverflow.com/questions/17499455
I tried to save data to json string in a txt file using Gson and then restore it using Gson either. Things go well if I do it in eclipse. But when packaged to jar, Gson throws Exceptions.
Here is the code for saving the file.
String gsonStr = gson.toJson(masterShips); // masterShips is ArrayList<Ship>
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("D:\\master_ship.txt"));
writer.write(gsonStr);
} catch (IOException e) {
System.err.println(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
Then I read the file in eclipse using this code (and it works):
Scanner in = new Scanner(new FileReader("D:\\master_ship.txt"));
String str = in.nextLine();
Log.toDebug(str);
in.close();
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(str);
JsonArray ja = je.getAsJsonArray();
for (int i=0; i<ja.size(); ++i) {
...
}
But after packaged into jar and run in cmd, Exception occurs:
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.
stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malform
ed JSON at line 1 column 4
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at kan.util.Master.loadMasterShip(Master.java:44)
at kan.util.Master.load(Master.java:27)
at kan.Main.main(Main.java:22)
Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLeni
ent(true) to accept malformed JSON at line 1 column 4
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1386)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:531)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:414)
at com.google.gson.JsonParser.parse(JsonParser.java:60)
... 4 more
According to the hint of the Exception, I changed my code and it still works in eclipse:
Scanner in = new Scanner(new FileReader("D:\\master_ship.txt"));
String str = in.nextLine();
in.close();
Reader reader = new StringReader(str);
JsonReader jr = new JsonReader(reader);
jr.setLenient(true);
JsonParser parser = new JsonParser();
JsonElement je = parser.parse(jr);
JsonArray ja = je.getAsJsonArray();
for (int i=0; i<ja.size(); ++i) {
...
}
But jar failed and throws
Exception in thread "main" java.lang.IllegalStateException: This is not a JSON A
rray.
at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
at kan.util.Master.loadMasterShip(Master.java:58)
at kan.util.Master.load(Master.java:30)
at kan.Main.main(Main.java:22)
As suggested by Sotirios I cut the length of the arraylist down, and when I increase the number of ships to 4, things go wrong. Here is the json:
[{"id":1,"name":"睦月","type":2,"rank":2,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":254,"range":1,"powerups":[1,1,0,0]},{"id":2,"name":"如月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":255,"range":1,"powerups":[0,1,0,0]},{"id":6,"name":"長月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":258,"range":1,"powerups":[0,1,0,0]},{"id":7,"name":"三日月","type":2,"rank":1,"fuelMax":15,"bulletMax":15,"slotNum":2,"speed":10,"afterLv":20,"afterId":260,"range":1,"powerups":[0,1,0,0]}]
↑ colunm 473
Exception in thread "main" com.google.gson.JsonSyntaxException: com.google.gson.
stream.MalformedJsonException: Unterminated object at line 1 column 473
at com.google.gson.internal.Streams.parse(Streams.java:56)
at com.google.gson.JsonParser.parse(JsonParser.java:84)
at kan.util.Master.loadMasterShip(Master.java:55)
at kan.util.Master.load(Master.java:30)
at kan.Main.main(Main.java:22)
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated object at
line 1 column 473
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1505)
at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:480)
at com.google.gson.stream.JsonReader.hasNext(JsonReader.java:403)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
666)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
659)
at com.google.gson.internal.bind.TypeAdapters$25.read(TypeAdapters.java:
642)
at com.google.gson.internal.Streams.parse(Streams.java:44)
... 4 more
Can anyone help me with this, you will be reaally preciated!
Use this class
import java.util.List;
public class GsonResponse
{
public int id;
public String name;
public int type;
public int rank;
public int fuelMax;
public int bulletMax;
public int slotNum;
public int speed;
public int afterLv;
public int afterId;
public int range;
public List<Integer> powerups;
/**
* #return the id
*/
public int getId() {
return id;
}
/**
* #param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the type
*/
public int getType() {
return type;
}
/**
* #param type the type to set
*/
public void setType(int type) {
this.type = type;
}
/**
* #return the rank
*/
public int getRank() {
return rank;
}
/**
* #param rank the rank to set
*/
public void setRank(int rank) {
this.rank = rank;
}
/**
* #return the fuelMax
*/
public int getFuelMax() {
return fuelMax;
}
/**
* #param fuelMax the fuelMax to set
*/
public void setFuelMax(int fuelMax) {
this.fuelMax = fuelMax;
}
/**
* #return the bulletMax
*/
public int getBulletMax() {
return bulletMax;
}
/**
* #param bulletMax the bulletMax to set
*/
public void setBulletMax(int bulletMax) {
this.bulletMax = bulletMax;
}
/**
* #return the slotNum
*/
public int getSlotNum() {
return slotNum;
}
/**
* #param slotNum the slotNum to set
*/
public void setSlotNum(int slotNum) {
this.slotNum = slotNum;
}
/**
* #return the speed
*/
public int getSpeed() {
return speed;
}
/**
* #param speed the speed to set
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* #return the afterLv
*/
public int getAfterLv() {
return afterLv;
}
/**
* #param afterLv the afterLv to set
*/
public void setAfterLv(int afterLv) {
this.afterLv = afterLv;
}
/**
* #return the afterId
*/
public int getAfterId() {
return afterId;
}
/**
* #param afterId the afterId to set
*/
public void setAfterId(int afterId) {
this.afterId = afterId;
}
/**
* #return the range
*/
public int getRange() {
return range;
}
/**
* #param range the range to set
*/
public void setRange(int range) {
this.range = range;
}
/**
* #return the powerups
*/
public List<Integer> getPowerups() {
return powerups;
}
/**
* #param powerups the powerups to set
*/
public void setPowerups(List<Integer> powerups) {
this.powerups = powerups;
}
}
just add below code where u parse
String strJson = "[{\"id\":1,\"name\":\"睦月\",\"type\":2,\"rank\":2,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":254,\"range\":1,\"powerups\":[1,1,0,0]},{\"id\":2,\"name\":\"如月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":255,\"range\":1,\"powerups\":[0,1,0,0]},{\"id\":6,\"name\":\"長月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":258,\"range\":1,\"powerups\":[0,1,0,0]},{\"id\":7,\"name\":\"三日月\",\"type\":2,\"rank\":1,\"fuelMax\":15,\"bulletMax\":15,\"slotNum\":2,\"speed\":10,\"afterLv\":20,\"afterId\":260,\"range\":1,\"powerups\":[0,1,0,0]}]";
GsonResponse gsonResponse = null;
try {
gsonResponse = new Gson().fromJson(strJson,
GsonResponse.class);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
I have a program that connects client threads to a cinema through a socket.
the client transfers his details (client no., required tickets) the cinema processes the request in another thread that makes sure there is enough seats available.
as you will see in the code, i have a slight problem with figuring out how to send a feedback to the client from the cinema(server), the code presented is not completed, i just wanted to give you an idea of how it looks.
my idea is to transfer the dataOutputStream to the cinemaThread and when its done, the cinema thread will return the feedback through the stream to the client is it possible?
Client side
import java.util.Random;
/**
* this class in designed to define client properties
* #author David and Adam
*
*/
public class Client extends Thread{
private int serialNumber;
private int NumberOfTickets;
private String creditNumber;
private String serverPort;
private int rowNumber;
/**
* full constructor
* #param serialNumber
* #param NumberOfTickets
* #param creditNumber
*/
public Client(int serialNumber){
this.serialNumber = serialNumber;
this.NumberOfTickets = generateTicketNumber();
this.creditNumber = generateCreditNumber();
this.rowNumber = -1;
}
/**
* returns a value in the required number of ticket range.
* #return
*/
private int generateTicketNumber(){
return (new Random()).nextInt(Constants.MaxNumberOfTickets-1)+Constants.MinNumberOfTickets;
}
/**
* returns a random credit number constructed of 16 digits.
* #return
*/
private String generateCreditNumber(){
String s = String.valueOf((new Random()).nextInt(9)+1);
for(int i=0 ; i<16 ; i++){
s = (new Random()).nextInt()+s;
}
return s;
}
Server side
import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* an implementation of server side defined as cinema hall
* #author David
*
*/
public class Cinema {
private int[][] cinemaHall;
private int ticketPrice;
private int securedPort;
private int numberOfRequests;
/**
* full constructor
*
*/
public Cinema(){
this.numberOfRequests = 0;
initializeCinemaHall();
setTicketPrice();
setSecuredPort();
}
/**
* initializes cinema hall to 0's
*/
private void initializeCinemaHall(){
this.cinemaHall = new int[10][10];
for(int i=0 ; i<cinemaHall.length ; i++)
for(int j=0 ; j<cinemaHall.length ; j++)
this.cinemaHall[i][j] = 0;
}
/**
* generates the ticket price on sale
*/
private void setTicketPrice(){
this.ticketPrice = Constants.TicketSalePrice;
}
/**
* sets the secured port
*
*/
private void setSecuredPort(){
this.securedPort = Constants.SecuredPort;
}
public int[][] getCinemaHall() {
return cinemaHall;
}
public void setCinemaHall(int[][] cinemaHall) {
this.cinemaHall = cinemaHall;
}
public int getTicketPrice() {
return ticketPrice;
}
public void setTicketPrice(int ticketPrice) {
this.ticketPrice = ticketPrice;
}
public int getNumberOfRequests() {
return numberOfRequests;
}
public void setNumberOfRequests(int numberOfRequests) {
this.numberOfRequests = numberOfRequests;
}
/**
* main function for establishing communication between cinema and customers
* #param args
* #throws IOException
*/
public static void main(String[] args) throws IOException{
Cinema cinema = new Cinema();
ServerSocket server = new ServerSocket(Constants.ServerPort);
System.out.println("**********Yes Planet server in listening**********");
// create a connection to clients
while(cinema.numberOfRequests<Constants.MaxClientNumber){
try {
//wait for client
Socket s = server.accept();
System.out.println("Client connected to socket");
// get client info
DataInputStream dis = new DataInputStream(s.getInputStream());
String clientInfo = dis.readLine();
String[] details = clientInfo.split("/");
try {
// parse data to correct type and send data to cinema thread
Thread cinemaThread = new CinemaThread(cinema, Integer.parseInt(details[0])
, Integer.parseInt(details[1]), details[2]);
cinemaThread.start();
}
catch (Exception e){
System.out.println("An error has occured, client request have been canceled");
}
} catch (IOException e) {
System.out.println("Connection error ");
e.printStackTrace();
}
}
}
}
Cinema Thread
public class CinemaThread extends Thread{
private int clientNumber;
private Cinema cinema;
private int requiredTickets;
private String clientCreditNumber;
private boolean occupied;
private int lineNumber;
private boolean alive;
/**
* full constructor
* #param clientNumber
* #param cinema
* #param requiredTickets
* #param clientCreditNumber
*/
public CinemaThread(Cinema cinema, int clientNumber, int requiredTickets, String clientCreditNumber){
this.clientNumber = clientNumber;
this.cinema = cinema;
this.requiredTickets = requiredTickets;
this.clientCreditNumber = clientCreditNumber;
this.occupied = false;
this.lineNumber = -1;
this.alive = true;
}
/**
* the method checks for available seats to each individual required client.
* in case an available sequence is found, cinema hall is updated with client details and a connection
* with the credit company is established forwarding the data needed to proceed.
*/
public void run(){
int ticketCount=0;
int startSeat = -1;
int endSeat = -1;
for(int i=0 ; i<cinema.getCinemaHall().length && !occupied; i++){
for(int j=0 ; j<cinema.getCinemaHall().length && !occupied; j++){
if(cinema.getCinemaHall()[i][j]>0)
ticketCount++;
else
ticketCount=0;
if(ticketCount == requiredTickets){
lineNumber = i;
startSeat = j-requiredTickets+1;
endSeat = j;
occupied=true;
}
}
for(int k=startSeat ;k<=endSeat ; k++)
cinema.getCinemaHall()[lineNumber][k] = clientNumber;
}
if(occupied){
// connection with credit company
}
this.alive = false;
}
public boolean status(){
return this.alive;
}
}
You don't need to transfer a DataOutputStream. Use Socket.getOutputStream().
server side
public static void main(String[] args) throws IOException{
// ... ...
// Socket s = server.accept();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// ... ...
Thread cinemaThread = new CinemaThread( /* ... */, dos);
// ... ...
}
public CinemaThread(/* ... */, DataOutputStream dos){
// ... ...
this.dos = dos;
}
public void run(){
// ... ...
if(occupied)
dos.writeBoolean(true);
else
dos.writeBoolean(false);
// ... ...
}
Streams are typically one way 'out' or 'in', so by my reading of the question, no it is not possible to use one stream for both.