11 Replies - 1207 Views - Last Post: 02 May 2010 - 10:16 PM Rate Topic: -----

#1 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Problem using String.split()

Posted 02 May 2010 - 08:05 PM

I am trying to split a string with . as the delimiter, but it refuses to work. If I change the delimiter, to just about anything other than . this code works, but if you run it as is, you will get ArrayOutOfBounds. Is there something special about . that I don't know about?

/**
 * @(#)MyHash.java
 *
 *
 * @author
 * @version 1.00 2010/5/1
 */


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;


public class MyHash {
    private static String[] reserved = new String[100];
    private static List list = new LinkedList();
    private static List list2 = new LinkedList();
    public MyHash() {

    }

    public static void main (String args[]){
      Hashtable hashtable  = new Hashtable(997);
      makeReserved();
      readFile();                                                 
      String line;
      ListIterator itr = list.listIterator();
      int listIndex = 0;
      while (listIndex < list.size()) {                                       
      	  
      	    if (itr.hasNext()){
      	      line = itr.next().toString();
      	      //HERE IS THE PROBLEM
      	      String[] words = line.split(".");//CHANGE THE . TO ANYTHING ELSE AND IT WORKS
      	      System.out.println(words[0]);//JUST CHECKING IF THE WHOLE PROCESS IS WORKING
      	    }
            listIndex++;
            }
      }
            
      
    
     
    




    public static void readFile() {
        String text;
        String[] words;
        BufferedReader in = null; 
        try {
            in = new BufferedReader(new FileReader("MyHash.java")); //NAME OF INPUT FILE
            

        } catch (FileNotFoundException ex) {
            Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            while ((text = in.readLine()) != null){
            text = text.trim();
            words = text.split("\\s+");
            for (int i = 0; i < words.length; i++){
                list.add(words[i]);
            }
            for (int j = 0; j < reserved.length; j++){
                if (list.contains(reserved[j])){
                    list.remove(reserved[j]);
                }
            }
            
           
        }
            
        } catch (IOException ex) {
            Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            in.close();
        } catch (IOException ex) {
            Logger.getLogger(MyHash.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }



    public static int keyIt (int x) {
       int key = x % 997;
       return key;
    }

    public static int horner (String word){
    	int length = word.length();
        char[] letters = new char[length];

        for (int i = 0; i < length; i++){
            letters[i]=word.charAt(i);
        }

        char[] alphabet = new char[26];
        String abc = "abcdefghijklmnopqrstuvwxyz";

        for (int i = 0; i < 26; i++){
            alphabet[i]=abc.charAt(i);
        }

        int[] numbers = new int[length];
        int place = 0;
        for (int i = 0; i < length; i++){
            for (int j = 0; j < 26; j++){
                if (alphabet[j]==letters[i]){
                    numbers[place]=j+1;
                    place++;

                }
            }
        }

        int hornered = numbers[0] * 32;

        for (int i = 1; i < numbers.length; i++){

            hornered += numbers[i];
            if (i == numbers.length -1){
                return hornered;
            }
            hornered = hornered % 997;
            hornered *= 32;
        }
        return hornered;
    }

    public static String[] makeReserved (){
        reserved[0] = "abstract";
        reserved[1] = "assert";
        reserved[2] = "boolean";
        reserved[3] = "break";
        reserved[4] = "byte";
        reserved[5] = "case";
        reserved[6] = "catch";
        reserved[7] = "char";
        reserved[8] = "class";
        reserved[9] = "const";
        reserved[10] = "continue";
        reserved[11] = "default";
        reserved[12] = "do";
        reserved[13] = "double";
        reserved[14] = "else";
        reserved[15] = "enum";
        reserved[16] = "extends";
        reserved[17] = "false";
        reserved[18] = "final";
        reserved[19] = "finally";
        reserved[20] = "float";
        reserved[21] = "for";
        reserved[22] = "goto";
        reserved[23] = "if";
        reserved[24] = "implements";
        reserved[25] = "import";
        reserved[26] = "instanceof";
        reserved[27] = "int";
        reserved[28] = "interface";
        reserved[29] = "long";
        reserved[30] = "native";
        reserved[31] = "new";
        reserved[32] = "null";
        reserved[33] = "package";
        reserved[34] = "private";
        reserved[35] = "protected";
        reserved[36] = "public";
        reserved[37] = "return";
        reserved[38] = "short";
        reserved[39] = "static";
        reserved[40] = "strictfp";
        reserved[41] = "super";
        reserved[42] = "switch";
        reserved[43] = "synchronize";
        reserved[44] = "this";
        reserved[45] = "throw";
        reserved[46] = "throws";
        reserved[47] = "trasient";
        reserved[48] = "true";
        reserved[49] = "try";
        reserved[50] = "void";
        reserved[51] = "volatile";
        reserved[52] = "while";
        reserved[53] = "=";
        reserved[54] = "==";
        reserved[55] = "!=";
        reserved[56] = "+";
        reserved[57] = "-";
        reserved[58] = "*";
        reserved[59] = "/";
        reserved[60] = "{";
        reserved[61] = "}";




        return reserved;
    }


}


This post has been edited by mattpd: 02 May 2010 - 08:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem using String.split()

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Problem using String.split()

Posted 02 May 2010 - 08:14 PM

If the String does not contain the delim, then the array from the split() call has a length of 0. So when you print out the 0 elem of an array with length 0, you get an ArrayIndexOutOfBoundsException.
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem using String.split()

Posted 02 May 2010 - 08:24 PM

If you wanna split on '.' you need to escape it:


words = line.split("\\.");



From the debugger: :)

displayName=hashtable]	Hashtable	"size = 0"	
displayName=itr]	LinkedList$ListItr	#73	
displayName=line]	String	"java.io.BufferedReader;"	
displayName=listIndex]	int	24	
displayName=words]	String[]	#187(length=3)	
displayName=[0]]	String	"java"	
displayName=[1]]	String	"io"	
displayName=[2]]	String	"BufferedReader;"	


