codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  PLEASE PLEASE HELP  jnekoonam at 01:00 on Wednesday, July 25, 2007
 

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

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

  Re: PLEASE PLEASE HELP  kanad at 07:27 on Thursday, July 26, 2007
 

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;
}

}
------------------------
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










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  How to send multiple perameters in SOAP request.
•  Java code for Insert picture on the table in spreadsheet
•  Re: Problem with concatenation
•  how to genrates the crystal report by sending a id at runtime
•  help me
•  pls help me with this..
•  Re: Security - Code verify
•  Job @ EarlySail
•  Job @ EarlySail (perl)


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007