![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
My problem is that I want to be able to create an array, but instead of entering the array by hand, I want for it to be read in from a text file, a string token at a time. I then want to sort it, I have looked at using a selction sort etc, and read up on the java.util.Arrays.sort method, and just cant make it work.
My code is as follows: import java.util.*;
import java.io.*;
public class Example
{
public static void main() throws IOException
{
//set up input stream
BufferedReader filein = new BufferedReader(new FileReader("C:\\Program Files\\filein.txt"));
//set up output stream
PrintWriter fileout = new PrintWriter(new FileWriter("C:\\Program Files\\fileout.txt"));
//Declare variables
String nextLine; //a line read from file
StringTokenizer t; //the words within the line
ArrayList a = new ArrayList(); //store all words here
String s; //next word
boolean isAWord; //true if string represents a Word
char[] nVal = { '.',',','!',':',';','?' }; // not valid characters
int countGood=0, countBad=0;
System.out.println("Starting to read");
nextLine = filein.readLine(); //read from input file
//start
while (nextLine != null)
{
t = new StringTokenizer(nextLine); //identify words
nextLine = nextLine.toLowerCase( );//Converts to Lowercase()
while (t.hasMoreTokens())
{
a.add(t.nextToken()); //add next word to list
}
java.util.Arrays.sort(a);
// This line wont work and brings up the error that it cannot resolve symbol
} //finished reading file
fileout.println(a);
System.out.println("Finished");
fileout.close();
}
}I have a problem getting anything to be written to the fileout textfile, and my sort method again doesnt seem to work. Could anybody help me out please? Thanks Andy |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I made a few small changes to your program. I changed it first to a do-while loop, because it has to go through at least once. I also put your readline inside the loop. Then, your sort method was giving me some errors, so I used a sort method inside the collectinos class. Just changed three lines, basically. Here's your edited code.
import java.util.*;
import java.io.*;
public class Example2
{
public static void main() throws IOException
{
//set up input stream
BufferedReader filein = new BufferedReader(new FileReader("filein.txt"));
//set up output stream
PrintWriter fileout = new PrintWriter(new FileWriter("fileout.txt"));
//Declare variables
String nextLine; //a line read from file
StringTokenizer t; //the words within the line
ArrayList a = new ArrayList(); //store all words here
String s; //next word
boolean isAWord; //true if string represents a Word
char[] nVal = { '.',',','!',':',';','?' }; // not valid characters
int countGood=0, countBad=0;
System.out.println("Starting to read");
//start
do
{
nextLine = filein.readLine(); //read from input file
t = new StringTokenizer(nextLine); //identify words
nextLine = nextLine.toLowerCase( );//Converts to Lowercase()
while (t.hasMoreTokens())
{
a.add(t.nextToken()); //add next word to list
}
Collections.sort(a);
// This line wont work and brings up the error that it cannot resolve symbol
}while (nextLine != null); //finished reading file
fileout.println(a);
System.out.println("Finished");
fileout.close();
}
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2004
Posts: 3
Rep Power: 0
![]() |
Hi, me and a couple of my freinds got together and have managed to come up with some different code. My question is whether there is any possible way at all to make this code more efficient, even if it is just making a snipet of the code shorter, or getting rid of a line here and there. You see we get marked on efficiency for this you see, and I also think my structure is not the best, if you have any suggestions how to make it better?
Thyanks
import java.util.*;
import java.io.*;
public class Example2
{
public static void main(String [] args) throws IOException
{
//set up input stream
File finput = new File("C:\\fi.txt");
FileReader fread = new FileReader(finput);
BufferedReader fin = new BufferedReader(fread);
String nextLine; //a line read from file
StringTokenizer t; //the words within the line
ArrayList words = new ArrayList(); // store all words here
String tmpString;
int identification;
System.out.println("\nGo through text\n");
while (true) //potentially infinite loop
{
nextLine = fin.readLine(); //read from input file
if (nextLine == null) break; //if no more lines, get out
t = new StringTokenizer(nextLine); //identify words
while (t.hasMoreTokens())
{
tmpString=t.nextToken();
//invoke a method for identify if it is a word
identification=isWord(tmpString);
switch(identification)
{
case -3: // it is a word
{
tmpString=tmpString.toLowerCase(); //convert to lowercases
words.add(tmpString); //add into arraylist
break;
}
case -1:
break; //it is not a word, so do nothig;
case -2: // it is a word with end of punctuation;
{
//eliminate punctuations;
tmpString=tmpString.substring(0,tmpString.length()-1);
words.add(tmpString.toLowerCase()); //add into arraylist;
break;
}
}//end of switch
}// end of internal while
} //end of external while
// Begin to sort...
//invoke a method to sort ArrayList words in an dictionary order
toSort(words);
//invoke a method to output the result into a file and display on screen
printResult(words);
fin.close(); //close the input dream
System.out.println("\nFinished!");
}// end of method main()
public static void toSort(ArrayList forSorting)
{
System.out.println("\nStarting to sort....");
int arraySize=forSorting.size();
String x; //temp swapping variable
int j;
//sorting the arraylist with InsertionSort algorithm
for(int i=0;i<arraySize;i++)
{
x=(String) forSorting.get(i); //element to be inserted
for(j=i;j>0 && x.compareTo(((String)forSorting.get(j-1)))<0;j--)
{
forSorting.set(j,forSorting.get(j-1));
}
forSorting.set(j,x);
}//end of for loop
}//end of method toSort()
public static int isWord(String ifWord)
{
String alphaCaps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String alphaLower = alphaCaps.toLowerCase();
String allowable = alphaCaps + alphaLower; //the string of valid letters
String allowPunc=",;:.!?"; // the string of valid punctuations
//Starting to identify…..
if (allowable.indexOf(ifWord.charAt(0))==-1) //check the first char
{
return -1; // if first char is not a letter, the token is not a word
}
else // check the rest of the token
{
for(int i=1;i<ifWord.length();i++)
{
if (allowable.indexOf(ifWord.charAt(i))==-1)
{
//identify if the last second letter is a punctuation
if (i<ifWord.length( )-1)
return -1; // it is not a word.
//identify if the last letter is a punctuation
//if yes, identify whether it is a valid punctuation.
if (i==ifWord.length( )-1 && allowPunc.indexOf(ifWord.charAt(i))!=-1)
return -2; // it is a word with end of punctuation
else
return -1; // String with unallowable punctuation
}
} // end of for loop
}// end of external if statement
return -3; // it is a word.
} //end of method isWord()
public static void printResult(ArrayList result) throws IOException
{
//set up output stream
File foutput = new File("C://fo.txt");
FileWriter fwrite = new FileWriter(foutput);
PrintWriter fout = new PrintWriter(fwrite);
System.out.println("\nStarting to output the result:"+"\n");
int counter=1; //initialize the appear times
int j;
for(int i=0;i<result.size();i++)
{
j=i+1;
if( j<=result.size()-1 && ((String) result.get(i)).compareTo((String) result.get(j))==0)
{
counter++;
}
else
{
//Starting to output
if (((String) result.get(i)).length()>=8)
{
System.out.println(result.get(i)+"\t\t"+counter);
fout.println(result.get(i)+"\t\t"+counter);
}
else
{
System.out.println(result.get(i)+"\t\t\t"+counter );
fout.println(result.get(i)+"\t\t\t"+counter);
}
counter=1; //back the counter to 1 for the next word
}
}//end of for loop
fout.close(); //close the output stream
}//end of method method printResult()
}//end of the classThanks mate Sco |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Nov 2004
Posts: 7
Rep Power: 0
![]() |
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|