This post has been edited by n8wxs: 02 May 2010 - 08:32 PM

Was This Post Helpful? 2
  • +
  • -

#4 mattpd  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 50
  • Joined: 15-March 08

Re: Problem using String.split()

Posted 02 May 2010 - 09:35 PM

View Postn8wxs, on 02 May 2010 - 07:24 PM, said:

If you wanna split on '.' you need to escape it:


words = line.split("\\.");



From the debugger: :)

displayName=hashtable]	Hashtable	"size = 0"	
displayName=itr]	LinkedList$ListItr	#73	
displayName=line]	String	"java.io.BufferedReader;"	
displayName=listIndex]	int	24	
displayName=words]	String[]	#187(length=3)	
displayName=[0]]	String	"java"	
displayName=[1]]	String	"io"	
displayName=[2]]	String	"BufferedReader;"	


Thank you sir
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem using String.split()

Posted 02 May 2010 - 09:40 PM

View Postn8wxs, on 02 May 2010 - 09:24 PM, said:

If you wanna split on '.' you need to escape it:


words = line.split("\\.");


Don't see why ? Why do you have to do this ?
n8wxs you know how to use a debugger at your age ?

This post has been edited by pbl: 02 May 2010 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem using String.split()

Posted 02 May 2010 - 09:43 PM

LOL!!
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem using String.split()

Posted 02 May 2010 - 09:51 PM

View Postn8wxs, on 02 May 2010 - 10:43 PM, said:

LOL!!

Hapy I made you laught (between you and me I do not know how to use a debugger). I prefer to write code without bug :)
But seriously my question is still there. Why do you need to escape it ?
Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem using String.split()

Posted 02 May 2010 - 10:02 PM

Because my friend, the argument to string.split() is a regular expression. To split on a period it has to be escaped because otherwise a "." matches anything.

See Class Pattern:


Quote

Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
...

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem using String.split()

Posted 02 May 2010 - 10:06 PM

Thanks... I'll go to bed less dum tonight
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Problem using String.split()

Posted 02 May 2010 - 10:09 PM

View Postpbl, on 03 May 2010 - 12:51 AM, said:

View Postn8wxs, on 02 May 2010 - 10:43 PM, said:

LOL!!

(between you and me I do not know how to use a debugger). I prefer to write code without bug :)


Same here! I have never used a debugger to help me debug my code.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Problem using String.split()

Posted 02 May 2010 - 10:15 PM

View Postmacosxnerd101, on 02 May 2010 - 11:09 PM, said:

View Postpbl, on 03 May 2010 - 12:51 AM, said:

View Postn8wxs, on 02 May 2010 - 10:43 PM, said:

LOL!!

(between you and me I do not know how to use a debugger). I prefer to write code without bug :)


Same here! I have never used a debugger to help me debug my code.

Hey macosxnerd101 this is why I love you :)
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Problem using String.split()

Posted 02 May 2010 - 10:16 PM

:flowers:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1