13 Replies - 6150 Views - Last Post: 02 June 2011 - 02:26 PM Rate Topic: -----

#1 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

string manipulation java task

Posted 01 June 2011 - 09:00 AM

Here is my problem, iam trying to move the first letter in every word i input in the dialog and place them last in every word.
ex: "road to" = "oadr ot" and after this is done, i want to change them back to normal text ex. "oadr ot" = "road to" i think iam very close to the soulution but i dont se it! i think it is the "whitespaces" but cant resolve this on my own.


s1 = showInputDialog("word/words input");
char[] charArray = s1.toCharArray();
char firstLetter = s1.charAt(0);
text = s1.substring(1,s1.length()+0) + firstLetter;
showMessageDialog(null,(s1));



s1 = showInputDialog(null,"Word/words input"
char[] charArray = s1.toCharArray();
char lastLetter = s1.charAt(3);
s1 = s1.substring(0,3);
showMessageDialog(null,lastLetter + (s1);


Is This A Good Question/Topic? 0
  • +

Replies To: string manipulation java task

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,292
  • Joined: 20-September 08

Re: string manipulation java task

Posted 01 June 2011 - 09:17 AM

No substringing needed, just swap chars in each char[] ('a' below) from a given String


char temp = a[a.length - 1];
a[a.length - 1] = a[0];
a[0] = temp;

This post has been edited by g00se: 01 June 2011 - 09:18 AM

Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: string manipulation java task

Posted 01 June 2011 - 09:33 AM

Quote

No substringing needed, just swap chars in each char[] ('a' below) from a given String


Well, the guy's gonna need to get word1, word2, etc.
I'd probably split on non-alpha characters to get an array of String, and then do the standard 2-value swap on first and last character of each element of that array, as described by goose.

Should be obvious that this transformation repeats with a cycle of 2: f(f(a)) == a, f(f(f(a))) == f(a), etc. So reversing the transform is easily done by applying it a second time.

This post has been edited by jon.kiparsky: 01 June 2011 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,292
  • Joined: 20-September 08

Re: string manipulation java task

Posted 01 June 2011 - 10:42 AM

Quote

I'd probably split on non-alpha characters to get an array of String


Yes

String s = JOptionPane.showInputDialog(....);
String[] words = s.trim().split(" ");

Was This Post Helpful? 0
  • +
  • -

#5 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

Re: string manipulation java task

Posted 01 June 2011 - 10:46 AM

s1 = showInputDialog("word/words input");
		char[] charArray = s1.toCharArray();
		char firstLetter = s1.charAt(0);
 		char temp = s1[s1.length - 1];
		text[s1.length - 1] = s1[0];
		text[] = temp;


		showMessageDialog(null,(text));


Like this? but then i get many error message's.

This post has been edited by aleaur: 01 June 2011 - 10:53 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2674
  • View blog
  • Posts: 11,292
  • Joined: 20-September 08

Re: string manipulation java task

Posted 01 June 2011 - 10:48 AM

See the code i posted
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: string manipulation java task

Posted 01 June 2011 - 10:52 AM

text[] = temp;  




An array of char is not a char (you can't eat a box of chocolates, you can only eat each of the chocolates in the box)

You need to specify which element of text[] you're assigning the value of temp to.
Was This Post Helpful? 0
  • +
  • -

#8 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

Re: string manipulation java task

Posted 01 June 2011 - 11:24 AM

I give up :stupid: , I think i need to read some more!
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: string manipulation java task

Posted 01 June 2011 - 11:39 AM

Quote

I give up!

Wrong answer

Quote

I think i need to read some more!

Right answer.

text[] is not an object that you can refer to. As part of a declaration, it means that text will be the name of an array, whose type you will have to have specified: char text[], for example. Once the variable is declared, you can refer to the array as a whole as text - for example, you can pass a reference to that array into a method:

char newArray[] = mungeArray(text);
will presumably munge the array called text and return the result, which you're assigning to the array called newArray.

If you want to refer to an element of an array, however, you use the bracket notation, but you have to indicate which element of the array you mean:

text[0] = temp;

for example.

Is that more clear?
Was This Post Helpful? 1
  • +
  • -

#10 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

Re: string manipulation java task

Posted 01 June 2011 - 12:20 PM

Thanx for your help but i relly think i need to read more about arrys and char. My friend told me to do like this.


text = showInputDialog("text input");

text = text.replaceAll("\\b(\\w)(\\w+)\\b", "$2$1")


And it works perfect! (But i dont understand it) So i really want to learn the other way's to solve this kind of problem. I will try and find a solution like your's and learn from that.
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,848
  • Joined: 19-March 11

Re: string manipulation java task

Posted 01 June 2011 - 12:41 PM

Make your friend sit down and explain that to you. If he's going to give you code, you should understand it.
regex (regular expressions - that's what he's using there) is a little tricky, but you can get the basics in a cup of coffee or two.

I won't try to explain all of it, but here's a few clues:

"\\b(\\w)(\\w+)\\b"
This is a regular expression. "\\b" means a word boundary (the double backslash is a complication I won't go into). "\\w" means a "word character": a character that can be part of a Java identifier, alphanumeric characters and the underscore. + means 1 or more of that. Parentheses surround a group to be saved for further use.

Okay, so this takes a string like "abc" and divides it into four sections, two of which are sort of imaginary. The first is the word boundary. The second is a single word character: 'a'. The third is one or more word characters: "bc". The last is the next word boundary. The second and third groups are kept for future use.

"$2$1"


Here's where they're used. Now we take group number 2 ($2) and group number 1 ($1) in that order, and that's the replacement String. So, for a String like "abd def ghi", we're going to find each set of characters that has a word boundary on either side, and we're going to grab the first word character of that and call it $1, then we're going to take one or more word characters and call those $2, and we're going to replace the original blob with $2$1.

More about that here, if you wnat to do some reading.



All that being said, you should also learn how to do it with the arrays, because you have to understand arrays. Absolutely required.
Was This Post Helpful? 0
  • +
  • -

#12 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

Re: string manipulation java task

Posted 01 June 2011 - 01:05 PM

More about that here, if you wnat to do some reading


Link not working! And yes! I spoke with him, and he tried to explain his method but you do it better. You see, i have study c# and c++ for one year now and i'am starting with java next spring. And i feel so dumb you can't belive it! Cause in c, i have never seen this kind of problem! People say the only difference is the syntax,, But i don't belive that!
Was This Post Helpful? 0
  • +
  • -

#13 aleaur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-June 11

Re: string manipulation java task

Posted 01 June 2011 - 01:23 PM

Got it!

if (indata.equals("2")){
			text = showInputDialog("word/words input");
			char[] s1 = text.toCharArray();
			char first = s1[0];
			for(int i = 0; i < s1.length -1; i++)
		        s1[i] = s1[i+1];
			s1[s1.length - 1] = first;
			text = new String(s1);


Now i just need to go back!(and to handle more than one word) My friend just gave my a additional task: What if you must put the letters "popo" after the word? Like this: "road to"= oadrpopo otpopo? In the first code it's simpel:

showMessageDialog(null,(text)+ "popo");

But the other way around, it's way more complicated, any tips?
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10437
  • View blog
  • Posts: 38,648
  • Joined: 27-December 08

Re: string manipulation java task

Posted 02 June 2011 - 02:26 PM

You could use a StringBuilder to delete() the first char, and append() it to the end of the StringBuilder.

Also, you mentioned earlier you wanted to read up on arrays. You might want to check out this tutorial for more information on them.

Hope this helps some. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1