|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
hi
i want to get input from a user (user name and a filename) through the ms-dos window(see the code below), and then i want add these strings to an queue and then print out the queue. I am having problems remembering how to do this if any of you could help me i'd be greatful thanx Heres the code i have so far: import java.io.*; public class queuedriver1 { public static void main(String[]args)throws IOException { BufferedReader keyboard = new BufferedReader( new InputStreamReader( System.in ) ); String inString; inString = keyboard.readLine(); } } Last edited by suzanne15 : March 14th, 2003 at 10:21 AM. |
|
#2
|
|||
|
|||
|
I am not sure what your queston is. Are you saying that you want to keep reading line after line of user input until the user says they are done all the while storing the strings they have been inputting, then echo them to the screen?
|
|
#3
|
|||
|
|||
|
Somthing like this may be what you want?
Code:
import java.io.*;
import java.util.ArrayList;
public class queuedriver1 {
public static void main(String[] args) throws IOException {
BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
ArrayList list = new ArrayList();
String inString = "";
while(!inString.equals("done")) {
inString = keyboard.readLine();
list.add(inString);
}
for (int i = 0; i < list.size(); i++) {
System.out.println((String)list.get(i));
}
}
}
|
|
#4
|
|||
|
|||
|
yeh thats a good start thanx very much for your help but would it work the same adding to a queue instead and would it be just as easy to alter the code
suzanne ![]() |
|
#5
|
|||
|
|||
|
I am not sure what you mean by "queue". The only queue class I know of in Java is javax.jms.Queue. Is this to what you are referring?
Or are you just talking about a collection that is First In First Out (FIFO)? Last edited by Nemi : March 14th, 2003 at 02:17 PM. |
|
#6
|
|||
|
|||
|
i dont really know what u mean but its an abstract data type i need to insert the words i enter so that they get added to a queue im sorry if u dont understand im really grateful for your help
![]() |
|
#7
|
|||
|
|||
|
Sorry i am being a bit vague but im not 100% sure what i am doing myself. I have been given the following question to do, and yes the queue is in terms of a FIFO
thanx for your help suzanne Question 4 • Minimal solution • add items to queue – Request information from user (name, file) • print [and remove] item from queue – Simulate printing first item on queue • list contents of queue – Alphabetically sorted by user • remove a specified item from the queue – Only owner of job can remove it • Must provide a list of queue items, alphabetically ordered by user (and within that by sequence of addition to the queue). – Must use a bst for this |
|
#8
|
|||
|
|||
|
You can use one of several java collections to implement a queue. Here is one that someone did using a linked list
http://javaalmanac.com/egs/java.util/coll_Queue.html You could feasibly do the same using an ArrayList or Vector or something Code:
ArrayList queue = new ArrayList();
// Add to end of queue
queue.add(object);
// Get head of queue
Object o = queue.remove(0);
It sounds like they are expecting a lot out of you without you knowing a lot about Java. Hope what I have given helps. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > help with adding to arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|