6 Replies - 4972 Views - Last Post: 07 May 2007 - 09:09 PM Rate Topic: -----

#1 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Assigning strings

Posted 07 May 2007 - 10:30 AM

Hello,

iím having difficulties assigning one string to the other. Iím required to assess each char in the string then assign t(i) = s(i). Before I evaluate each character in the string Iím trying to assign them using charAt().

Hereís what I did so far:

String string	= new String("Hello!");
String string2   = new String("");
		
StringBuffer s   = new StringBuffer(string);
StringBuffer t   = new StringBuffer(string2);
	   	
		   
for(int i = 0; i < s.length(); i++)
{	
t.charAt(i) = s.charAt(i);
//t.codePointAt(i) = s.codePointAt(i); 
}		   	




Although this doesnít compile. Any other suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: Assigning strings

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Assigning strings

Posted 07 May 2007 - 10:47 AM

The charAt() method does not actually perform assignments - it returns the character at the specified index.

Try assigning directly.

also, can you post the exact error messages you receive?
Was This Post Helpful? 0
  • +
  • -

#3 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Assigning strings

Posted 07 May 2007 - 11:08 AM

Quote

Hello Amadeus,
I tried assigning them directly t(i) = s(i), although I get the following error:

--------------------Configuration: <Default>--------------------
C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\Palindrome\src\Palindrome.java:27: cannot find symbol
symbol : method t(int)
location: class Palindrome
t(i) = s(i);
^
C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\Palindrome\src\Palindrome.java:27: cannot find symbol
symbol : method s(int)
location: class Palindrome
t(i) = s(i);
2 errors
Process completed.
---------------------------------------------------------------------------------------------
I then tried the following t.setCharAt(i, s.charAt(i)) but no luck!

--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.setCharAt(StringBuffer.java:210)
at Palindrome.main(Palindrome.java:26)

Any other suggestions?

Thanks

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Assigning strings

Posted 07 May 2007 - 11:28 AM

The syntax for array subscripts are the [] brackets - the compiler is seeing the () and making the assumption that it is a call to a method.

Try t[i] = s[i];
Was This Post Helpful? 0
  • +
  • -

#5 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Assigning strings

Posted 07 May 2007 - 12:15 PM

It still didn't compile. Here's my code:
 
public class Palindrome 
{
	public static void main (String[] args) 
	{
		  String string	= new String("pop");
	 	String string2   = new String("");
		
		StringBuffer s   = new StringBuffer(string);
		StringBuffer t   = new StringBuffer(string2);
		
		for(int i = 0; i < s.length(); i++)
		t[i] = s[i]; 		
	}
}



And here's the error:
--------------------Configuration: <Default>--------------------
C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\Palindrome\src\Palindrome.java:23: array required, but java.lang.StringBuffer found
t[i] = s[i];
^
C:\Program Files\Xinox Software\JCreatorV4LE\MyProjects\Palindrome\src\Palindrome.java:23: array required, but java.lang.StringBuffer found
t[i] = s[i];
^
2 errors

That should work!
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Assigning strings

Posted 07 May 2007 - 12:31 PM

You'll have to use the following methodology...the setCharAt() method:
t.setCharAt(i, s.charAt(i))


The problem is you have declared the StringBufffer object with an initial value of an empty string - therefore you are trying to set the value of a char at an index that dops not exist. Use the setLength() method to resize the StringBuffer to ensure that you have the proper amount of room.
Was This Post Helpful? 0
  • +
  • -

#7 TwoSpots  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 45
  • Joined: 02-February 07

Re: Assigning strings

Posted 07 May 2007 - 09:09 PM

Amadeus

I got'er running! Thanks for the pointers - much appreciated!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1