Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lds;
- import java.io.*;
- import java.util.*;
- /**
- * This program reads an input file with some integers,
- * stores them in a queue, and splits them in another
- * 2 queues, one for positive numbers and the other one
- * for negative ones. Finally the values in these 2 queues.
- * are printed in 2 different columns.
- */
- public class OrderQueue {
- public static void main(String[] args) throws Exception
- {
- int position = -1;
- //This loops looks for the position of the argument "-input".
- for (int i=0; i < args.length; i++) {
- if (args[i].equals("-input")) {
- position = i;
- break;
- }
- }
- //If there's no "-input" argument we will throw an exception.
- if (position == -1) {
- throw new Exception("There's no -input argument");
- }
- String arg = null;
- try {
- arg = args[position+1];
- } catch (ArrayIndexOutOfBoundsException e) {
- throw new Exception("You must enter a filename to process");
- }
- File file = new File(arg);
- Scanner input = new Scanner(file);
- QueueIntLinked first = readAndWrite(input);
- QueueIntLinked positive = new QueueIntLinked();
- QueueIntLinked negative = new QueueIntLinked();
- separate(first, positive, negative);
- print(positive, negative);
- }
- /**
- * This method prints positive and negative numbers extracted from 2 different stacks in 2 separated columns.
- * @param positive This first parameter is the queue which stores the positive values, which will be printed at the right.
- * @param negative This second parameter is the queue which stores the negative values, which will be printed at the left.
- */
- private static void print(QueueIntLinked positive, QueueIntLinked negative) throws Exception
- {
- for(int i=0; i<positive.size() || i<negative.size(); i++)
- {
- if(i>positive.size())
- {
- System.out.println( negative.remove() + "\t" + " ");
- } else if(i>negative.size())
- {
- System.out.println( " " + "\t" + positive.remove());
- } else
- System.out.println(negative.remove() + "\t" +positive.remove());
- }
- }
- /**
- * This method will split the values from the queue named "first" and will store them, if they
- * are negative on the queue named "negative" and in the opposite case on the queue named "positive",
- * both provided as parameters.
- * @param first Queue which contains the numbers to split.
- * @param positive Queue which will store the positive numbers.
- * @param negative Queue which will store the negative numbers.
- */
- private static void separate(QueueIntLinked first, QueueIntLinked positive, QueueIntLinked negative) throws Exception
- {
- while(!first.isEmpty())
- {
- int number = (int) first.remove();
- if(number >= 0)
- {
- positive.add(number);
- } else
- negative.add(number);
- }
- }
- /**
- * This method is used to store the numbers of the input
- * in a queue which will be returned.
- * @param s This parameter of type scanner will be used to pass the values from the input to the method.
- * @return QueueIntLinked The method returns a queue which stores the values read from the input.
- */
- public static QueueIntLinked readAndWrite(Scanner s) throws Exception {
- QueueIntLinked numbers = new QueueIntLinked();
- while(s.hasNextInt()) {
- numbers.add(s.nextInt());
- }
- return numbers;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement