Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. package lds;
  2. import java.io.*;
  3. import java.util.*;
  4. /**
  5.  * This program reads an input file with some integers,
  6.  * stores them in a queue, and splits them in another
  7.  * 2 queues, one for positive numbers and the other one
  8.  * for negative ones. Finally the values in these 2 queues.
  9.  * are printed in 2 different columns.
  10.  */
  11. public class OrderQueue {
  12.    
  13.     public static void main(String[] args) throws Exception
  14.     {
  15.         int position = -1;
  16.         //This loops looks for the position of the argument "-input".
  17.         for (int i=0; i < args.length; i++) {
  18.             if (args[i].equals("-input")) {    
  19.                 position = i;
  20.                 break;
  21.             }
  22.         }
  23.         //If there's no "-input" argument we will throw an exception.
  24.         if (position == -1) {
  25.             throw new Exception("There's no -input argument");
  26.         }
  27.        
  28.         String arg = null;
  29.        
  30.         try {
  31.             arg = args[position+1];
  32.         } catch (ArrayIndexOutOfBoundsException e) {
  33.             throw new Exception("You must enter a filename to process");
  34.         }
  35.                
  36.         File file = new File(arg);
  37.         Scanner input = new Scanner(file);
  38.         QueueIntLinked first = readAndWrite(input);
  39.         QueueIntLinked positive = new QueueIntLinked();
  40.         QueueIntLinked negative = new QueueIntLinked();
  41.  
  42.         separate(first, positive, negative);
  43.         print(positive, negative);
  44.     }
  45.     /**
  46.      * This method prints positive and negative numbers extracted from 2 different stacks in 2 separated columns.
  47.      * @param positive This first parameter is the queue which stores the positive values, which will be printed at the right.
  48.      * @param negative This second parameter is the queue which stores the negative values, which will be printed at the left.
  49.      */
  50.     private static void print(QueueIntLinked positive, QueueIntLinked negative) throws Exception
  51.     {
  52.         for(int i=0; i<positive.size() || i<negative.size(); i++)
  53.         {
  54.             if(i>positive.size())
  55.             {
  56.                 System.out.println( negative.remove() + "\t" + " ");
  57.             } else if(i>negative.size())
  58.             {
  59.                 System.out.println( " " + "\t" + positive.remove());
  60.             } else
  61.                 System.out.println(negative.remove() + "\t" +positive.remove());
  62.         }
  63.        
  64.     }
  65.  /**
  66.   * This method will split the values from the queue named "first" and will store them, if they
  67.   * are negative on the queue named "negative" and in the opposite case on the queue named "positive",
  68.   * both provided as parameters.
  69.   * @param first Queue which contains the numbers to split.
  70.   * @param positive Queue which will store the positive numbers.
  71.   * @param negative Queue which will store the negative numbers.
  72.   */
  73.     private static void separate(QueueIntLinked first, QueueIntLinked positive, QueueIntLinked negative) throws Exception
  74.     {
  75.         while(!first.isEmpty())
  76.         {
  77.             int number = (int) first.remove();
  78.             if(number >= 0)
  79.             {
  80.                 positive.add(number);
  81.             } else
  82.                 negative.add(number);
  83.         }
  84.        
  85.     }
  86.  
  87.     /**
  88.      * This method is used to store the numbers of the input
  89.      * in a queue which will be returned.
  90.      * @param s This parameter of type scanner will be used to pass the values from the input to the method.
  91.      * @return QueueIntLinked The method returns a queue which stores the values read from the input.
  92.      */
  93.     public static QueueIntLinked readAndWrite(Scanner s) throws Exception {
  94.         QueueIntLinked numbers = new QueueIntLinked();
  95.         while(s.hasNextInt()) {
  96.             numbers.add(s.nextInt());
  97.         }
  98.         return numbers;
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement