Results 1 to 20 of 27
Thread: Problem with CHAR
- 04-24-2012, 07:11 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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)); }
Any ideas? Clearly I've done something wrong.
Thanks in advance!
-
Re: Problem with CHAR
Why are you looping down: i-- and not up i++? Am I missing something?
- 04-24-2012, 07:24 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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
-
Re: Problem with CHAR
You're welcome!
- 04-24-2012, 04:32 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 301
- Rep Power
- 9
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.
- 04-24-2012, 04:50 PM #6
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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
Java Code:i<array.length
Java Code:i<=array.length-1
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]; }
- 04-25-2012, 12:55 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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(); }
- 04-25-2012, 01:02 AM #8
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-25-2012, 01:03 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
Re: Problem with CHAR
Ahh. That makes perfect sense. Brilliant.
Thanks!
- 04-25-2012, 01:29 AM #10
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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? :]
- 04-25-2012, 02:51 AM #11
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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)
- 04-25-2012, 02:56 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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; }
- 04-25-2012, 03:15 AM #13
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-25-2012, 03:19 AM #14
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
Re: Problem with CHAR
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. :]
- 04-25-2012, 03:26 AM #15
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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?
- 04-25-2012, 03:30 AM #16
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-25-2012, 03:30 AM #17
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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.
- 04-25-2012, 03:32 AM #18
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
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.
- 04-25-2012, 03:33 AM #19
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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.
- 04-25-2012, 03:35 AM #20
Member
- Join Date
- Apr 2012
- Posts
- 25
- Rep Power
- 0
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; }
Java Code:private StringBuffer text = new StringBuffer(""); private StringBuffer result = new StringBuffer("");
Similar Threads
-
Printing Dec to Char Problem
By rjagan in forum New To JavaReplies: 6Last Post: 09-24-2011, 12:50 PM -
Problem with scanning char
By danthegreat in forum New To JavaReplies: 4Last Post: 09-09-2011, 08:01 PM -
char problem.
By santa in forum New To JavaReplies: 5Last Post: 02-18-2011, 01:24 AM -
replaceALL(char oldChar, char newChar) method
By arson09 in forum New To JavaReplies: 0Last Post: 04-28-2010, 06:48 AM -
Problem with if statement - Char
By KardKaper in forum New To JavaReplies: 8Last Post: 12-22-2009, 02:14 PM
Bookmarks