Hello All,

I am trying to parse a text file with the following type lines:

Mr. Jones has a "dog" and a "cat"

I need to extract the dog and cat and put them into an array of Items.

Here is the set up:

character = read.read();
char first = file.indexOf('\"');
char second = file.indexOf('\"') ;

Here is a snippet of my code:

while(character != EOF)
  {
        for(int i = 1; i < animal.length; i++)
        {

          String name1 = animal[i].substring(first, second);

                 for(int j = 1; i < animal.length; j++)
                 String name2 = animal[j].substring(first, second);

          System.out.println(name1);

          System.out.println(name2);
          }
}

I am lost on this. Any help would be greatly appreciated.

Thanks

Try the split function of the String class... Split on the " charachter...

If the text does not start with a " you will need all uneven entries of the returning array, otherwise all even entries...

good luck

You may also want to consider using the Scanner class with regular expressions.

And once again I'll request:
Please use [ code ] tags to format code in your posts so indentation is preserved.

Don't read the file character by character. Always make sure your the I/O operations performed by your program are buffered unless needed otherwise. The less the I/O calls, the better. Instead, read the entire line in a string using BufferedReader / Scanner class and perform the processing. Of the many ways in which the solution can be achieved, regular expressions and manual looping are two of them.

Using the regular expression approach, the regular expression would look something along the lines of: 'Mr. Jones "dog" and "cat" bosh'.match(/[^"]*"([^"]+)"[^"]*"([^"]+)"[^"]*/); This is Javascript, but the concept doesn't change and if you are familiar with regexes you should be able to convert it into a Java equivalent without much effort. If you are not comfortable with regexes, manual looping is the way to go.

Using manual looping, all you would have to do is look for quotes, set a flag, start collecting characters until you find a matching quote, reset the flag and continue the process. The matched data can be stored in a container like ArrayList.

I would personally go with the looping approach due to it's simplicity and extensibility. Plus if this is a homework problem, I am sure your professor would be expecting the looping approach rather than a classy regular expression solution.

That is interesting, I will give that a try. Thanks

Hello all,

I am trying to use the split function as suggested earlier. I am having a problem and keep getting a NoSuchElementElementException.

Can anyone tell me where my mistakes are?

The file that I am trying to read is in the following format:

Mr. Jones has a "dog" and a "cat"


I need to extract the dog and cat and put them into an array of Items.

Here are some snippets of my code:

while((str = xmlParse.readLine()) != null)
            {
               s = new Scanner (str).useDelimiter("\"");
             
               animal1 = s.next();
               animal2 = s.next();
             
               addItem(animal1, animal2); 
            
            }//while

Here is the addItem method:

public void addItem( String animal1, String animal2)
      {      
         pet[count] = new Item(animal1, animal2);
         count++;
      }

Any suggestions would be great and highly appreciated.


Thanks

You could do it like this:

//Read in the file:
java.io.File file = new java.io.File("myfile.txt"); //supply your file name.
java.io.FileReader FR = new java.io.FileReader(file);
StringBuffer buf = new StringBuffer();
int read=0;
try{
   char[] cbuf = new char[1024];
    while ((read  = FR.read(cbuf, 0, cbuf.length)) > -1){
    buf.append(cbuf, 0, read);			
  }
}
catch(Exception ex){
  System.out.println("ex thrown: "+ex);
}
finally{
  FR.close();
}

//Now parse the data read in:
String animalStr = buf.toString();
int q1 = animalStr.indexOf("\"");
if (q1 < 0){
  System.out.println("no animals");
  System.exit(0);
}
int q2 = animalStr.indexOf("\"", q1 + 1);
java.util.ArrayList<String> myAnimals = new java.util.ArrayList<String>();
while (q1 > 0 && q2 > 0){
    String animal = animalStr.substring(q1, q2);
    myAnimals.add(animal);
    q1 = animalStr.indexOf("\"", q2 + 1);
    if (q1 < 0) break;
    q2 = animalStr.indexOf("\"", q1 + 1);
}

//now print out:
for (String myAnimal : myAnimals){
  System.out.println(myAnimal);
}

Thank you for your help. I am trying your suggestion. I am currently getting StringIndexOutOfBoundsException error. Can anyone let me know where I am getting off track?

Here is a snippet of what I am working with:

while((str = xmlParse.readLine()) != null)
            {
                        
               int start = str.indexOf("\"") + 1; 
               int end = str.indexOf( ("\""), start );
            	
               String animal1 = str.substring(start, end);			
                         
               int start2 = str.indexOf(("\""), (end + 1) );
               int end2 = str.indexOf(("\""), start2 + 2);
            
               String animal2 = str.substring(start2, end2);
             				  
               addItem(animal1, animal2);
            }//while

Thanks for your help

Have you looked up what StringIndexOutOfBounds means, the method call that is throwing it and which line it is occurring on? The stack trace will tell you the last two of those. This is just basic debugging that you need to learn to do on your own. The exception message even tells you the index value that was out of bounds and some adding some basic System.out.println() statements can help you know the string and indexes that are causing the problem.

If you continue to have difficulties with it, post back the code, the exact exception message, and exactly what you do not understand about it.

String.indexOf throws a StringIndexOutOfBoundsException when either the index parameter is < 0 or > than the length of the String. Hence, make sure to check the indexes before sending them to the indexOf method.

Thank you for your help. I am trying your suggestion. I am currently getting StringIndexOutOfBoundsException error. Can anyone let me know where I am getting off track?

Here is a snippet of what I am working with:

while((str = xmlParse.readLine()) != null)
            {
                        
               int start = str.indexOf("\"") + 1; 
if (start < 0) continue; //check that first quote was found.
               int end = str.indexOf( ("\""), start );
            	
               String animal1 = str.substring(start, end);			
                         
               int start2 = str.indexOf(("\""), (end + 1) );
if (start2 < 0) continue; //check that first quote was found.
               int end2 = str.indexOf(("\""), start2 + 1); //add only 1.
//indexOf would throw a StringIndexOutOfBoundsException if the second parameter
//exceeds the length of the string.
            
               String animal2 = str.substring(start2, end2);
             				  
               addItem(animal1, animal2);
            }//while

Thanks for your help

String.indexOf throws a StringIndexOutOfBoundsException when either the index parameter is < 0 or > than the length of the String. Hence, make sure to check the indexes before sending them to the indexOf method.

indexOf() does not throw StringIndexOutOfBoundsException.

I have tried all that I can think of. I even used the code seperately and directly input a string and everything worked fine.

I am not sure why I am getting this error. Here is the actual error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1768)
at Item3.getXml(Item3.java:178)
at ItemsTest.main(ItemsTest.java:33)


Here is the code I am using. I line that is producing the error is bolded:

while((str = xmlParse.readLine()) != null)
            {
                        
               int start = str.indexOf("\"") + 1; 
               int end = str.indexOf( ("\""), start );
            	
               [b]String animal1 = str.substring(start, end);[/b]			
                         
               int start2 = str.indexOf(("\""), (end + 1) );
               int end2 = str.indexOf(("\""), start2 + 2);
            
               String animal2 = str.substring(start2, end2);
             				  
               addItem(animal1, animal2);
            }//while

Through using the method that jnetpro provided, I have found that the problem lies somewhere with "end". I just can't figure out what or where the problem lies with it.

Any suggestions would be appreciated.

Thanks

My Mistake, it is String.substring() that throws a StringIndexOutOfBoundsException if either parameter is < 0 or > the string length.

The solution is to check the int parameters you send to String.substring().

Carefully read the API documention on the indexOf() method you are calling and the reason for the -1 should be quite clear. From there it is a matter of determining why that is the value you are receiving.

This little error is testing my nerves. I can't seem to figure out what is causing this. Can anyone provide any suggestions?

Did you read the api on indexOf() and the conditions under which it will return a -1? Have you added System.out.println() statements to verify your parameters to indexOf() and substring().

You keep posting "I can't figure it out", but do not say what you have tried to do to solve this on your own. There is a point to learning how to debug things like this for yourself and we have tried to guide you towards this as much as possible without simply giving you the answer.

I have read and understand the API on indexOf() and substring() and have verified every single integer and string with System.out.println() everything displays just as it should then lastly displays the error. I have checked and rechecked. When I try to display the elements of the array I get the same error which points me back to the same location. Which I have checked again and again.

I have learned alot with this program, but this little thing stumps me.

If you have added println statement to verify str, start, and end then you should know exactly what string input is returning a -1 for which indexOf() call. You have all of the variables there leading to that error, so you just need to figure out why that particular string input is yielding the -1. Are you certain that the final value of "str" is not an empty line or one which has improper quotation marks?

Exactly, that is what I am working on now. The last line of the file does not have quotation marks at all, so I deleted that line, just for testing before I got into fixing that problem, and tested the program and still had that same problem. ( I replaced the line). So I have it set now to compare and if equal to that last line, not read. But the problem still exists. I found that through testing the error is with "end" . But I cannot figure out why.

Solved it. It was not even the substring like the error pointed to. That was just an indirect error. Thanks everyone

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.