I am working on a program that reads in a text file as a command line argument, then goes through the file, breaking the file down into strings, special symbols (such as .:!;?) and integers. The program stores each type into its own collection. I have all the collection methods and everything written, but am now writing the client to go through the file and break it apart into these three different types. I am not sure how to go about doing this. Should I be using fileName.next() to go through and compare the token to the various types?

I am not looking for the code, but for ideas of what I should be researching to figure this problem out. Thanks for any help in advance.

Recommended Answers

All 7 Replies

I haven't yet learned about the regex class. So would not even know where to begin with that. I am fairly new to java, only in my second course using it. My main issue is how to determine if the next character in the file is a string or integer or special symbol. If I can get that figured out, I can finish this program easily, and it is something I should know, but it is just evading my memory for the last week on how to do it.

My main issue is how to determine if the next character in the file is a string or integer or special symbol.

Thats exactly the reason why Ezzaral suggested using Regular Expressions.
If you would have simply googled for "Regular Expressions in Java" you would have found this excellent tutorial as your first search result (I say excellent cause even I got my Java Regex know how from there).

Sorry if I sounded like an @#$@#$ in my last post. Wasn't my intent. I did go and read the regex class info that you suggested. I just don't see how it would work with what I am trying to do. I also doubt that my professor would give us a program to write that doesn't use what we have already learned. I have done some research on ways to do this. I have been playing around with a StreamTokenizer but can't get that to work. Here is the text that I am to read in and sort out into strings, special characters, and numbers. I don't know if it will help at all to see what I have to split up, but I don't know what else to do.

What Is a Class?

    In the real world, you'll often find many individual objects all of the same kind. 
    There may be thousands of other bicycles in existence, all of the same make and model. 
    Each bicycle was built from the same set of blueprints and therefore contains the same 
    components. In object-oriented terms, we say that your bicycle is an instance of the 
    class of objects known as bicycles. A class is the blueprint from which individual objects 
    are created.

    The following Bicycle class is one possible implementation of a bicycle:

        class Bicycle {

               int cadence = 0;
               int speed = 0;
               int gear = 1;

               void changeCadence(int newValue) {
                    cadence = newValue;
               }

               void changeGear(int newValue) {
                    gear = newValue;
               }

               void speedUp(int increment) {
                    speed = speed + increment;   
               }

               void applyBrakes(int decrement) {
                    speed = speed - decrement;
               }

               void printStates() {
                    System.out.println("cadence:"+cadence+" speed:"+speed+" gear:"+gear);
               }
        }

my professor would give us a program to write that doesn't use what we have already learned.

But the real question is why do you want to limit yourself to what you have already learned and not go a step ahead of the rest.

As far applications of regular expressions to your problem domain go, they are used to recognize patterns in a string.
eg [A-Za-z] would match single occurrence of any letter in the english alphabet. similarly you can check for occurrences of digits and special characters.
OR you could forget all about them and Ezzaral's and my previous posts and use the normal but ... approach of parsing each character of the String of the text file contents using the charAt() function of the String class and perform each test for Digits/letters individually using the methods like isDigit() etc of the Character class

Regular Expressions are a great tool, but probably overkill in this case. You can iterate character by character and use methods in the Character class to see if the current character is a letter, digit, whitespace, etc.

Ok, I am going basic as basic gets doing this program. Its really irritating me and I am to the point of just getting through this one. After discussing solutions with my professor, he wants us to parse through it and basically use if statements for some unknown reason to determine what action to take based on the character. The client I am writing uses a second class (gotten from the textbook) to do add actions on the collection. So now my problem is getting the characters, if subsequent (such as a word in the file) into the collection.

I have the text file read into a string, and am using the Character.isLetter(), isDigit, isWhitespace, to determine if the character being read is a letter, number, or whitespace. Here is my code for that, and following is the code for my method to add the element to the instance of the appropriate collection. The method to add an element is an extension of a parent class so the variables are not initialized in it. I appreciate all the advice so far and have researched all recommendations you all have given me and thanks in advance for anymore help you give me. I have always had a hard time working with string types.

import java.io.*;


public class ClientForNoDupCollection<E>
{
	

	
	public static void main(String[] args) throws IOException 
	{
		
		//Collections to hold the three types of characters in the file.
		BasicCollection<String> strings = new BasicCollection<String>();
		BasicCollection<String> symbols = new BasicCollection<String>();
		BasicCollection<String> numbers = new BasicCollection<String>();
		String file;
		
		file = readFileAsString("data.txt");
		
		
		//Loop to run through the file and sort out the characters into
		//words, numbers, and special characters.
		int i = 0;
		while(i < file.length())
		{
			char c = file.charAt(i);
			//Causes to skip all whitespace characters in the file.
			if(Character.isWhitespace(c))
				i++;
			
			//Sorts words out of the file.
			else if(Character.isLetter(c))
			{
				StringBuffer word = new StringBuffer(c);
				
				while(Character.isLetter(i+1))
				{
					word.insert(i+1, file.charAt(i+1));
					i++;
				}
				strings.add(word.toString());
				i++;
			}
			//Sorts out numbers from the file.
			else if(Character.isDigit(c))
			{
				StringBuffer number = new StringBuffer(c);
				
				while(Character.isDigit(i+1))
				{
					number.insert(i+1, file.charAt(i+1));
					i++;
				}
				numbers.add(number.toString());
				i++;
			}
			//Sorts out symbols from the file.
			else
			{
				StringBuffer symbol = new StringBuffer(c);
				
				symbols.add(symbol.toString());
				i++;
			}
		}	
	}

    private static String readFileAsString(String string)
          								throws java.io.IOException
    {
              StringBuffer fileData = new StringBuffer(1000);
              BufferedReader reader = new BufferedReader(
                      new FileReader(string));
             
              char[] buf = new char[1024];
             int numRead=0;
             
             while((numRead=reader.read(buf)) != -1)
             {
                 fileData.append(buf, 0, numRead);
             }
             
             reader.close();
             return fileData.toString();
     }
}

MY ADD METHOD

public boolean add( E element )
	{
		if ( element == null )
			throw new java.lang.IllegalArgumentException();
		if ( this.size == collection.length )
			resize(this.size + this.size / 2);
		for(int i = 0; i < this.size; i++)
		{
			if(collection[i] != element)
				i++;
			if(collection[i] == element)
				return false;
		}
		collection[size] = element;
		size++;
		modCount++;
		numElements++;
		return true;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.