Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Problem with CHAR

    Hello, all!

    Working on a program to take a string, iterate through each character on at a time, modify those characters by an offset value to change them into new characters, and spit out the result. Having an odd issue trying to assign a character from a StringBuffer to a char.

    Java Code:
    for (int i = 0; i <= text.length(); i--)
    		{
    			// Pull one character from string
    			char tempChar = text.charAt(i);
    			// Get the value of the character
    			value = Character.getNumericValue(tempChar);
    			if (negative = true)
    				// Adjust the value of the character by the offset
    				value = value - offset;
    			else
    				// Adjust the value of the character by the offset
    				value = value + offset;
    			// Convert value back into a character
    			tempChar = (char) value;
    			// Append to result string
    			result.append(Character.toString(tempChar));
    		}
    Line 4 of that bit is where I'm getting an index out of range: -1 exception. text is a StringBuffer.

    Any ideas? Clearly I've done something wrong.

    Thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    No. You're not. But apparently I am. That's what I was missing.

    I was originally counting down. Epic fail!

    Thanks so much. :P

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Problem with CHAR

    You're welcome!

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Problem with CHAR

    "i <= text.length()"

    If you are indexing you may not go up to equal the length because you start with element zero -> you will be out of range probably here too.

  6. #6
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Whenever you use a for loop to iterate through any array, the normal convention is to start with
    Java Code:
    i=0
    and you always test for
    Java Code:
    i<array.length
    . You won't see most java programmers testing for
    Java Code:
    i<=array.length-1
    So the conventional for loop will look like this and ensure you don't generate an ArrayIndexOutOfBoundsException.

    Java Code:
    int[]  ints = {1,3,5,7,9,11};
    int[]  newints = new int[ints.length];  // Have to initialize the new array.  Make it the same size as the source for my copy
    
    for (int i=0; i<ints.length; i++)  {  // Arrays such as this use ".length" for their size.  
                                          // Other things will have .length() or .size() methods.
    	newints[i] = ints[i];
    }

  7. #7
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Right. I understand how a for statement iterates through something. Or at least I think I do... Yet I'm getting a StringIndexOutofBounds Exception equal to the length of the string I'm trying to process. Not sure why, exactly. I'm using a StringBuffer, but the .length() method should still function the same.

    Java Code:
    	public String encrypt(int offset)
    	{
    		int value;
    		
    		// Iterate from = to the length of the string
    		for (int i = 0; i <= text.length(); i++)
    		{
    			// Pull one character from string
    			char tempChar = text.charAt(i);
    			// Get the value of the character
    			value = Character.getNumericValue(tempChar);
    			if (negative = true)
    				// Adjust the value of the character by the offset
    				value = value - offset;
    			else
    				// Adjust the value of the character by the offset
    				value = value + offset;
    			// Convert value back into a character
    			tempChar = (char) value;
    			// Append to result string
    			result.append(Character.toString(tempChar));
    		}
    		
    		return text.toString();
    	}
    Any ideas?

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Your problem is in your for statement's comparator. All arrays, including strings, in Java have zero origin. That means you go from i=0 to i<thing.length(). Not i<=thing.length(). Your out of bounds exception is due to this.

  9. #9
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Ahh. That makes perfect sense. Brilliant.

    Thanks!

  10. #10
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Aaaaand, unfortunately now I have another problem. The loop iterates correctly, but returns blank characters. Perhaps char int values don't work the way I think they do?

    I'd thought that, for example, the unicode value of 'A' would return 65. Adjusting this by 3 would then return 'D'. But clearly that isn't the case.

    Perhaps someone could explain? :]

  11. #11
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Generally, that cast of the int as char should be fine. But I do see a problem here on this if statement from your earlier post:
    Java Code:
    if (negative = true)
    Did you really intend to assign the value of true to the variable negative? Or were you testing if the value of negative had been set to true?

  12. #12
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    I was testing if the value of negative had been set to true, but I simplified it to just test the condition that negative represents. Here's the updated version.

    Java Code:
    	public StringBuffer encrypt(int offset)
    	{
    		int value;
    		
    		
    		
    		// Iterate from = to the length of the string
    		for (int i = 0; i < text.length(); i++)
    		{
    			// Pull one character from string
    			char tempChar = text.charAt(i);
    			// Get the value of the character
    			value = Character.getNumericValue(tempChar);
    			if (offset < 0)
    				// Adjust the value of the character by the offset
    				value = value - offset;
    			else
    				// Adjust the value of the character by the offset
    				value = value + offset;
    			// Convert value back into a character
    			tempChar = (char) value;
    			// Append to result string
    			result.append((tempChar));
    		}
    		
    		return result;
    	}

  13. #13
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    I don't think you are doing what you really want to then. If the offset is negative, what happens when you subtract it from a value. Two negatives make a..... ;-)

    You do not need to check the offset at all. It's an offset, I would simply add it and be done. If it's negative, you're adding a negative (i.e. subtracting the absolute value). If it's positive you're adding it. Done.

  14. #14
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Quote Originally Posted by jlczuk View Post
    I don't think you are doing what you really want to then. If the offset is negative, what happens when you subtract it from a value. Two negatives make a..... ;-)

    You do not need to check the offset at all. It's an offset, I would simply add it and be done. If it's negative, you're adding a negative (i.e. subtracting the absolute value). If it's positive you're adding it. Done.
    Yeah, I realized the math on that was bad after I posted it. But that's not the main problem. The problem is that when a take a char and create a new char using the int value of the original char modified by an offset number or some sort I get nothing but blank space. Even with bad math, I would still get some sort of char.

    Thanks for catching that though. :]

  15. #15
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Ok, I see the real problem now. What is the string you are constructing and what is the one you are returning?

  16. #16
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Sorry for spamming my posts, I'm getting tired and trying to do too much too quickly.

    One other thing I noticed. I don't see where either the result or text variables are being initialized prior to entering that method. Generally speaking, it isn't a good idea to modify instance variables from inside a method like that as a side-effect. If that is the method's clear intention, that is fine. But the method is returning a string, that happens to be an instance variable (which is a bit redundant) and also updating the result instance variable.

    I think that result should be local to that method. If you want to set an instance variable to what that method returns, that's fine.

  17. #17
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Uh. I'm assigning user input to a stringbuffer called text, iterating each character in that stringbuffer through my for loop to alter the characters, and appending them to an empty results stringbuffer that is outputted to the display. The return string is always the result of the original "text" string being run through that loop.

  18. #18
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with CHAR

    Can you please post a fresh copy of the code? I'm having trouble finding the right one to look at.

  19. #19
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Yeah, the use of class variables is intended there. In case I'd like to re-use either the original string or the modified result later.

  20. #20
    Razion is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: Problem with CHAR

    Java Code:
    public StringBuffer encrypt(int offset)
    	{
    		int value;
    		System.out.println(text);
    		
    		
    		// Iterate from = to the length of the string
    		for (int i = 0; i < text.length(); i++)
    		{
    			// Pull one character from string
    			char tempChar = text.charAt(i);
    			// Get the value of the character
    			value = Character.getNumericValue(tempChar);
    			if (offset < 0)
    				// Adjust the value of the character by the offset
    				value -= Math.abs(offset);
    			else
    				// Adjust the value of the character by the offset
    				value += offset;
    			// Convert value back into a character
    			tempChar = (char) value;
    			// Append to result string
    			result.append((tempChar));
    		}
    		
    		return result;
    	}
    And those variables are declared as

    Java Code:
    	private StringBuffer text = new StringBuffer("");
    	private StringBuffer result = new StringBuffer("");

Page 1 of 2 12 LastLast

Similar Threads

  1. Printing Dec to Char Problem
    By rjagan in forum New To Java
    Replies: 6
    Last Post: 09-24-2011, 11:50 AM
  2. Problem with scanning char
    By danthegreat in forum New To Java
    Replies: 4
    Last Post: 09-09-2011, 07:01 PM
  3. char problem.
    By santa in forum New To Java
    Replies: 5
    Last Post: 02-18-2011, 12:24 AM
  4. replaceALL(char oldChar, char newChar) method
    By arson09 in forum New To Java
    Replies: 0
    Last Post: 04-28-2010, 05:48 AM
  5. Problem with if statement - Char
    By KardKaper in forum New To Java
    Replies: 8
    Last Post: 12-22-2009, 01:14 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
  •