Results 1 to 2 of 2
  1. #1
    carl is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default I want to be able to do this with stacks and queues as well as with vectors

    Hi, Here's my question. I have a sample file listed bellow:

    Java Code:
    10 11 12 13 14 14 13 12 11 10
    10 11 12 13 12 11 10
    10 11 12 10 11 12
    Note that these data are in a file. Each line in the file will contain a set of integers separated by spaces. how can I write a program that reads the name of a file from the command line, determine whether the first half of the numbers are sorted in ascending order and the second half of the numbers in descending order?? The first half and second half must contain the same integers.
    I want to be able to do this with stacks and queues as well as with vectors.

    Thanks.

  2. #2
    lenny is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Please Check this:

    Java Code:
    import java.util.Vector;
    import java.util.Stack;
    import java.util.Collections;
    import java.io.*;
    import java.util.StringTokenizer;
    
    public class Study {
    private static File F=null;
    private static String fileName="";
    private BufferedReader br=null;
    private FileInputStream fin=null;
    private String currentLine="";
    private boolean ascendingDescendingNumbersAreEquals=false;
    public Study() {
    }
    /*Setting File Name*/
    public static void setFileName(String fName) {
    fileName=fName;
    F = new File(fName);
    }
    
    public static void main(String[] args) {
    Study study = new Study();
    study.setFileName(args[0]);
    study.completeTask();
    }
    
    public void completeTask() {
    int lineNo=1;
    try {
    fin = new FileInputStream(F);
    br = new BufferedReader(new InputStreamReader(fin));
    //Read Line by Line
    while((currentLine=br.readLine())!=null) {
    
    if(checkSorting(currentLine)) {
    System.out.println("Line "+lineNo+", contains numbers in sorted in order");
    if(areAscendingDescendingNumbersAreEquals()) {
    System.out.println("For Line "+lineNo+", numbers are equal in ascending and descending way");
    }
    } else {
    System.out.println("Line "+lineNo+", does NOT contain numbers in sorted order");
    }
    
    lineNo++;
    
    }
    fin.close();
    br.close();
    
    } catch (Exception e) {
    e.printStackTrace();
    }
    
    }
    /*First Condition Check Sorting */
    private boolean checkSorting(String line) {
    boolean sorted=true;
    StringTokenizer tokenizer=null;
    int totalNumbersFound=0;
    Vector ascendingNumbers;//Vector for ascending Numbers
    Stack descendingNumbers;//Stack for descending Numbers
    //For Vector list is stored in list (used for ascending)
    //For stack it is last in first out (used for descending)
    int counter=0;
    try {
    tokenizer = new StringTokenizer(line);//Space is default token
    totalNumbersFound=tokenizer.countTokens();
    // Half are considered ascending and half descending
    if(totalNumbersFound%2==0) {
    ascendingNumbers = new Vector();//Vector for ascending Numbers
    descendingNumbers = new Stack();//Stack for descending Numbers
    
    while(tokenizer.hasMoreElements()) {
    counter++;
    //Checking till half numbers
    if(counter<=(totalNumbersFound/2)) {
    ascendingNumbers.add(tokenizer.nextElement().toString());//add Element
    } else {
    descendingNumbers.push(tokenizer.nextElement().toString());//push
    }
    
    }//while
    /*Checking for ascending numbers*/
    for(int i=0;i<ascendingNumbers.size()-1;i++) {
    int num1=-1,num2=0;
    num1=Integer.parseInt((String)ascendingNumbers.get(i));
    num2=Integer.parseInt((String)ascendingNumbers.get(i+1));
    //Num2 should be greater here than num1
    if(num2!=(num1+1)) {
    sorted=false;
    break;
    }
    }
    
    /*Checking for descending numbers*/
    for(int i=0;i<descendingNumbers.size()-1;i++) {
    int num1=-1,num2=0;
    num1=Integer.parseInt((String)descendingNumbers.get(i));
    num2=Integer.parseInt((String)descendingNumbers.get(i+1));
    //Num1 should be greater here than num2
    if(num1!=(num2+1)) {
    sorted=false;
    break;
    }
    }
    //Check for equality
    ascendingDescendingNumbersAreEquals=checkNumbersForEquality(ascendingNumbers,descendingNumbers);
    
    } else {
    sorted=false;
    }
    
    } catch (Exception e) {
    e.printStackTrace();
    }
    return sorted;
    }
    private boolean checkNumbersForEquality(Vector ascendingNumbers,Stack descendingNumbers) {
    boolean equal=true;
    int num1=0,num2=0;
    for(int i=0;i<ascendingNumbers.size();i++) {
    num1=Integer.parseInt((String)ascendingNumbers.get(i));
    num2=Integer.parseInt((String)descendingNumbers.pop());
    if(num1!=num2) {
    equal =false;
    break;
    }
    }
    ascendingDescendingNumbersAreEquals = equal;
    return equal;
    }
    private boolean areAscendingDescendingNumbersAreEquals() {
    return ascendingDescendingNumbersAreEquals;
    }
    
    }
    Java Code:
    Output:
    Line 1, contains numbers in sorted in order
    For Line 1, numbers are equal in ascending and descending way
    Line 2, does NOT contain numbers in sorted order
    Line 3, does NOT contain numbers in sorted order
    Line 4, contains numbers in sorted in order
    For Line 4, numbers are equal in ascending and descending way
    
    File data.txt:
    10 11 12 13 14 14 13 12 11 10
    10 11 12 13 12 11 10
    10 11 12 10 11 12
    1 2 3 4 4 3 2 1

Similar Threads

  1. Implementing a Stack Using two Queues
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 11:28 AM
  2. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  3. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 10:04 AM
  4. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •