Results 1 to 5 of 5
  1. #1
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Question Read String as Chars Errors

    I read a a string file(.txt) with BufferedReader which works fine.

    I then check that all the characters are valid according to rules that I set (Certain ASCII values will throw exception).
    I then set it up as follows:

    ProgramString = inputString; //The Program
    StringLength = inputString.length() - 1; //The Program Length
    CurPos = 0; //Current Cursor Position

    Now I go through the string one char at a time, the CurPos keeps track of where I'm at the current moment.
    The string I use as test.. [ "hello words het d" ]

    My program works for the first 3 words, but for some reason it never reaches the 'd'.
    At the whitespace before the 'd' I call a method consumeWhitespace, which then calls consumeChar

    Java Code:
    public boolean consumeChar() {
    		// Consume the current character. Return true if a character could be
    		// consumed, otherwise false.
    		if (hasNextChar() == true) {
    			if (ProgramString.charAt(CurPos + 1) == '\n') {
    				CurPos = CurPos + 1;
    			}
    			CurPos = CurPos + 1;
    			return true;
    		}else return false;
    	}
    Java Code:
    public boolean hasNextChar() {
    		// return true if the position *after* our current position in the string is
    		// not past the string's end (ie. there's at least two characters left in
    		// the string to consume, the current and the next one).
    		
    		if (CurPos <= StringLength && CurPos + 1 <= StringLength) {
    			return true;
    		}else return false;
    		
    	}

    The error I get:

    Java Code:
    hello words het d   //JUST THE STRING
    16	d      //STRING LENGTH ASWELL AS CHAR AT THAT POINT
    
    The Symbol Is: hello   //THE FIRST SYMBOL  [set of chars]
    
    The Symbol Is: words
    
    The Symbol Is: het
    Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    	at java.util.Arrays.copyOf(Unknown Source)
    	at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
    	at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source)
    	at java.lang.AbstractStringBuilder.append(Unknown Source)
    	at java.lang.StringBuilder.append(Unknown Source)
    	at flapjack.types.FJSymbol.readForm(FJSymbol.java:50)
    	at flapjack.reader.FJReader.readForm(FJReader.java:34)
    	at HQ.main(HQ.java:22)
    This is when I call the hasNextChar, but when I change the code as follows it does not give the error but it won't do what it is suppose to do.

    Java Code:
    public boolean hasNextChar() {
    		// return true if the position *after* our current position in the string is
    		// not past the string's end (ie. there's at least two characters left in
    		// the string to consume, the current and the next one).
    		
    		if (CurPos != StringLength && CurPos + 1 != StringLength) {
    			return true;
    		}else return false;
    		
    	}

    EDIT:

    Java Code:
    I also tried this 
    public boolean consumeChar() {
    		// Consume the current character. Return true if a character could be
    		// consumed, otherwise false.
    		if (CurPos <= StringLength) {
    			/*if (ProgramString.charAt(CurPos + 1) == '\n') {
    				CurPos = CurPos + 1;
    			}*/
    			CurPos = CurPos + 1;
    			return true;
    		}else return false;
    	}
    But then I get an array index out of bounce error, which does not make sense as when I call this method my current CurPos = 15;
    So I should be able to increment it and go forth with my business.
    Last edited by Asmicor; 08-20-2013 at 03:43 PM.

  2. #2
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Read String as Chars Errors

    OK, so I was able to make it work somewhat. I am now able to read all the chars en process them, but I still get the out of bounds error after that. At least now I can continue with test cases, but this problem is not fixed yet.

  3. #3
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Read String as Chars Errors

    Quote Originally Posted by Asmicor View Post
    OK, so I was able to make it work somewhat. I am now able to read all the chars en process them, but I still get the out of bounds error after that. At least now I can continue with test cases, but this problem is not fixed yet.
    Not sure but try this at line 5 of last block of your code :
    Java Code:
    if (CurPos <= StringLength-1)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Read String as Chars Errors

    Have you read what the String.split( ... ) method can do for you?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Asmicor is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Read String as Chars Errors

    So I was able to fix the problem at the end, it was related to a while loop that called one of these methods as a condition. The while loop had 2 conditions, but for some reason I tried and flipped the positions of them. Now the loop first checks if CurPos <= StringLength before checking the other condition. Can't believe I spent 3 hours looking for this error. :0

Similar Threads

  1. chars being read from a file not treated like literals
    By ajetrumpet in forum New To Java
    Replies: 2
    Last Post: 04-05-2013, 01:53 PM
  2. Replies: 2
    Last Post: 07-21-2011, 03:46 AM
  3. Replies: 10
    Last Post: 03-29-2011, 11:09 AM
  4. Replies: 1
    Last Post: 02-07-2011, 08:22 PM
  5. Make String into chars
    By myst in forum New To Java
    Replies: 19
    Last Post: 06-20-2010, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •