Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-07-2007, 02:31 AM
Member
 
Join Date: Jul 2007
Posts: 35
carl is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 08:05 AM
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
Please Check this:

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; } }
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vectors of Vectors or hash-somethings? mindwarp New To Java 3 03-10-2008 03:57 PM
Implementing a Stack Using two Queues rhm54 New To Java 2 01-29-2008 04:00 AM
Help with Vectors and Strings... kaban New To Java 2 12-09-2007 10:04 AM
Using Stacks ravian New To Java 7 11-28-2007 10:53 AM
Using Vectors in Java JavaForums Java Blogs 0 11-04-2007 08:31 PM


All times are GMT +3. The time now is 10:52 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org